首页 > 解决方案 > VBS - 如何将工作表从一个工作簿复制到另一个工作簿?

问题描述

我正在尝试将 .csv 复制到现有工作簿中的新工作表中。我已经从文档和类似问题中复制了代码,但我遇到了 Expected Statement 800A0400错误。


    Set xl  = CreateObject("Excel.Application")
    xl.Visible = True
    Set wb_report = xl.Workbooks.Open("path.xlsm")
    Set old_raw_ws = wb_report.Sheets("Raw Export")
    xl.DisplayAlerts = False
    old_raw_ws.Delete
    xl.DisplayAlerts = True
    
    Const vbFormatStandard = 1
    Const vbFormatText     = 2
    Const vbFormatDate     = 4
    
    Const xlDelimited      = 1
    Const xlDoubleQuote    = 1
    
    ' change according to number/type of the fields in your CSV
    dataTypes = Array( Array(1, vbFormatText) _
      , Array(2, vbFormatStandard) _
      , Array(3, vbFormatText) _
      , Array(4, vbFormatDate) _
      )
    
    xl.Workbooks.OpenText "path2.csv", , , xlDelimited, xlDoubleQuote, False, False, True, , , , dataTypes
    Set wb_source = xl.ActiveWorkbook
    wb_source.Sheets(1).Copy Before:=wb_report.Sheets(1) ' this line generates the error
    
    WScript.Sleep(5000)
    wb_report.Close
    wb_source.Close
    xl.Quit

标签: excelcsvvbscriptcopyworksheet

解决方案


推荐阅读