How do I write a code that will loop through a directory structure?
I have the following problem: in a directory C:\Documents\Means\ I have directories C:\Documents\Means\Measurement 2\ to C:\Documents\Means\Measurement 120\ each of these directories consists of .txt files. In each folder Measurement 2 to Measurement 120 there is one DBI.txt file which has only two rows. I need to loop through directory structure and import all DBI.txt files one after the other.
So far I managed to create this code:
Sub DBI()
Dim SourceWorkBook As Excel.Workbook
Dim DestWorkBook As Excel.Workbook
Dim wks As Worksheet
Set DestWorkBook = ThisWorkbook
DestWorkBook.Sheets.Add
Sheets("Sheet2").Name = "DBI"
Folder = "C:\Documents\Measurement 2\"
Set DestSht = DestWorkBook.Sheets("DBI")
'DestSht.Cells.Clear
FName = Dir(Folder & "DBI.txt")
Do While FName <> ""
Set Bk = Workbooks.Open(Filename:=Folder & FName)
' For Each Sht In Bk.Sheets
For a_counter = 2 To 120
j = 2 * a_counter
DestSht.Range("B" & j).Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents\Measurement 2\DBI.txt" _
, Destination:=Range("B" & j))
.Name = "DBI"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ":"
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next a_counter
End Sub
How do I write in the loop procedure to go through all folders Measurement 2 to Measurement 120 and import it in sheet DBI in column B in even rows (B2:B3, B4:B5, B6:B7 etc.)?
I have the following problem: in a directory C:\Documents\Means\ I have directories C:\Documents\Means\Measurement 2\ to C:\Documents\Means\Measurement 120\ each of these directories consists of .txt files. In each folder Measurement 2 to Measurement 120 there is one DBI.txt file which has only two rows. I need to loop through directory structure and import all DBI.txt files one after the other.
So far I managed to create this code:
Sub DBI()
Dim SourceWorkBook As Excel.Workbook
Dim DestWorkBook As Excel.Workbook
Dim wks As Worksheet
Set DestWorkBook = ThisWorkbook
DestWorkBook.Sheets.Add
Sheets("Sheet2").Name = "DBI"
Folder = "C:\Documents\Measurement 2\"
Set DestSht = DestWorkBook.Sheets("DBI")
'DestSht.Cells.Clear
FName = Dir(Folder & "DBI.txt")
Do While FName <> ""
Set Bk = Workbooks.Open(Filename:=Folder & FName)
' For Each Sht In Bk.Sheets
For a_counter = 2 To 120
j = 2 * a_counter
DestSht.Range("B" & j).Select
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;C:\Documents\Measurement 2\DBI.txt" _
, Destination:=Range("B" & j))
.Name = "DBI"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = ":"
.TextFileColumnDataTypes = Array(1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
Next a_counter
End Sub
How do I write in the loop procedure to go through all folders Measurement 2 to Measurement 120 and import it in sheet DBI in column B in even rows (B2:B3, B4:B5, B6:B7 etc.)?