0
Sub AddCsvDataToExcel()
'Ask User for File's Name
      filename = "E:\Surendra\Excel_Testing\NewTemplate\C_0_1\accelVsTime.csv"
      'Check for no entry
      'If filename = "" Then End
      'Get Next Available File Handle Number
      FileNum = FreeFile()
      'Open Text File For Input
      Open filename For Binary As #FileNum

      'Set The Counter to 1
      Counter = 1
      'Loop Until the End Of File Is Reached
      Do While Seek(FileNum) <= LOF(FileNum)
         'Display Importing Row Number On Status Bar
          'Store One Line Of Text From File To Variable
          Line Input #FileNum, ResultStr

          'Store Variable Data Into Active Cell
    With oExcelReadWorkSheet2
          splitValues = Split(ResultStr, ",")
          .Cells(Counter, 1) = Replace(splitValues(0), Chr(34), "")
          .Cells(Counter, 2) = Replace(splitValues(1), Chr(34), "")
          .Cells(Counter, 3) = Replace(splitValues(2), Chr(34), "")
          .Cells(Counter, 4) = Replace(splitValues(3), Chr(34), "")
        End With
          'Cells(Counter + 5, 5) = Replace(splitValues(4), Chr(34), "")
          'Cells(Counter + 5, 6) = Replace(splitValues(5), Chr(34), "")
          Counter = Counter + 1
      'Start Again At Top Of 'Do While' Statement
      Loop
      'Close The Open Text File
      Close
      'Remove Message From Status Bar
End Sub

oExcelReadWorkSheet2是我要复制数据的工作表对象。当我直接从 excel 运行此代码时,它正在工作。但是当我将它保存为 VBS 文件并运行时,我收到以下错误:

compilation error: `Expected end of statement at "Open filename For Binary As #FileNum"`

有人知道我如何解决这个错误吗?

4

1 回答 1

0

VBA 不是 VBScript。而不是 VBA 的 Open,您必须使用FileSystemObject的 .OpenTextFile、.ReadLine 和 .Close 方法。

于 2013-03-21T09:59:13.247 回答