I am importing data from three different text file imports. I had been doing this with manual selection of the files with no issues, but wanted to automate the process further and set up code to import the newest file in each of the target folders. However, while I can run any one of these three imports independently, when I try to run them back to back, I get a Run-Time Error 1004 - Excel cannot find the text file to refresh this external data range on the second of three imports - on the .Refresh BackgroundQuery:=False. I would say this is because no data is actually added to the workbook on the second import, but cannot figure out why the first one imports and the second one does not. The first one works fine - regardless of which order I call these subs in. Any help would be greatly appreciated. Here are the three subs:
VBA Code:
Sub AutoImportHF()
Dim TargetFile As String
ChDir HFDir
If RunY = vbYes Then
MyPath = HFDir
MyFile = Dir(MyPath & "*.txt", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
fName = LatestFile
TargetFile = HFDir & LatestFile
Else
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
TargetFile = fName
End If
Sheets("HF Import").Activate
Range("A2:Z500").ClearContents
Range("A2").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TargetFile, Destination:=Range("$A$2"))
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
If Range("A2").Value = 1 Then MsgBox ("There's an error in the HF Import. Please delete row 2 on the HF import and correct the formula in cell " & _
"A2 on the Booking Pace Export page. You'll then need to manually copy and paste the booking pace export into the report.")
End Sub
VBA Code:
Sub AutoImportSegmentation()
Dim TargetFile As String
ChDir DSDir
If RunY = vbYes Then
MyPath = DSDir
MyFile = Dir(MyPath & "*.txt", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
fName = LatestFile
TargetFile = DSDir & LatestFile
Else
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
TargetFile = fName
End If
Sheets("Segment Import").Activate
Range("A1:Z500").ClearContents
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TargetFile, Destination:=Range("$A$1"))
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
VBA Code:
Sub AutoImportDailyClosing()
Dim TargetFile As String
ChDir DCDir
If RunY = vbYes Then
MyPath = DCDir
MyFile = Dir(MyPath & "*.txt", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
MsgBox "No files were found...", vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
fName = LatestFile
TargetFile = DCDir & LatestFile
Else
fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
TargetFile = fName
End If
Sheets("Daily Closing Import").Activate
Range("A1:Z500").ClearContents
Range("A1").Select
With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & TargetFile, Destination:=Range("$A$1"))
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub