So I'm banging my head against the wall trying to run a macro that generates a new worksheet for every .dbf file in specified directory and then copies ranges of data. The code works great for the first file but then gets hung up on that first .dbf and never advances to the subsequent files. I'm sure its a simple fix, but I'm still relatively green.
Any help would be much appreciated!
Sub LoopFiles()
Dim strDir As String, strFileName As String, strDBFs As String
Dim wbSourceBook As Workbook, wbTargetBook As Workbook
Dim wsTemplate As Worksheet, wsNewSheet As Worksheet
Dim DBFnum As Integer
strDir = "C:\Users\JMack\Desktop\WaterQuality\TempDBFs\"
strFileName = Dir(strDir & "*.dbf")
strSiteName = Left(strFileName, (InStrRev(strFileName, ".", -1, vbTextCompare) - 1))
Set wbTargetBook = ThisWorkbook
Set wsTemplate = wbTargetBook.Sheets(1)
Do While strFileName <> ""
Dim i As Integer
i = 1
''Add new worksheet based on exceedence table template
wsTemplate.Copy after:=wbTargetBook.Worksheets(i)
Set wsNewSheet = wbTargetBook.Sheets(i + 1)
wsNewSheet.Name = "test"
Set wbSourceBook = Workbooks.Open(strDir & strFileName)
''There is more code in the middle but not pertinent to the issue
i = i + 1
strFileName = strDir()
wbSourceBook.Close savechanges:=False
Loop
'Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub
Any help would be much appreciated!
Sub LoopFiles()
Dim strDir As String, strFileName As String, strDBFs As String
Dim wbSourceBook As Workbook, wbTargetBook As Workbook
Dim wsTemplate As Worksheet, wsNewSheet As Worksheet
Dim DBFnum As Integer
strDir = "C:\Users\JMack\Desktop\WaterQuality\TempDBFs\"
strFileName = Dir(strDir & "*.dbf")
strSiteName = Left(strFileName, (InStrRev(strFileName, ".", -1, vbTextCompare) - 1))
Set wbTargetBook = ThisWorkbook
Set wsTemplate = wbTargetBook.Sheets(1)
Do While strFileName <> ""
Dim i As Integer
i = 1
''Add new worksheet based on exceedence table template
wsTemplate.Copy after:=wbTargetBook.Worksheets(i)
Set wsNewSheet = wbTargetBook.Sheets(i + 1)
wsNewSheet.Name = "test"
Set wbSourceBook = Workbooks.Open(strDir & strFileName)
''There is more code in the middle but not pertinent to the issue
i = i + 1
strFileName = strDir()
wbSourceBook.Close savechanges:=False
Loop
'Application.ScreenUpdating = True
'Application.DisplayAlerts = True
End Sub