Results 1 to 8 of 8

Thread: excel - selecting sheets dependant on date in sheet name

  1. #1

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    excel - selecting sheets dependant on date in sheet name

    Hi,

    I have a workbook that is made up of 13 sheets, named January to December and one called reports.

    Now i have a sub on the report sheet that runs through all the other sheets and copies certain data to the reports sheet in a nice list.

    But i want to add a sort of manual filter to the reports list by way of a combobox.

    Options will be:
    "All Months" - Copy relevant data from all sheets
    "Previous Months" - Copy data from sheets PREVIOUS to system date
    "Future Months" - Copy all data from sheets AFTER system date

    Im sure you get the idea.

    I figured the combobox_change event could fire the current sub, and then i'll have a check in there to see what the combo's value is. Any idea how i can incorporate that so it checks the date and matches it to the sheet names?

    Here is the current sub.
    VB Code:
    1. Private Sub Worksheet_Activate()
    2. Dim rng As Range
    3. Dim lLastRow As Integer
    4. Dim iNextRow As Integer
    5. Dim x As Integer
    6. Dim bFound As Boolean
    7.  
    8. Dim ws As Worksheet
    9. Dim sht_Rpt As Worksheet  'Set a handle for the Reports Sheet
    10. Set sht_Rpt = Sheets("Reports")
    11.  
    12.     sht_Rpt.Rows("7:65536").Delete Shift:=xlUp
    13.     bFound = False
    14.     For Each ws In Worksheets
    15.         If ws.Name <> "Reports" Then
    16.             Set rng = ws.Range("A1").SpecialCells(xlCellTypeLastCell)
    17.             lLastRow = rng.Row
    18.            
    19.             For x = 2 To lLastRow 'First two rows contain headers etc
    20.                 If ws.Cells(x, "E").Value = "" And ws.Cells(x, "A").Value <> "" And ws.Cells(x, "E").Interior.ColorIndex = xlNone Then
    21.                     If bFound = False Then
    22.                         bFound = True
    23.                         With sht_Rpt.Cells(FindBottomRow(sht_Rpt, 1), "A")
    24.                             .Font.Bold = True
    25.                             .Value = ws.Name
    26.                         End With
    27.                     End If
    28.                     iNextRow = FindBottomRow(sht_Rpt, 1)
    29.                     sht_Rpt.Range("A" & iNextRow, "D" & iNextRow).Value = ws.Range("A" & x, "D" & x).Value
    30.                     sht_Rpt.Range("E" & iNextRow).Value = ws.Range("G" & x).Value
    31.                 End If
    32.             Next x
    33.         End If
    34.         bFound = False
    35.     Next
    36. End Sub
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: excel - selecting sheets dependant on date in sheet name

    Is your combo on the sheet or on a userform? You can use the Select Case block to evaluate the selection and act accordingly using the date funtions.
    VB Code:
    1. Private Sub ComboBox1_Change()
    2.     Select Case ComboBox1.Text
    3.         Case "All Months"
    4.             'Blah
    5.         Case "Previous Month"
    6.             Sheets(MonthName(Month(DateAdd("m", -1, Date)))).Activate
    7.             'Blah, Blah...
    8.         Case "Next Month"
    9.             Sheets(MonthName(Month(DateAdd("m", 1, Date)))).Activate
    10.             'Blah, Blah, Blah...
    11.     End Select
    12. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Re: excel - selecting sheets dependant on date in sheet name

    Thanks for the reply.

    Combobox is on a sheet.

    I dont want it to activate a single sheet as such like your example. Say the user decided to select "Previous Months" and the current month is October.

    So the code would run through all the sheets named "January" to "September" and pull the data from them, copying it to the Reports sheet.

    Im thinking that maybe in the combobox_change code i have an array and fill it with all the names of the sheets that need to be cycled for the current filter. Then when the combobox is changed the array is wiped and refilled.

    Seems a really longwinded way though. There must be something better.
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  4. #4

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Re: excel - selecting sheets dependant on date in sheet name

    Ok, well this is what I've come up with so far. This DOES work, but if you can see where I can improve this code please say. I think the loops definately need some work

    VB Code:
    1. Option Explicit
    2. Private sMonth(12) As String
    3.  
    4. Private Sub cmbFilter_Change()
    5. Dim i As Integer
    6.  
    7. Erase sMonth()
    8. Select Case cmbFilter.Text
    9.     Case "Previous Months"
    10.         For i = 1 To Month(Now) - 1
    11.             sMonth(i - 1) = MonthName(i)
    12.         Next
    13.     Case "Previous & Current Months"
    14.         For i = 1 To Month(Now)
    15.             sMonth(i - 1) = MonthName(i)
    16.         Next
    17.     Case "Future Months"
    18.         For i = Month(Now) + 1 To 12
    19.             sMonth(i - 1) = MonthName(i)
    20.         Next
    21.     Case "Current Month"
    22.         sMonth(0) = MonthName(Month(Now))
    23.     Case Else ' All Months
    24.         For i = 1 To 12
    25.             sMonth(i - 1) = MonthName(i)
    26.         Next
    27. End Select
    28.  
    29. Display_Data
    30.  
    31. End Sub
    32.  
    33. Private Sub worksheet_activate()
    34.     cmbFilter.Value = "All Months" ' Reset to all months, ensures up-to-date data
    35. End Sub
    36.  
    37. Private Sub Display_Data()
    38. Dim rng As Range
    39. Dim lLastRow As Integer
    40. Dim iNextRow As Integer
    41. Dim x As Integer
    42. Dim i As Integer
    43. Dim bFound As Boolean
    44.  
    45. Dim ws As Worksheet
    46. Dim sht_Rpt As Worksheet  'Set a handle for the Reports Sheet
    47. Set sht_Rpt = Sheets("Reports")
    48.  
    49. On Error Resume Next
    50.     sht_Rpt.Rows("7:65536").Delete Shift:=xlUp
    51.     bFound = False
    52.    
    53.     For i = 0 To UBound(sMonth)
    54.         If Len(sMonth(i)) > 0 Then
    55.             Set ws = Sheets(sMonth(i))
    56.             Set rng = ws.Range("A1").SpecialCells(xlCellTypeLastCell)
    57.             lLastRow = rng.Row
    58.            
    59.             For x = 2 To lLastRow 'First two rows contain headers etc
    60.                 If ws.Cells(x, "E").Value = "" And ws.Cells(x, "A").Value <> "" And ws.Cells(x, "E").Interior.ColorIndex = xlNone Then
    61.                     If bFound = False Then
    62.                         bFound = True
    63.                         With sht_Rpt.Cells(FindBottomRow(sht_Rpt, 1), "A")
    64.                             .Font.Bold = True
    65.                             .Value = ws.Name
    66.                         End With
    67.                     End If
    68.                     iNextRow = FindBottomRow(sht_Rpt, 1)
    69.                     sht_Rpt.Range("A" & iNextRow, "D" & iNextRow).Value = ws.Range("A" & x, "D" & x).Value
    70.                     sht_Rpt.Range("E" & iNextRow).Value = ws.Range("G" & x).Value
    71.                 End If
    72.             Next x
    73.         End If
    74.         bFound = False
    75.     Next i
    76. End Sub

    This is a funny thing, this line works fine on its own in the worksheet_activate() code, but when i moved it to the display_data() sub it breaks with this error "Delete method of Range class failed". I think this must have something to do with the combobox or something, but i put the error handler above it and the problem goes away.
    VB Code:
    1. sht_Rpt.Rows("7:65536").Delete Shift:=xlUp

    Any thoughts please?
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: excel - selecting sheets dependant on date in sheet name

    Well I just gave you the logic as a simple example but Lee created a nice example. What happens when your on Janurary and you select previous month? Is it supposed to go to December of the previous year?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Re: excel - selecting sheets dependant on date in sheet name

    Quote Originally Posted by RobDog888
    Well I just gave you the logic as a simple example but Lee created a nice example. What happens when your on Janurary and you select previous month? Is it supposed to go to December of the previous year?
    That would be me.. who asked the question :P

    Anyway, at this stage no it shouldnt switch between years as the workbook only contains sheets for a single year, next year will be in a different sheet, though i'll have to link them at some point i spose.

    At the moment, if i click on previous when in january it just displays the blank page with the headers which is fine.

    I appreciate your input, cheers.

    lee
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: excel - selecting sheets dependant on date in sheet name

    My mistake! Well you could check what the current Active sheet is and if its Janurary then dont go back. Same logic for when your on December and select Next Months.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250

    Re: excel - selecting sheets dependant on date in sheet name

    Quote Originally Posted by RobDog888
    My mistake! Well you could check what the current Active sheet is and if its Janurary then dont go back. Same logic for when your on December and select Next Months.
    No worries. That won't work for this instance as Active Sheet will always be "Reports" wont it? Thats where the combobox is.

    It doesn't seem to be throwing out any errors as it is now, do i really need to add more checks to that part?

    Have made a change to the loops which has increased speed slightly.
    VB Code:
    1. [b]iNextRow = FindBottomRow(sht_Rpt, 1) ' call this only once now[/b]
    2.     For i = 0 To UBound(sMonth)
    3.         If Len(sMonth(i)) > 0 Then
    4.             Set ws = Sheets(sMonth(i))
    5.             Set rng = ws.Range("A1").SpecialCells(xlCellTypeLastCell)
    6.             lLastRow = rng.Row
    7.            
    8.             For x = 2 To lLastRow 'First two rows contain headers etc
    9.                 If ws.Cells(x, "E").Value = "" And ws.Cells(x, "A").Value <> "" And ws.Cells(x, "E").Interior.ColorIndex = xlNone Then
    10.                     If bFound = False Then
    11.                         bFound = True
    12.                         [b]With sht_Rpt.Cells(iNextRow, "A")[/b]
    13.                             .Font.Bold = True
    14.                             .Value = ws.Name
    15.                         End With
    16.                         [b]iNextRow = iNextRow + 1 ' increase it as we go so we dont have to keep checking the function[/b]
    17.                     End If
    18.                     sht_Rpt.Range("A" & iNextRow, "D" & iNextRow).Value = ws.Range("A" & x, "D" & x).Value
    19.                     sht_Rpt.Range("E" & iNextRow).Value = ws.Range("G" & x).Value
    20.                     [b]iNextRow = iNextRow + 1[/b]
    21.                 End If
    22.             Next x
    23.         End If
    24.         bFound = False
    25.     Next i
    Last edited by Lee_S; Oct 18th, 2005 at 07:36 PM.
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

Posting Permissions

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



Click Here to Expand Forum to Full Width