loop through *CPU.txt files in directory

bertjeiv

New Member
Joined
Sep 23, 2010
Messages
32
Hello all

I have written a script that imports a *.txt files to excel and then retrieves needed information from that.

now i have to loop through certain files (*CPU.txt) in a directory.

I cannot find useable tutorials, the ones i tried failed on compilation...

this is my importation block:
Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;d:\Diagnose_ARC56_CPU.txt" _
        , Destination:=Range("A1"))
        .Name = "Diagnose_ARC12_CPU_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
Thanks
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
thank you while it works as its supposed to but its not what i need, this stitches all files together and then imports it. I need to import each file on another worksheet.

I need some code that takes the directory path and and filename and pastes it to the ActiveSheet.QueryTables.Add part of the code
 
Upvote 0
I think I found a solution:
Code:
[B]Sub RunCodeOnAllXLSFiles()[/B] 
Dim lCount As Long Dim wbResults As Workbook 
Dim wbCodeBook As Workbook   
Application.ScreenUpdating = False 
Application.DisplayAlerts = False 
Application.EnableEvents = False  
On Error Resume Next     
Set wbCodeBook = ThisWorkbook         
With Application.FileSearch             
.NewSearch             [COLOR=#008000]
'Change path to suit[/COLOR]             
.LookIn = "C:\MyDocuments\TestResults"             
.FileType = msoFileTypeExcelWorkbooks             
[COLOR=#008000]'Optional filter with wildcard[/COLOR]             
'.Filename = "Book*.xls"                 
If .Execute > 0 Then 
[COLOR=#008000]'Workbooks in folder[/COLOR]                     
For lCount = 1 To .FoundFiles.Count 
[COLOR=#008000]'Loop through all[/COLOR]                         
[COLOR=#008000]'Open Workbook x and Set a Workbook variable to it[/COLOR]                         
Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)                                                  
[COLOR=#008000]'DO YOUR CODE HERE[/COLOR]                          
wbResults.Close SaveChanges:=False                     
Next lCount                 
End If         
End With 
On Error GoTo 0 
Application.ScreenUpdating = True 
Application.DisplayAlerts = True 
Application.EnableEvents = True 
[B]End Sub[/B]
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,256
Members
452,901
Latest member
LisaGo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top