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
To test this code you need to do few things before.
First, insert code in the ThisWorkbook code module.
Second, code suppose that all cells with dates are formated as a date, not a text.
Third, sheet need to look as you show in the post.
Fourth, the dates you insert in the B1 and B2 need to exist in the column 4.
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
A button should work but you can try my code in Post #10. It must be put in the correct corresponding Sheet module. So if your inventory is on Sheet1, insert this code into the Sheet1 module in the Editor. You can get there by double clicking on the correct sheet in the left hand pane of the editor.
 
Upvote 0
@EXCEL MAX - I just tried your code and it does not return a range of columns. It only returns the "Start Date" and the "End Date"
 
Upvote 0
This was to be my suggestion. Contains some possibly useful, data validation?
Should default to a full set of columns if bad / incomplete entries made to B1 or B2

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RngB1 As Range, RngB2 As Range, RngC4 As Range
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B1:B2")) Is Nothing Then Exit Sub

Set RngB1 = Range("B1")
Set RngB2 = Range("B2")
Set RngC4 = Range("C4")

ActiveSheet.UsedRange.Columns.EntireColumn.Hidden = False

LastCol = Cells(4, Columns.Count).End(xlToLeft).Column

If Not IsDate(RngB1) Or Not IsDate(RngB2) Then Exit Sub
If RngB2 < RngB1 Then Exit Sub
If Not RngB1 >= RngC4 Or RngB1 > Cells(4, LastCol) Then Exit Sub
If Not RngB2 >= RngC4 Or RngB2 > Cells(4, LastCol) Then Exit Sub

Application.ScreenUpdating = False
Range(Cells(4, 3), Cells(4, LastCol)).Columns.EntireColumn.Hidden = True

Show1 = RngB1 - RngC4 + 3
Show2 = Show1 + RngB2 - RngB1

Range(Cells(4, Show1), Cells(4, Show2)).EntireColumn.Hidden = False

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Yes, you have right.
It's because I have understand a post in that way.
I just tried you code and saw a difference.
 
Upvote 0
This was to be my suggestion. Contains some possibly useful, data validation?
Should default to a full set of columns if bad / incomplete entries made to B1 or B2

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim RngB1 As Range, RngB2 As Range, RngC4 As Range
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("B1:B2")) Is Nothing Then Exit Sub

Set RngB1 = Range("B1")
Set RngB2 = Range("B2")
Set RngC4 = Range("C4")

ActiveSheet.UsedRange.Columns.EntireColumn.Hidden = False

LastCol = Cells(4, Columns.Count).End(xlToLeft).Column

If Not IsDate(RngB1) Or Not IsDate(RngB2) Then Exit Sub
If RngB2 < RngB1 Then Exit Sub
If Not RngB1 >= RngC4 Or RngB1 > Cells(4, LastCol) Then Exit Sub
If Not RngB2 >= RngC4 Or RngB2 > Cells(4, LastCol) Then Exit Sub

Application.ScreenUpdating = False
Range(Cells(4, 3), Cells(4, LastCol)).Columns.EntireColumn.Hidden = True

Show1 = RngB1 - RngC4 + 3
Show2 = Show1 + RngB2 - RngB1

Range(Cells(4, Show1), Cells(4, Show2)).EntireColumn.Hidden = False

Application.ScreenUpdating = True
End Sub
@ Snakechips - Thank you, Thank you! This code works perfectly!
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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