• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

how do i loop thru all the worksheets in 2 files one by one

XLbeginer

New Member
Hi,i am new to macro coding,i have to browse thru a directory and sub folders get the names of 2 files,and get the names of sheets from each file one by one.ex:file 1 sheet and file 2 sheet 1 and perform a macro on the 2 sheets and the loop thru the next sheet in both the files.please find the code below:


-----

[pre]
Code:
Sub test()
Dim myDir As String, fn As String, ws As Worksheet
Dim myDir1 As String, fn1 As String, ws1 As Worksheet
Dim ind As Long
Dim wbk As Workbook, wbk1 As Workbook
ind = 1
myDir = "P:test1beta" '<- change folder path
myDir1 = "P:test1prod" '<- change folder path
Do
fn = GetFile(myDir, ind) 'Dir(myDir & "*.xls")
fn1 = GetFile(myDir1, ind) 'Dir(myDir1 & "*.xls")
'Do While fn <> "" And fn1 <> ""
If fn <> "" And fn1 <> "" Then
Set wbk = Nothing
Set wbk = Application.Workbooks.Open(myDir & "" & fn)
Set wbk1 = Nothing
Set wbk1 = Application.Workbooks.Open(myDir1 & "" & fn1)
With wbk 'Workbooks.Open(myDir & "" & fn)
For Each ws In .Sheets
MsgBox "filename is " & fn & " sheet name is " & ws.Name
'Debug.Print "filename is " & fn & " sheet name is " & ws.Name
With wbk1 'Workbooks.Open(myDir1 & "" & fn1)
For Each ws1 In .Sheets
MsgBox "filename is " & fn1 & " sheet name is " & ws1.Name
'Debug.Print "filename is " & fn1 & " sheet name is " & ws1.Name
Next '----> problem here : inner loop is going on
End With
Next
wbk1.Close False
wbk.Close False
End With
End If
'Loop
ind = ind + 1
Loop Until fn = "" And fn1 = ""
End Sub

'procedure to get the file from directory
Function GetFile(filePath As String, fileInd As Long) As String
Dim ind As Long
ChDir filePath
GetFile = Dir(filePath & "*.xlsx")
For ind = 2 To fileInd
GetFile = Dir
Next
End Function
[/pre]
-----


The issue is the 2nd file all the sheet names i was able to get,but the file 1 has the sheet1(file1 sheet1 and file2 sheet1,file2 sheet2,file2 sheet3).i need file1 sheet1,file2 sheet1 and then file1 sheet2 ,file2 sheet2 and so on.please help.


EDITED: Please next time embed posted code or tables within backticks (`) to keep indentation and column alignment.
 
Hi, XLbeginer!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about questions in general...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


About this question in particular...


Maybe you want to give a look at this topic:

http://chandoo.org/forums/topic/excel-files-documenting-inspector


Regards!
 
Hi, XLbeginer!


Not tested but give a look at this modification of your first procedure:

-----

[pre]
Code:
Option Explicit

Sub test()
Dim myDir As String, fn As String, ws As Worksheet
Dim myDir1 As String, fn1 As String, ws1 As Worksheet
Dim ind As Long
Dim wbk As Workbook, wbk1 As Workbook
Dim I As Integer, J As Integer, A As String, B As String
ind = 1
myDir = "P:test1beta" '<- change folder path
myDir1 = "P:test1prod" '<- change folder path
Do
fn = GetFile(myDir, ind) 'Dir(myDir & "*.xls")
fn1 = GetFile(myDir1, ind) 'Dir(myDir1 & "*.xls")
'Do While fn <> "" And fn1 <> ""
If fn <> "" And fn1 <> "" Then
Set wbk = Nothing
Set wbk = Application.Workbooks.Open(myDir & "" & fn)
Set wbk1 = Nothing
Set wbk1 = Application.Workbooks.Open(myDir1 & "" & fn1)
' get max worksheets
If wbk.Worksheets.Count > wbk1.Worksheets.Count Then
J = wbk.Worksheets.Count
Else
J = wbk1.Worksheets.Count
End If
' loop thru worksheets
For I = 1 To J
If I <= wbk.Worksheets.Count Then A = wbk.Worksheets(I).Name Else A = "<don't exist>"
B = "filename is " & fn & " sheet name is " & A
If I <= wbk.Worksheets.Count Then A = wbk1.Worksheets(I).Name Else A = "<don't exist>"
B = B & vbCrLf & "filename is " & fn1 & " sheet name is " & A
MsgBox B
'Debug.Print b
Next
wbk1.Close False
wbk.Close False
End If
'Loop
ind = ind + 1
Loop Until fn = "" And fn1 = ""
End Sub
[/pre]
-----


Regards!
 
Hi, XLbeginer!

Glad you solved it. Thanks for your feedback and welcome back whenever needed or wanted.

Regards!
 
Back
Top