View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
violet violet is offline
external usenet poster
 
Posts: 52
Default applying modules to mulitple worksheet

Bob, for your i change the name to my sheet name already. somehow the module
still only apply to the sheet that is active and no tthe sheet that i have
defined.

Public Sub test_values_Master()
Dim wkb As Workbook
Dim wks As Worksheet

Set wkb = ActiveWorkbook
For Each wks In wkb.Worksheets(Array("China", "Malaysia"))
Call changes
Next wks
End Sub

as for Bill, after the sheet is selected, then what to do next. where should
i call the sub in to apply the macro to the selected sheet. sorry for so many
problems as i really just started vba these few days, then my supervisor want
me do these.

"Bill Pfister" wrote:

Violet, to carry Bob's example further, I've included examples where you can
either explicitly state which sheets to include (sub
PrintSelectSheets_FixedSet) or you can use selective criteria to determine
which sheets to select (sub PrintSelectSheets_Variable looks to see if the
value in cell "A1" of each sheet is true).



Public Sub PrintSelectSheets_FixedSet()
Dim wkb As Workbook
Dim strSheets() As String

Set wkb = ThisWorkbook

' Put all the sheets to print into the strSheets array
ReDim strSheets(0 To 1) As String

strSheets(0) = "a a"
strSheets(1) = "c c"

wkb.Sheets(strSheets).Select
End Sub



Public Sub PrintSelectSheets_Variable()
Dim wkb As Workbook
Dim strSheets() As String
Dim lngFound As Long
Dim i As Long

Set wkb = ThisWorkbook
lngFound = -1

' Put all the sheets to print into the strSheets array
ReDim strSheets(0 To wkb.Sheets.Count - 1) As String

For i = 0 To wkb.Sheets.Count - 1
If (wkb.Sheets(i + 1).Range("A1").Value = True) Then
lngFound = lngFound + 1
strSheets(lngFound) = wkb.Sheets(i + 1).Name
End If
Next i

ReDim Preserve strSheets(0 To lngFound) As String

wkb.Sheets(strSheets).Select

End Sub


"Bob Phillips" wrote:

You have to change the values in the array to your sheet names

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"violet" wrote in message
...
strange..it doesn't work...

"Bob Phillips" wrote:

Public Sub test_values_Master()
Dim wkb As Workbook
Dim wks As Worksheet

Set wkb = ActiveWorkbook ' or set wkb equal to any workbook you want

For Each wks In wkb.Worksheets(Array("Sheet2", "Sheet3"))
Call test_values(wks)
Next wks
End Sub

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"violet" wrote in message
...
Bill,

The code you have for me is for all the worksheets in the

workbook?what if
i
wan only selected few?

"Bill Pfister" wrote:


Violet, you want to pass the worksheet(s) as a parameter into your

sub.
Then you can call the sub from another routine and pass in the
worksheets.
Here's an example:


public sub test_values_Master( )
dim wkb as workbook
dim wks as worksheet

set wkb = activeworkbook ' or set wkb equal to any workbook you

want

for each wks in wkb.worksheets
call test_values( wks )
next wks
end sub


public sub test_values( wks as worksheet )
' perform operations on wks
end sub

Hope this helps - let me know if it wasn't what you were looking

for.

Regards,
Bill


"violet" wrote:

i have written a sub test_values in modules named Identifychages.

I
know that
by runing the module it will perform the sub on the excel

worksheet
that i am
opening. however, i wish to run this sub on mulitiple worksheets

on
the same
workbook.how can i do it?anyone can give me advice? i still very

new
to vba
coding in excel.