简体   繁体   English

使用VBA的Excel中的默认路径

[英]Default path in Excel using VBA

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. 目标是使日常工作自动化,以打开工作簿,导入.csv文件并执行一些格式化,保存和退出excel的操作,唯一的要求就是打开计算机。 I'm using the windows task scheduler to open a macro enabled workbook on a daily basis. 我每天使用Windows任务计划程序打开启用了宏的工作簿。 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. 在此站点的帮助下,我在任务中添加了参数/ p“我的特定路径”以设置希望访问的导入路径。 Then, using the Workbook_open function, the import is performed, however, the following error occurs while highlighting the .refresh backgroundquery:=false line: 然后,使用Workbook_open函数执行导入,但是,突出显示.refresh backgroundquery:= false行时,发生以下错误:

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. 不用说我不是VBA专家,所以我将代码拼凑在一起,但是除了.csv文件的初始导入之外,其他所有方法都有效。 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. FName = Dir(Path & DayTime & ".csv")仅返回文件名,如果文件不存在,则返回空白。

Add the path to the parameter and it should work fine. 将路径添加到参数,它应该可以正常工作。 I also prefer using Call and (). 我也更喜欢使用Call和()。

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. 同样,如果在发生错误时您已经检查了ImportCsvFileFileName变量,那么您将看到路径丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM