Drop down list activates a macro

marin820

New Member
Joined
May 11, 2007
Messages
13
Hi everyone,

This is a good, fairly straightforward one: I have a drop down list with 3 choices on a spreadsheet (form that people fill out). I'm trying to write a macro where, choice 1 keeps the form as is; choice 2 hides rows 20-30 and unhides rows 30-40; choice 3 hides rows 20-30 and unhides rows 40-50.

Thanks for all your help.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

davidhall80

Well-known Member
Joined
Jul 8, 2006
Messages
663
Stick that in the module for your chosen sheet


Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

If Range("A1") = "choice 2" Then
Rows("20:30").EntireRow.Hidden = True
Rows("30:40").EntireRow.Hidden = False
End If

If Range("A1") = "choice 3" Then
Rows("20:30").EntireRow.Hidden = False
Rows("30:40").EntireRow.Hidden = True
End If


End Sub
 
Upvote 0

marin820

New Member
Joined
May 11, 2007
Messages
13
The problem with the above code is that I'm using a data validation list in cell A1, and you cant use the Worksheet_Change() because data validation lists don't trigger events.

Does anyone know a way around this - should I try Worksheet_Calculate() or something else instead?
 
Upvote 0

davidhall80

Well-known Member
Joined
Jul 8, 2006
Messages
663
I would say to use a Combo Box, but you probably want to keep the formatting of the data validation cells....right??
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
marin280

What version of Excel are you on?

If it's 2000 or later then data validation will trigger the change event.
 
Upvote 0

marin820

New Member
Joined
May 11, 2007
Messages
13
I'm using Excel 2003...I tried it again and entered the code in Object, not Module and this seems to work, but only on Choice 2; when I pick Choice 3 the form doesn't change or hide the respective rows.

Also, is it possible to toggle back and forth between options on the drop down menu and have the form change accordingly, or is this only possible with a combo box?
 
Upvote 0

davidhall80

Well-known Member
Joined
Jul 8, 2006
Messages
663
I went to sheet1---> View code---> and pasted that code in there and the validation box did trigger the change. I also noticed that the spelling in "Choice" was Case Sensitive...Just make sure that all of your characters & spaces are identical between the validation list & the VB code.
 
Upvote 0

Forum statistics

Threads
1,191,709
Messages
5,988,236
Members
440,139
Latest member
ngaicuong2017

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
Top