Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...I always seem to find what I need in previous threads without starting my own. I have started a couple but that's it..."

Geography

Where in the world do Tek-Tips members come from?

Looping through Excel worksheets with vbaHelpful Member! 

Eprice (TechnicalUser)
13 Jun 12 15:58
Hi,
I have an excel worksheet where they can add new worksheets (2) for each customer. The worksheets will be named according to the cusotmer's name such as Joe-Branch, Joe-State, Lisa-Branch, Lisa-State and so on. I need to create a summary sheet based on what they enter on those sheets so I am going to use a button with vba code to loop though the sheets to get the answers. I don't know how many customers there will be or what the sheet names will be besides the "-Branch" and "-State" part of the name. How do I use a loop to go through the sheets with -Branch in the sheet name to get the answers and store them to a summary sheet's cell. Then I would run the code to loop through each -State sheet to get those totals. I do know how to loop though all sheets in a workbook but not every other one depending on the sheets name.
Thanks
Lisa
Helpful Member!  SkipVought (Programmer)
13 Jun 12 16:17


hi,

Your workbook design has severely complicated your quest.

If ALL your Customer data were in ONE TABLE (sheet), it would be a very simple task, either a lookup formula or an aggregation formula, to obtain a Customer's data. NO VBA REQUIRED! Probably take less than 30 seconds!

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Andrzejek (Programmer)
13 Jun 12 16:28

I agree with Skip, but how about:

Sub Macro1()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Sheets
Select Case Split(ws.Name, "-")(1)
Case "State"
MsgBox "Your sheet for State is named " & ws.Name
Case "Branch"
MsgBox "Your Branch sheet is named " & ws.Name
End Select
Next ws

End Sub

Have fun.

---- Andy

xlbo (MIS)
13 Jun 12 20:34
alternatively

Sub looper()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

If InStr(LCase(ws.Name), "state") > 0 Then

MsgBox ws.Name & " is a state sheet"

ElseIf InStr(LCase(ws.Name), "branch") > 0 Then

MsgBox ws.Name & " is a branch sheet"
Else

'not a branch or a state sheet

End If

Next

End Sub

Andy - a couple of points on your code...

Activeworkbook usage can be prone to errors - better to use THISworkbook reference - it will ALWAYS refer to the workbook that the code is invoked from

Also, you are looping through all sheets - not just worksheets - this will include chart sheets (and if there are any, old xl4 macro sheets) which can cause issues sometimes as they can have different properties. Better to use thisowrkbook.WORKsheets

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244: How to get the best answers before you ask a question

Andrzejek (Programmer)
14 Jun 12 8:18

Thanks xlbo, good to know.

Or how about:
[tt]
Dim i As Integer

With ThisWorkbook
For i = 1 To .Sheets.Count
If InStr(1, .Sheets(i).Name, "-") Then
Select Case Split(.Sheets(i).Name, "-")(1)
Case "State"
MsgBox "Your sheet for State is named " & .Sheets(i).Name
Case "Branch"
MsgBox "Your Branch sheet is named " & .Sheets(i).Name
End Select
Else
MsgBox "This sheet is named " & .Sheets(i).Name
End If
Next i
End With

[/tt]

Have fun.

---- Andy

Eprice (TechnicalUser)
14 Jun 12 12:18
Thanks for all your suggestions. I think I do agree with Skip and when the customer is added to the worksheet I will put all that data into a table and then link to that. It is good to know how to loop through all the worksheets for future use.
Lisa

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close