I'm trying to pull some data from a .csv
file on a network drive into a worksheet in a macro-enabled workbook on a different network drive using a VBScript, but I'm getting the following error:
"Unknown runtime error."
The goal is to copy all the data from the data
worksheet in the data
workbook to the test1
worksheet in the spreadsheet
workbook. Here's my code:
Set app1 = CreateObject("Excel.Application")
Set wb1 = app1.WorkBooks.Open("Q:\sample_dir\spreadsheet.xlsm")
Set ws1 = app1.ActiveWorkBook.WorkSheets("test1")
Set app2 = CreateObject("Excel.Application")
Set wb2 = app2.WorkBooks.Open("M:\sample_dir\data.csv")
Set ws2 = app2.ActiveWorkBook.WorkSheets("data")
i = 1
Do Until ws2.cells(i,1).value = "" 'Find how many rows there are
i = i + 1
Loop
For j = 1 to 5000 'Find how many columns there are
If ws2.cells(1,j).value = "" Then
Exit For
End If
Next
For k = 1 to i
For m = 1 to j
ws1.cells(k,m).value = ws2.cells(k,m).value
Next
Next
app1.ActiveWorkBook.Save
wb1.Close
wb2.Close
app1.Quit
app2.Quit
WScript.Quit
I had originally used a For
loop where the Do Until
exists (and it didn't have the error), but I looked up the looping in VBScript and saw this as an opportunity to clean up and simplify the code.
EDIT: The error says as follows:
excel.vbs(10,1) Microsoft VBScript runtime error: Unknown runtime error
SOLUTION:
I looked into the directory that held my .vbs script and found that a copy of the spreadsheet.xlsm
file had been saved there. I deleted that spreadsheet and ran the code again, and it worked just fine (didn't save it to the script directory and it updated the spreadsheet on the network drive).
This issue came back up again and my first solution didn't actually work. I found out the cause was due to my indexing when continuing to debug. In my code, I had used i = j = 1
to initialize the variables for the two Do Until
loops, which apparently doesn't work here (curse you, C background!)