VBA code error

Nickiemay

Board Regular
Joined
Oct 31, 2013
Messages
52
I am attempting to hide columns based on the selection made in a drop down list in B1. There are five options, four of which would result in certain columns hiding. "Select" hides all relevant columns, "Whites", "Colors" and "Grays" hide certain columns, and "Various" hides no columns. Changes apply to this worksheet only. Below is the code I have entered in my VBA. All Macros are enabled. What is wrong with it?!

Private Sub Worksheet_Change (ByVal Target As Range)

If Target.Address = "$B$1" Then
If UCase(Target) = "Colors" Then
Columns("E:L").EntireColumn.Hidden = True
Else
Columns("E:L").EntireColumn.Hidden = False
End If
End If


End Sub


Private Sub Worksheet_Change1 (ByVal Target As Range)

If Target.Address = "$B$1" Then
If UCase(Target) = "Grays" Then
Columns("E:H").EntireColumn.Hidden = True
Columns("M:P").EntireColumn.Hidden = True
Else
Columns("E:H").EntireColumn.Hidden = False
Columns("M:P").EntireColumn.Hidden = False
End If
End If


End Sub


Private Sub Worksheet_Change2 (ByVal Target As Range)

If Target.Address = "$B$1" Then
If UCase(Target) = "Whites" Then
Columns("I:P").EntireColumn.Hidden = True
Else
Columns("I:P").EntireColumn.Hidden = False
End If
End If


End Sub


Private Sub Worksheet_Change3 (ByVal Target As Range)

If Target.Address = "$B$1" Then
If UCase(Target) = "Select" Then
Columns("E:P").EntireColumn.Hidden = True
Else
Columns("E:P").EntireColumn.Hidden = False
End If
End If


End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You can only have one Worksheet_Change event procedure so all your code needs to go in there. And UCase(Target) can never be "Colors" because that's proper case. You need to test for "COLORS".
 
Upvote 0
You cannot just make up your own event procedures (your Change1, Change2, etc.)... you must use the ones provided by VB. To do what you want, you need to handle all of the possibilities within VB's Change event. I think this will do what your posted code suggests you want...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$1" Then
    Columns.Hidden = False
    If UCase(Target) = "COLORS" Then
      Columns("E:L").EntireColumn.Hidden = True
    ElseIf UCase(Target) = "GRAYS" Then
      Columns("E:H").EntireColumn.Hidden = True
      Columns("M:P").EntireColumn.Hidden = True
    ElseIf UCase(Target) = "WHITES" Then
      Columns("I:P").EntireColumn.Hidden = True
    ElseIf UCase(Target) = "SELECT" Then
      Columns("E:P").EntireColumn.Hidden = True
    End If
  End If
End Sub
 
Upvote 0
*You all beat me to the punch!

Hi,
Give this a try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Set KeyCells = Range("B1")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
           Is Nothing Then

    Columns("E:P").EntireColumn.Hidden = False
    Select Case Target.Value
        Case "Select"
            Columns("E:P").EntireColumn.Hidden = True
        Case "Colors"
            Columns("E:L").EntireColumn.Hidden = True
        Case "Grays"
            Columns("E:H").EntireColumn.Hidden = True
            Columns("M:P").EntireColumn.Hidden = True
        Case "Whites"
            Columns("I:P").EntireColumn.Hidden = True
        End Select
End If
End Sub

Your code was looking for all caps (UCase) when the values in the dropdown were not in all caps. Thus, it didn't match and probably wasn't executing.
 
Upvote 0
You cannot just make up your own event procedures (your Change1, Change2, etc.)... you must use the ones provided by VB. To do what you want, you need to handle all of the possibilities within VB's Change event. I think this will do what your posted code suggests you want...

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$B$1" Then
    Columns.Hidden = False
    If UCase(Target) = "COLORS" Then
      Columns("E:L").EntireColumn.Hidden = True
    ElseIf UCase(Target) = "GRAYS" Then
      Columns("E:H").EntireColumn.Hidden = True
      Columns("M:P").EntireColumn.Hidden = True
    ElseIf UCase(Target) = "WHITES" Then
      Columns("I:P").EntireColumn.Hidden = True
    ElseIf UCase(Target) = "SELECT" Then
      Columns("E:P").EntireColumn.Hidden = True
    End If
  End If
End Sub

Rick, I consider myself honored to have come up with something so similar to your code! I got my if statement from a M$ KB. Is there any value to all the "If Not ... Is Nothing" stuff?
 
Upvote 0
Rick, I consider myself honored to have come up with something so similar to your code! I got my if statement from a M$ KB. Is there any value to all the "If Not ... Is Nothing" stuff?
The OP is testing only a single cell for the change, so it is simpler to test if the address of the target cell is that single cell address. When there are more cells to test for changing, then the Intersect method is handier than AND'ing together multiple individual address tests. I like the Select Case structure you proposed... my first thought was to use it in my response, but then decided the If..ElseIf structure was not all that bad for what the OP is doing and I was afraid the OP might not be familiar with Select Case and wanted to avoid a repetitive set of teaching posts trying to introduce him/her to it.
 
Upvote 0

Forum statistics

Threads
1,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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