Run Macro From Drop Down List

BettyBoop0916

New Member
Joined
May 20, 2011
Messages
16
Hi there,

I have a small problem. I am trying to have a macro run from a drop down list that I created through data validation.

The macro I am trying to run is as follows:

Sub GasAndOil()
ActiveSheet.Unprotect
Rows(ActiveCell.Row & ":" & ActiveCell.Row + 5).Select
Selection.Delete
Worksheets("Data").Visible = True
Sheets("Data").Select
Sheets("Data").Range("C9:I15").Select
Selection.Copy
Sheets(Index).Select
Selection.Insert
Worksheets("Data").Visible = False
ActiveSheet.Protect
End Sub

Now...I want this macro to run when I select "(5675) Gas & Oil - Regina" from a drop down list. This drop down list can be in any row in column A...it really just depends on where I put my cursor.

Does anyone have any ideas on how I can have it call this macro?

Thanks!
 

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.
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Value = "(5675) Gas & Oil - Regina" Then
    Application.EnableEvents = False
    Call GasAndOil
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0
Thanks so much! It works perfectly - the only issue I'm having now is that every time I insert a new row using a button I've created, it says "Type Mismatch"

This is the macro I've assigned to the button:

Private Sub CommandButton2_Click()
ActiveSheet.Unprotect
Worksheets("Data").Visible = True
Sheets("Data").Select
Sheets("Data").Range("C1:I6").Select
Selection.Copy
Sheets(Index).Select
Selection.Insert Shift:=xlDown
Worksheets("Data").Visible = False
ActiveSheet.Protect
End Sub

And it gets hung up on the following:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Value = "(5675) Gas & Oil - Regina" Then
Application.EnableEvents = False
Call GasAndOil
Application.EnableEvents = True
End If
End Sub

Any suggestions?
 
Upvote 0
Try

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 And Target.Value = "(5675) Gas & Oil - Regina" Then
Application.EnableEvents = False
Call GasAndOil
Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,176
Members
452,893
Latest member
denay

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