Macro for column range

robocop

New Member
Joined
Jan 10, 2015
Messages
18
I have a whole calendar year of inventory data. I want that every time I enter a start & end dates (ex. cells B1 & B2) it ONLY makes viewable the columns of the specified date ranges
while other columns before and after the selected dates are hidden. Is this possible? See example below. I hope it makes sense. Thank you again to all the excels masterminds out there who helps others solves problems.
1629991716219.png
 

Attachments

  • 1629991687348.png
    1629991687348.png
    24.5 KB · Views: 6

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
It is possible, however, it would help if you uploaded your data as an XL2BB as we cannot manipulate and test data in a picture.
 
Upvote 0
You can try this...
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
   
    Dim vCH As Integer, vF1 As Range, vF2 As Range, vC1 As Integer, vC2 As Integer
   
    Application.ScreenUpdating = False
    With ActiveSheet
        For vCH = 3 To .UsedRange.Columns.Count
            Columns(vCH).Hidden = False
        Next vCH
        Set vF1 = Application.Range("C4", Cells(4, _
                    .UsedRange.Columns.Count)).Find([B1], , xlFormulas)
        Set vF2 = Application.Range("C4", Cells(4, _
                    .UsedRange.Columns.Count)).Find([B2], , xlFormulas)
        If Not vF1 Is Nothing And Not vF2 Is Nothing Then
            vC1 = vF1.Column
            vC2 = vF2.Column
            For vCH = 3 To .UsedRange.Columns.Count
                If Not vCH = vC1 And Not vCH = vC2 Then _
                    Columns(vCH).Hidden = True
            Next vCH
        End If
    End With
   
End Sub
 
Upvote 0
How about this:

VBA Code:
Sub ShowColRange()

    Dim sD As Date, eD As Date
    Dim sCol As Range
    Dim sAdd As String, sAdd2 As String
    Dim Da As Long, lcol As Long
    
    sD = Range("B1")
    eD = Range("B2")
    Da = eD - sD
    lcol = Cells(4, Columns.Count).End(xlToLeft).Column
    Range(Cells(4, 3), Cells(4, lcol)).EntireColumn.Hidden = True
    Set sCol = Range(Cells(4, 3), Cells(4, lcol)).Find(what:=sD)
    sAdd = sCol.Address
    sAdd2 = Range(sAdd).Offset(0, Da).Address
    Range(sAdd, sAdd2).EntireColumn.Hidden = False
    
End Sub
 
Upvote 0
How about this:

VBA Code:
Sub ShowColRange()

    Dim sD As Date, eD As Date
    Dim sCol As Range
    Dim sAdd As String, sAdd2 As String
    Dim Da As Long, lcol As Long
   
    sD = Range("B1")
    eD = Range("B2")
    Da = eD - sD
    lcol = Cells(4, Columns.Count).End(xlToLeft).Column
    Range(Cells(4, 3), Cells(4, lcol)).EntireColumn.Hidden = True
    Set sCol = Range(Cells(4, 3), Cells(4, lcol)).Find(what:=sD)
    sAdd = sCol.Address
    sAdd2 = Range(sAdd).Offset(0, Da).Address
    Range(sAdd, sAdd2).EntireColumn.Hidden = False
   
End Sub
Hello igold
Thank you for responding.
This is definitely on the right track. This code is doing what i need however when i do enter new set of start/end dates, it doesnt automatically update untill i run the macro.
I'm i missing anything?
 
Upvote 0
You can try this...
VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  
    Dim vCH As Integer, vF1 As Range, vF2 As Range, vC1 As Integer, vC2 As Integer
  
    Application.ScreenUpdating = False
    With ActiveSheet
        For vCH = 3 To .UsedRange.Columns.Count
            Columns(vCH).Hidden = False
        Next vCH
        Set vF1 = Application.Range("C4", Cells(4, _
                    .UsedRange.Columns.Count)).Find([B1], , xlFormulas)
        Set vF2 = Application.Range("C4", Cells(4, _
                    .UsedRange.Columns.Count)).Find([B2], , xlFormulas)
        If Not vF1 Is Nothing And Not vF2 Is Nothing Then
            vC1 = vF1.Column
            vC2 = vF2.Column
            For vCH = 3 To .UsedRange.Columns.Count
                If Not vCH = vC1 And Not vCH = vC2 Then _
                    Columns(vCH).Hidden = True
            Next vCH
        End If
    End With
  
End Sub
@ Excel Max - thanks for replying. Nothing happens when i run this code.
 
Upvote 0
It is possible, however, it would help if you uploaded your data as an XL2BB as we cannot manipulate and test data in a picture.
@ alansidman - thanks for the tip. I will try to download XL2BB and use when posting moving forward.
 
Upvote 0
No, I thought you would attach it to a Command Button. There is plenty of real estate there. Perhaps in the C1:D2 area. Enter your dates and click.
 
Upvote 0
If you want something automatic you could put this code into the worksheet module that the data exists on. The code will run when you enter the end date...

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Target = Range("B2") Then Exit Sub
    
    Dim sD As Date, eD As Date
    Dim sCol As Range
    Dim sAdd As String, sAdd2 As String
    Dim Da As Long, lcol As Long
    
    sD = Range("B1")
    eD = Range("B2")
    Da = eD - sD
    lcol = Cells(4, Columns.Count).End(xlToLeft).Column
    Range(Cells(4, 3), Cells(4, lcol)).EntireColumn.Hidden = True
    Set sCol = Range(Cells(4, 3), Cells(4, lcol)).Find(what:=sD)
    sAdd = sCol.Address
    sAdd2 = Range(sAdd).Offset(0, Da).Address
    Range(sAdd, sAdd2).EntireColumn.Hidden = False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,368
Messages
6,124,523
Members
449,169
Latest member
mm424

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