VBA help -- hide columns based upon drop down

dmj120

Active Member
Joined
Jan 5, 2010
Messages
308
Office Version
  1. 365
  2. 2019
  3. 2010
I tried recording a macro [twice] but selecting the 'option' in A1 (drop down: All, Pricing, Review) doesn't seem to get me anything.... :confused:
VBA Code:
Sub Macro1()
'
' Macro2 Macro
'

'
    Columns("A:AN").Select
    Selection.EntireColumn.Hidden = False
    Range("C:D,Q:AC").Select
    Range("Q1").Activate
    Selection.EntireColumn.Hidden = True
End Sub

I am trying to dynamically hide/show columns based upon the selection is A1. Something like:
IF A1 = "All" then show all columns
OR IF A1 = "Pricing" show cols A:B, F:J, AND HIDE cols N:P, AF:AN
OR IF A1 = "Review" show cols A,C, F:J, AND HIDE cols B, N,P etc.....
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
like this ??

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
    
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
    
    
    If Range("A1") = "All" Then
            RangeAll.Hidden = False
        
        ElseIf Range("A1") = "Pricing" Then
            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True
    
        ElseIf Range("A1") = "Review" Then
            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True
    
        Else
            RangeAll.Hidden = False
    End If

End Sub
 
Upvote 0
like this ??

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
   
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
   
   
    If Range("A1") = "All" Then
            RangeAll.Hidden = False
       
        ElseIf Range("A1") = "Pricing" Then
            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True
   
        ElseIf Range("A1") = "Review" Then
            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True
   
        Else
            RangeAll.Hidden = False
    End If

End Sub

I copied that into a module, but nothing happens when I change cell A1. I've only assigned a macro to an image - since it's referencing a specific cell, should it work just by copy/pasting, or is there something else I have to do?
 
Upvote 0
I copied that into a module, but nothing happens when I change cell A1. I've only assigned a macro to an image - since it's referencing a specific cell, should it work just by copy/pasting, or is there something else I have to do?
Can you provide a picture/table of your excel form?
 
Upvote 0
Can you provide a picture/table of your excel form?

A small snippet..
1720661826972.png
 
Upvote 0
try

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
    Dim targetcell As Range
    
    Set targetcell = Range("A1").Value
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
    

    If targetcell = "All" Then

            RangeAll.Hidden = False

        ElseIf targetcell = "Pricing" Then

            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True

        ElseIf targetcell = "Review" Then

            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True

        Else

            RangeAll.Hidden = False

    End If

End Sub
 
Upvote 0
try

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
    Dim targetcell As Range
   
    Set targetcell = Range("A1").Value
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
   

    If targetcell = "All" Then

            RangeAll.Hidden = False

        ElseIf targetcell = "Pricing" Then

            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True

        ElseIf targetcell = "Review" Then

            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True

        Else

            RangeAll.Hidden = False

    End If

End Sub
I get an error
1720662665857.png
 
Upvote 0
sorry I forgot to move it

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
    Dim targetcell As Range
    
    Set targetcell = Range("A1")
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
    

    If targetcell.Value = "All" Then

            RangeAll.Hidden = False

        ElseIf targetcell.Value = "Pricing" Then

            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True

        ElseIf targetcell.Value = "Review" Then

            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True

        Else

            RangeAll.Hidden = False

    End If

End Sub
 
Upvote 0
sorry I forgot to move it

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
    Dim targetcell As Range
   
    Set targetcell = Range("A1")
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
   

    If targetcell.Value = "All" Then

            RangeAll.Hidden = False

        ElseIf targetcell.Value = "Pricing" Then

            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True

        ElseIf targetcell.Value = "Review" Then

            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True

        Else

            RangeAll.Hidden = False

    End If

End Sub

Hmm.... I updated the targetcell to A1 -- A11 was in the IF ELSEIF - still 424 error

1720663351545.png
 
Upvote 0
sorry I forgot to move it

VBA Code:
Sub Macro1()
Application.ScreenUpdating = False

    Dim RangeAll As Range, RangePricing As Range, RangeReview As Range
    Dim targetcell As Range
   
    Set targetcell = Range("A1")
    Set RangeAll = Columns.EntireColumn
    Set RangePricing = Union(Range("N:P"), Range("AF:AN"))
    Set RangeReview = Union(Range("B:B"), Range("N:N"), Range("P:P"))
   

    If targetcell.Value = "All" Then

            RangeAll.Hidden = False

        ElseIf targetcell.Value = "Pricing" Then

            RangeAll.Hidden = False
            RangePricing.EntireColumn.Hidden = True

        ElseIf targetcell.Value = "Review" Then

            RangeAll.Hidden = False
            RangeReview.EntireColumn.Hidden = True

        Else

            RangeAll.Hidden = False

    End If

End Sub
sorry to use this one please @dmj120
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,148
Members
452,615
Latest member
bogeys2birdies

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