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"`
このエラーを解決する方法を知っている人はいますか?