VBA宏将CSV文件与命令行复制功能

时间:2015-09-10 23:36:43

标签: excel-vba csv vba excel

我编写了一个宏,它可以使用命令shell组合所选文件夹中的文件。我使用命令提示符的原因是文件很大并且需要一些时间才能打开。

问题是,当我选择包含空格的文件夹(即J:\ Network Accrual \ Invoices)时,宏会失败。我试图遵循其他建议,包括4个引语(""""& path&"""&# 34;),但这似乎不适用于我设置的操作 - " cmd.exe / C copy J:\ TEST * csv J:\ TEST \ Combined.csv"。

以下是代码:

Private Sub COMBINE_FILES()

Dim FldrPicker As FileDialog
Dim myPath As String

'Retrieve Target Folder Path From User
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
  .Title = "Select A Target Folder"
  .AllowMultiSelect = False
    If .Show <> -1 Then GoTo NextCode
    myPath = .SelectedItems(1) & "\"
End With

'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings

'Display path
MsgBox myPath

 'Combine files in Command Line
  Dim RET As Variant
  RET = Shell("cmd.exe /C copy  " & myPath & "*csv " & myPath & "Combine.csv", 0)

ResetSettings:
'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

尝试

RET = Shell("cmd.exe /C copy  """ & myPath & "*csv"" """ & myPath & "Combine.csv""", 0)