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
Reply to seo5906
|
One thing I noticed is you may have to have ActiveSheet proceed each method.
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 |