The goal is to automate a daily task to open a workbook, import .csv files and perform some formatting, save, and exit excel, the only requirement being the computer is on. I'm using the windows task scheduler to open a macro enabled workbook on a daily basis. With help from this site, I added the argument /p "my specific path" to the task to set the import path I wish to access. Then, using the Workbook_open function, the import is performed, however, the following error occurs while highlighting the .refresh backgroundquery:=false line:

Run-time error '1004':
Excel cannot find the text file to refresh this external data range.
Check to make sure the text file has not been moved or renamed, then try
  the refresh again.

Now, if I select "End" to stop the debugger and simply run the macro on my own, everything works fine. The path is on a network, if that matters. I can post parts of the code if that would help, but it's pretty long. It goes without saying I'm no VBA whiz, so I've pieced the code together,but everything works except the initial import of the .csv file. Thanks in advance for any advice.

Relevant code:

R = 1
FName = Dir(Path & DayTime & ".csv")

Do While FName <> ""
    ImportCsvFile FName, ActiveSheet.Cells(R, 1)
    R = ActiveSheet.UsedRange.Rows.count + 1
    DayTime = DayTime + 1
    FName = Dir(Path & DayTime & ".csv")
Loop

Sub ImportCsvFile(FileName As Variant, Position As Range)
With ActiveSheet.QueryTables.Add(Connection:= _
  "TEXT;" & FileName _
  , Destination:=Position)
  .Name = Replace(FileName, ".csv", "")
  .FieldNames = True
  .RowNumbers = False
  .FillAdjacentFormulas = False
  .RefreshOnFileOpen = False
  .BackgroundQuery = True
  .RefreshStyle = xlInsertDeleteCells
  .SavePassword = False
  .SaveData = True
  .AdjustColumnWidth = True
  .TextFilePromptOnRefresh = False
  .TextFilePlatform = xlMacintosh
  .TextFileStartRow = 1
  .TextFileParseType = xlDelimited
  .TextFileTextQualifier = xlTextQualifierDoubleQuote
  .TextFileConsecutiveDelimiter = False
  .TextFileTabDelimiter = True
  .TextFileSemicolonDelimiter = False
  .TextFileCommaDelimiter = False
  .TextFileSpaceDelimiter = False
  .TextFileOtherDelimiter = ","
  .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
  .Refresh BackgroundQuery:=False
End With
End Sub
有帮助吗?

解决方案

FName = Dir(Path & DayTime & ".csv") returns only the filename or blank if the file does not exist.

Add the path to the parameter and it should work fine. I also prefer using Call and ().

Call ImportCsvFile(Path & FName, ActiveSheet.Cells(R, 1))

Apparently the current directory changed after you started using the debugger. It must have changed from the folder that didn't have your files to the folder that does. That's why it worked when running it the second time. I'm not what caused that.

Also if you would've checked the FileName variable within the ImportCsvFile sub when the error occurred you would have seen that the path was missing.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top