LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Junior Member
 
Posts: 6
Default how do i loop thru all the worksheets in 2 files one by one

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:
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:\test1\beta" '<- change folder path

myDir1 = "P:\test1\prod" '<- 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


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.
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA loop to create named .csv files for all worksheets and workbooks in a folder KQBats Excel Programming 11 May 10th 12 04:47 AM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 5 March 29th 07 05:48 AM
Naming Worksheets - Loop within a loop issue klysell Excel Programming 0 March 27th 07 11:17 PM
(Complex) Loop within loop to create worksheets klysell Excel Programming 1 March 20th 07 12:03 AM
Loop thru files in DIR, delete worksheets & reset worksheet Name property?? Mike Taylor Excel Programming 1 October 24th 03 04:14 AM


All times are GMT +1. The time now is 05:06 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"