private macro won't work.....

cmefly

Well-known Member
Joined
May 13, 2003
Messages
683
any idea why this won't work...

i've got a drop-down menu (A1) and in B1 i have a vlookup formula that can equal one of three result (x, y, or z).

i'm trying to hide and unhide rows based on the value in b1.

any ideas?


Private Sub worksheet_change()

Dim therange As Range
Dim i As Integer

therange = Range("w22:ab104")


If Application.VLookup(Range("b20").Value, therange, 4, 0) = "x" Then
Rows("22:123").Select
Selection.EntireRow.Hidden = False

Rows("37:123").Select
Selection.EntireRow.Hidden = True
End If


If Application.VLookup(Range("b20").Value, therange, 4, 0) = "y" Then
Rows("22:123").Select
Selection.EntireRow.Hidden = False

Rows("22:37").Select
Selection.EntireRow.Hidden = True

Rows("97:123").Select
Selection.EntireRow.Hidden = True
End If

If Application.VLookup(Range("b20").Value, therange, 4, 0) = "z" Then
Rows("22:123").Select
Selection.EntireRow.Hidden = False

Rows("22:96").Select
Selection.EntireRow.Hidden = True
End If

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Object variables require Set:

Code:
[B][COLOR=red]Set[/COLOR][/B] therange = Range("w22:ab104")
 
Upvote 0
The sub heading for the change event of a worksheet looks like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
End Sub
You seem to be missing a bit from yours.:)
 
Upvote 0
i'm getting a compile error....is worksheet_change the correct one to use? the macro is stopping on that line....
 
Upvote 0
Is the compile error something like this:

'Procedure declaration does not match....'

If it is that's because you are using the incorrect heading for the worksheet change event.

Whether you need to use the change event is kind of another matter.

Do you want the code to trigger when a change is made on the worksheet?

Is that change the result of a formula?
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,289
Members
452,902
Latest member
Knuddeluff

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