Iterate though sheets in external workbook

Posted on 2011-02-17
Last Modified: 2012-06-27
Trying to run a for each loop on an external workbook with variable number of sheets.

for each wkSht in Workbooks(startFolder).Worksheets

Open in new window

Not sure why it won't work. startFolder is my absolute path to the workbook, as in C:/book.xls

Still new to VBA and I know this is simple. Please help!

Question by:keats88
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 30

Accepted Solution

SiddharthRout earned 125 total points
ID: 34919737
Try this

Sub Sample()
    Dim wb As Workbook
    Dim wkSht As Worksheet
    Set wb = Workbooks.Open("C:\MyFile.xls")
    For Each wkSht In wb.Worksheets
      MsgBox wkSht.Name
End Sub

Open in new window


Author Comment

ID: 34920032
That works, but is there a way to do this without having to actually open the workbook?

LVL 30

Assisted Solution

SiddharthRout earned 125 total points
ID: 34920048
No that I know of... AFAIK, You have to open the workbook.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question