Consulting

Results 1 to 9 of 9

Thread: how to have a selection of last month

  1. #1

    Red face how to have a selection of last month

    i have a date column and a price column. I'll attach the file
    I wanna select all the prices of last month from 1st day to the last day.
    sometimes there's no price for the first day so the selection must be started from second day if there's no second day it must be started from third day and...
    Attached Files Attached Files
    Last edited by ffarshadd; 06-14-2017 at 01:15 AM.

  2. #2
    I wanna select all the prices of last month from 1st day to the last day.
    Define last month. It could be December? or month before actual month? or actual month?
    witch year?
    Also you have date doubled (for April 2017 ...)
    it's not so clear what you want...

  3. #3
    Quote Originally Posted by Tom Jones View Post
    Define last month. It could be December? or month before actual month? or actual month?
    witch year?
    Also you have date doubled (for April 2017 ...)
    it's not so clear what you want...
    by saying last month i meant the previous month. for example we're in 6th month of 2017 i'll need to select the whole 5th month of 2017= 1st of may to 30th of may in 2017
    I'll delete the date doubled by this code: Cells.RemoveDuplicates Columns:=Array(1)
    imagine they don't exist.
    thanx for answering

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Try this -- I faked in some May 2017 data since that's the 'Last Month' and there was none in the test data


    Option Explicit
    
    Sub Macro1()
    Dim r As Range, r1 As Range
    Set r = ActiveSheet.Cells(1, 1).CurrentRegion
    r.Cells(1, 1).AutoFilter
    r.AutoFilter Field:=1, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic
    Set r1 = r.SpecialCells(xlCellTypeVisible)
    If r1.Areas.Count = 2 Then Set r1 = r1.Areas(2)
         r1.Interior.Color = vbGreen
        r.Parent.AutoFilterMode = False
        r1.Select
    End Sub
    Attached Files Attached Files
    Last edited by Aussiebear; 04-16-2023 at 10:29 PM. Reason: Reduced the whitespaces
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    In CF put this formula

    =AND(A2>=EOMONTH(TODAY(),-2)+1,A2<EOMONTH(TODAY(),-1))
    Last edited by Aussiebear; 04-16-2023 at 10:29 PM. Reason: Added code tags

  6. #6
    Quote Originally Posted by Paul_Hossler View Post
    Try this -- I faked in some May 2017 data since that's the 'Last Month' and there was none in the test data


    Option Explicit
    
    Sub Macro1()
    Dim r As Range, r1 As Range
    Set r = ActiveSheet.Cells(1, 1).CurrentRegion
    r.Cells(1, 1).AutoFilter
    r.AutoFilter Field:=1, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic
    Set r1 = r.SpecialCells(xlCellTypeVisible)
    If r1.Areas.Count = 2 Then Set r1 = r1.Areas(2)
        r1.Interior.Color = vbGreen
        r.Parent.AutoFilterMode = False
        r1.Select
    End Sub
    thanx for answering but it didn't work
    Last edited by Aussiebear; 04-16-2023 at 10:31 PM. Reason: Reduced the whitespace

  7. #7
    It works for me!
    I mean Paul_Hossler code's.
    In what way it didn't work? Error? Don't select wright cells?

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Quote Originally Posted by ffarshadd View Post
    thanx for answering but it didn't work
    Details?
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Here is a solution that uses no worksheet functions
    Option Explicit
    
    Sub SelectLastMonthsPrices()
    'Date / price columns must be sorted ascending before running sub
    Dim FirstCel As Range
    Dim LastCel As Range
    Dim FirstDay As Date
    Dim LastDay As Date
    Dim Cel As Range
    Dim WholePreviousMonthPrices As Range
    'Range("A:B").Sort Header:=xlYes
    FirstDay = DateAdd("m", -1, Format(Date, "mm/1/yyyy"))
    LastDay = DateAdd("d", -1, Format(Date, "mm/1/yyyy"))
    Set FirstCel = ActiveSheet.Cells(2, 1)
    Do While FirstCel < FirstDay
         Set FirstCel = FirstCel.Offset(1)
    Loop
    Set LastCel = FirstCel.End(xlDown)
    Do While LastCel > LastDay
          Set LastCel = LastCel.Offset(-1)
    Loop
    Set WholePreviousMonthPrices = Range(FirstCel, LastCel).Offset(0, 1)
    WholePreviousMonthPrices.Select
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •