dancingcab
New Member
- Joined
- Nov 10, 2014
- Messages
- 14
Hi all,
I have a large number of text files containing five tab delimited columns and several hundred rows. I currently have a macro that opens the text file, finds and calculates the required 'results' then pastes these into a new summary sheet. It's currently taking around 1 second per file, which seems quite slow to me.
I'm wondering if there is a quicker way to read the data from the text files and export the required 'results' without having to open each text file in excel? I'm new to VBA so any advice is greatly appreciated.
This is my code:
Sub Results()
Call GetFolder
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
Application.StatusBar = "Calculating CA Results....."
Call CreateSummary 'This creates the summary sheet with all the correct titles etc
NRow = 5
sFile = Dir(sPath & Application.PathSeparator & "*(1).txt")
'Do While sFile <> ""
Do While Len(sFile) > 0
Workbooks.OpenText FileName:=sPath & "\" & sFile _
, DataType:=xlDelimited, Tab:=True
myFile = ActiveWorkbook.Name
Call CAAnalysis 'This calculates the required results - see below
Set DestRange = Sheet1.Range("B" & NRow)
NRow = NRow + DestRange.Rows.Count
sFile = Dir()
Loop
The code for CAAnalaysis is:
Sub CAAnalysis()
Dim Start1 As Integer
Dim End1 As Integer
Dim AveCurrent1 As Single
Start1 = [MATCH(0.05, ROUND(A:A,5),0)]
End1 = [MATCH(0.15, ROUND(A:A,5),0)]
AveCurrent1 = WorksheetFunction.Average(Range(Cells(Start1, "D"), Cells(End1, "D"))) * 1000000000
ActiveWorkbook.Close False
Windows("Results.xlsx").Activate
Range("A" & NRow).Value = myFile
Range("B" & NRow) = AveCurrent1
End Sub
Thanks!
I have a large number of text files containing five tab delimited columns and several hundred rows. I currently have a macro that opens the text file, finds and calculates the required 'results' then pastes these into a new summary sheet. It's currently taking around 1 second per file, which seems quite slow to me.
I'm wondering if there is a quicker way to read the data from the text files and export the required 'results' without having to open each text file in excel? I'm new to VBA so any advice is greatly appreciated.
This is my code:
Sub Results()
Call GetFolder
Application.ScreenUpdating = False
Application.DisplayStatusBar = True
Application.StatusBar = "Calculating CA Results....."
Call CreateSummary 'This creates the summary sheet with all the correct titles etc
NRow = 5
sFile = Dir(sPath & Application.PathSeparator & "*(1).txt")
'Do While sFile <> ""
Do While Len(sFile) > 0
Workbooks.OpenText FileName:=sPath & "\" & sFile _
, DataType:=xlDelimited, Tab:=True
myFile = ActiveWorkbook.Name
Call CAAnalysis 'This calculates the required results - see below
Set DestRange = Sheet1.Range("B" & NRow)
NRow = NRow + DestRange.Rows.Count
sFile = Dir()
Loop
The code for CAAnalaysis is:
Sub CAAnalysis()
Dim Start1 As Integer
Dim End1 As Integer
Dim AveCurrent1 As Single
Start1 = [MATCH(0.05, ROUND(A:A,5),0)]
End1 = [MATCH(0.15, ROUND(A:A,5),0)]
AveCurrent1 = WorksheetFunction.Average(Range(Cells(Start1, "D"), Cells(End1, "D"))) * 1000000000
ActiveWorkbook.Close False
Windows("Results.xlsx").Activate
Range("A" & NRow).Value = myFile
Range("B" & NRow) = AveCurrent1
End Sub
Thanks!