Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / New Users / February 2008

Tip: Looking for answers? Try searching our database.

Running the same VBA code for Many Sheets

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gatarossi@ig.com.br - 14 Feb 2008 00:42 GMT
Dear all,

I have a lot of sheets in my workbook and I need to run the same code
in each sheet, but one variable of my VBA code is exactly the name of
the sheet.

I have a sheet with the name of all sheets that I will use:

For example:
      A
1   dog
2   cat
3   fish

Then I'm trying to create a variable with the name of the sheet:

Sub test()

Dim xlsht As Excel.Worksheet
Dim xlsht2 As Excel.Worksheet

prodline = xlsht2.Cells(2, 1)

Set xlsht = Sheets(prodline)

xlsht.Select

End Sub

But it doesn't work!!!!

How can I do it???

Thanks a lot!!!
Dave Peterson - 14 Feb 2008 01:07 GMT
I don't see where you set xlsht2 to anything.

So
prodline = xlsht2.Cells(2, 1)
looks like it cause an error.

> Dear all,
>
[quoted text clipped - 30 lines]
>
> Thanks a lot!!!

Signature

Dave Peterson

Jim Cone - 14 Feb 2008 01:15 GMT
You need to declare prodline as a string...
  Dim prodline as String
You also need to tell Excel what xlsht refers to...
  Set xlsht = worksheets("name of sheet with titles on it")
Then...

Sub AirCode
 Dim prodline as string
 Dim rCell as range
 Dim xlsht As Worksheet
 Set xlsht = Worksheets("Titles Sheet")

 For Each rCell in xlsht.Range("A1:A3)
       prodline = rCell.Value
       Worksheets(prodline).Range("B3").Value = "Mush"
 Next' rCell
End Sub
Signature

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)

<gatarossi@ig.com.br>
wrote in message
Dear all,
I have a lot of sheets in my workbook and I need to run the same code
in each sheet, but one variable of my VBA code is exactly the name of
the sheet.
I have a sheet with the name of all sheets that I will use:

For example:
      A
1   dog
2   cat
3   fish

Then I'm trying to create a variable with the name of the sheet:
Sub test()
Dim xlsht2 As Excel.Worksheet
prodline = xlsht2.Cells(2, 1)
Set xlsht = Sheets(prodline)
xlsht.Select
End Sub

But it doesn't work!!!!
How can I do it???
Thanks a lot!!!

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.