automatic macro

  • Thread starter Thread starter Ewing25
  • Start date Start date
E

Ewing25

How do I make it so my code runs everytime I click on a certain worksheet.

This is my code so far.

Sub TripsCopy()
Dim Trips As Integer
Dim tr As Integer

Active.Sheet (Expense)
Trips = InputBox("Enter Number of Trips Taken")
If Trips > 1 Then
'Sheets("Individual Trip").Select
For tr = 1 To Trips
Sheets("Individual Trip").Copy After:=Sheets(Sheets.Count)
Next
End If
End Sub
 
In the worksheet code area include this event macro:

Private Sub Worksheet_Activate()
Call TripsCopy
End Sub



Because it is worksheet code, it is very easy to install and use:

1. right-click the tab name near the bottom of the window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you save the workbook, the macro will be saved with it.

To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm
 
Back
Top