VBA to find current month and year - Hide all others

CLAZGRE

New Member
Joined
Dec 19, 2023
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
Good Afternoon All,

I am looking to hide all columns within a range of columns that are not the current month. It worked perfectly when I didn't have my sheet formatted as a table, however it is easier to use my sheet for other purposes formatted as a table, so I took the plunge.

I have attached an image of a section of my table headers.

I was using the below VBA code, but when using it now it is a table, it throws out every single December in my sheet. Is there anyway I can change the code to just have the current month? Also is there a way to code next month instead of current month?



VBA Code:
Sub Create_Backing()
'
' Create_Backing Macro
' Create the backing for approval purposes on a spreadsheet
'
' Keyboard Shortcut: Ctrl+Shift+C
'
    Dim rng As Range
    Dim cell As Range
    
'   Designate which cells to check (all in one row)
    Set rng = Range("$C$5:$DS$5")
    
'   First, unhide all columns
    rng.EntireColumn.Hidden = False
    
'   Loop through cells and hide columns
    For Each cell In rng
'       Check to see if current month/year
        If Format(cell, "mmm-yy") <> Format(Date, "mmm-yy") Then
'           Hide if not equal
            cell.EntireColumn.Hidden = True
        End If
    Next cell
    
' Hide difference and comments
   
   Dim c As Range

    For Each c In Range("A5:DY5").Cells
        If c.Value = "Difference" Then
            c.EntireColumn.Hidden = True
        End If
   
   Next c
   
   Dim d As Range

    For Each d In Range("A5:DY5").Cells
        If d.Value = "Remaining PO Value" Then
            d.EntireColumn.Hidden = True
        End If
   
   Next d
   
   Dim e As Range

    For Each e In Range("A5:DY5").Cells
        If e.Value = "Comments" Then
            e.EntireColumn.Hidden = True
        End If
    
    Next e
   
Dim f As Range

    For Each f In Range("A5:DY5").Cells
        If f.Value = "PO No." Then
            f.EntireColumn.Hidden = True
        End If
    
    Next f
    
' Copy sheet to new book

    ActiveSheet.Copy
    
End Sub

Any help would be greatly appreciated!

Many Thanks & Kind Regards

Clazgre
 

Attachments

  • Screenshot 2023-12-19 145614.png
    Screenshot 2023-12-19 145614.png
    19.7 KB · Views: 12

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Please try the following on a copy of your workbook. Only looking as the months (current and next month options).
VBA Code:
Sub This_Month()
    Application.ScreenUpdating = False
    Dim c As Range, d As Date, LCol As Long
    d = Format(Date, "mmm-yy")
    LCol = Cells(5, Columns.Count).End(xlToLeft).Column
    
    Range("5:5").EntireColumn.Hidden = False
    For Each c In Range(Cells(5, 3), Cells(5, LCol))
        If c <> d Then c.EntireColumn.Hidden = True
    Next c
    Application.ScreenUpdating = True
End Sub

Sub Next_Month()
    Application.ScreenUpdating = False
    Dim c As Range, d As Date, LCol As Long
    d = Format(WorksheetFunction.EDate(Date, 1), "mmm-yy")
    LCol = Cells(5, Columns.Count).End(xlToLeft).Column
    
    Range("5:5").EntireColumn.Hidden = False
    For Each c In Range(Cells(5, 3), Cells(5, LCol))
        If c <> d Then c.EntireColumn.Hidden = True
    Next c
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top