Search : in
By :

How to specify macro for CURRENT active sheet

Last answer on Aug 19, 2009 2:58:35 pm BST seo5906, on May 22, 2009 9:42:41 pm BST 
 Report this message to moderators

Hello,
this is a very basic question, I am sorry to ask. I have created a macro in excel that I want to run in the current worksheet that is selected. How do I do this? For example, I have a sortZip Macro that I want to run on several worksheets when I hit the RUN button but it only works on the worksheet that I originally created the macro on.

I would really appreciate your advice on this!!! - Please :D

Code is:
Sub SortZip()
'
' SortZip Macro
'

'
ActiveWorkbook.Worksheets("GilbertApril09").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("GilbertApril09").Sort.SortFields.Add Key:=Range( _
"H2:H390"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("GilbertApril09").Sort.SortFields.Add Key:=Range( _
"L2:L390"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("GilbertApril09").Sort
.SetRange Range("A1:L390")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Configuration: Windows Vista Internet Explorer 7.0

Best answers for « How to specify macro for CURRENT active sheet » in :
VBA: Finding Hdc in an Excel worksheet or UserForm ShowVBA: Finding Hdc in an Excel worksheet or UserForm Here are two small examples on how to find Hdc in a worksheet: By clicking on Sheet1 the UserForm is displayed. Put the pointer on UF, hold the left mouse button down and drag the...
[VBA] Detecting changes in cell Show[VBA] Detecting changes in cell The Event Change feature of a sheet will detects the change in the active cell but it gives no information about the content. The example given below will help you to find out if the cell was changed,...
Excel tips : How to insert date in a cell ShowExcel tips : How to insert date in a cell Below are some tips on how to insert date and time in an excel cell for a specific purpose:- To insert current date, press CTRL + ; in the chosen cell. To insert current time, press CTRL+...
Spreadsheets - Cell Selection ShowCell Selection Spreadsheets are powerful tools for working with data. However, to work with data, it is necessary to have tools to rapidly choose the required cells. Line Selection An entire line can be chosen by clicking directly on the line...
Linux - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence the name "shell". The shell therefore acts as an intermediary between the operating system and the user thanks to command lines...
UNIX system - The shell ShowIntroduction to the shell The command interpreter is the interface between the user and the operating system, hence its name "shell". The shell therefore acts as an intermediary between the operating system and the user using command lines...

1

aquarelle, on May 22, 2009 9:46:26 pm BST
  • +1

Hi,
Insert a module, cut and past your macro in it.
Best regards "Pour trouver une solution à ses problèmes, il faut s'en donner la peine."

Reply to aquarelle

2

seo5906, on May 22, 2009 10:53:01 pm BST
  • +1

Thanks for your reply but this doesn't help me to run the macro on ANY future sheets. I don't want to copy the module each time, if that's what you are implying. The names of the worksheets will change frequently so I want to be able to click on any worksheet and run the Macro. Thanks again.

Reply to seo5906

5

aquarelle, on Jul 13, 2009 12:14:17 pm BST
  • +1

Hi,
You have to put the macro into the personal.xls Workbook after that this wil run on any workbooks and worksheets.
Best regards "Pour trouver une solution à ses problèmes, il faut s'en donner la peine."

Reply to aquarelle

3

Max, on Jun 26, 2009 2:00:23 pm BST
  • +1

Maybe ActiveSheet.Name

Reply to Max

4

Fish, on Jul 13, 2009 11:56:53 am BST
  • +3

There's no need for the .name part, just ActiveSheet works fine in Excel 2007.

Reply to Fish

6

 Doc Immortal, on Aug 19, 2009 2:58:35 pm BST
  • +3

One thing I noticed is you may have to have ActiveSheet proceed each method.

As an example, here's a short macro that saves the data from each cell in row H into a word document.
The macro is saved in an empty Excel file and it works for any Excel file loaded (assuming the empty Excel file with the macro is also loaded).

Without ActiveSheet preceeding things like Range("H" & i).Copy, the macro will try to do stuff with the Empty Excel file that has the macro.

Sub saveToWord

    Dim i As Integer
    Dim finalRow as Integer
    Dim appWD As Object
    Dim myString as String
    Dim numberOne as String
    Dim numberTwo as Strng
    Dim myFilename As String

    ' Create a new instance of Word & make it visible
    Set appWD = CreateObject("Word.Application")
    appWD.Visible = True

    'Find the last row with data in the database
    finalRow = ActiveSheet.Range("A9999").End(xlUp).Row
    For i = 2 To finalRow
        myString = ActiveSheet.Range("a" & i).Value

       ' these two cells are both alphanumeric cells; first two characters are always numbers
       numberOne = Left(ActiveSheet.Range("b" & i).Value, 2) 
       numberTwo = Left(ActiveSheet.Range("c" & i).Value, 2) 

       ' Build the unique filename. 
       ' In my Excel file these three elements are sufficient to make the filename unique
       myFilename = myString & " " & numberOne & " " & numberTwo

       ' We want to put the data from cell H into a word doc
       ActiveSheet.Range("H" & i).Copy
    
       ' Tell Word to create a new document
       appWD.Documents.Add
       ' Tell Word to paste the contents of the clipboard into the new document
       appWD.Selection.Paste
       ' Save the new document with the filename and close the word document
       appWD.ActiveDocument.SaveAs filename:= myFilename
       appWD.ActiveDocument.Close
       
    Next i

    ' Close the Word application
    appWD.Quit
End Sub

Reply to Doc Immortal