使用 zoho sheet 的 vba 脚本导致的 "unknown error" 是什么
what is the "unknown error" caused by vba script using zoho sheet
我得到的 "Encountered error at line 32 : unknown error" 是什么(第 32 行是 "With ws" 的那一行)我该如何补救?它没有像我希望的那样将所有数据粘贴到新 sheet 的第二行和下面。
Option Explicit
Sub updateCSV()
Dim wb As Workbook
Dim ws As Worksheet
Dim newWS As Worksheet
Dim i As Long
Dim z As Long
Dim lr As Long
Dim desc As Variant, created As Variant, amt As Variant, fee As Variant
'Set Objects
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
Set newWS = Sheets.Add
'Vars -> Assumes headers are in row 1
desc = Application.Match("Customer Description", ws.Rows(1), 0)
created = Application.Match("Created (UTC)", ws.Rows(1), 0)
amt = Application.Match("Amount", ws.Rows(1), 0)
fee = Application.Match("Fee", ws.Rows(1), 0)
z = 1
'Format newWS
With newWS
.Cells(1, 1).Value = "description"
.Cells(1, 2).Value = "date"
.Cells(1, 3).Value = "Amount"
End With
'Get data from ws
With ws
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
'Assumes data starts at row 2 and headers are on row 1
For i = 2 To lr
z = z + 1
newWS.Cells(z, 1).Value = ws.Cells(i, desc).Value
newWS.Cells(z, 2).Value = Format(ws.Cells(i, created).Value, "mm/dd/yyyy")
newWS.Cells(z, 3).Value = ws.Cells(i, amt).Value
Next i
'Add values back
For i = 2 To lr
z = z + 1
newWS.Cells(z, 1).Value = "stripe fee: " & ws.Cells(i, desc).Value
newWS.Cells(z, 2).Value = Format(ws.Cells(i, created).Value, "mm/dd/yyyy")
newWS.Cells(z, 3).Value = ws.Cells(i, fee).Value * -1
Next i
End With
'Fit
newWS.Columns.AutoFit
End Sub
通过将第 10 行的 "Variant" 替换为 "Long",代码将正确执行。
原第10行
Dim desc As Variant, created As Variant, amt As Variant, fee As Variant
长:
Dim desc As Long, created As Long, amt As Long, fee As Long
我得到的 "Encountered error at line 32 : unknown error" 是什么(第 32 行是 "With ws" 的那一行)我该如何补救?它没有像我希望的那样将所有数据粘贴到新 sheet 的第二行和下面。
Option Explicit
Sub updateCSV()
Dim wb As Workbook
Dim ws As Worksheet
Dim newWS As Worksheet
Dim i As Long
Dim z As Long
Dim lr As Long
Dim desc As Variant, created As Variant, amt As Variant, fee As Variant
'Set Objects
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
Set newWS = Sheets.Add
'Vars -> Assumes headers are in row 1
desc = Application.Match("Customer Description", ws.Rows(1), 0)
created = Application.Match("Created (UTC)", ws.Rows(1), 0)
amt = Application.Match("Amount", ws.Rows(1), 0)
fee = Application.Match("Fee", ws.Rows(1), 0)
z = 1
'Format newWS
With newWS
.Cells(1, 1).Value = "description"
.Cells(1, 2).Value = "date"
.Cells(1, 3).Value = "Amount"
End With
'Get data from ws
With ws
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
'Assumes data starts at row 2 and headers are on row 1
For i = 2 To lr
z = z + 1
newWS.Cells(z, 1).Value = ws.Cells(i, desc).Value
newWS.Cells(z, 2).Value = Format(ws.Cells(i, created).Value, "mm/dd/yyyy")
newWS.Cells(z, 3).Value = ws.Cells(i, amt).Value
Next i
'Add values back
For i = 2 To lr
z = z + 1
newWS.Cells(z, 1).Value = "stripe fee: " & ws.Cells(i, desc).Value
newWS.Cells(z, 2).Value = Format(ws.Cells(i, created).Value, "mm/dd/yyyy")
newWS.Cells(z, 3).Value = ws.Cells(i, fee).Value * -1
Next i
End With
'Fit
newWS.Columns.AutoFit
End Sub
通过将第 10 行的 "Variant" 替换为 "Long",代码将正确执行。
原第10行
Dim desc As Variant, created As Variant, amt As Variant, fee As Variant
长:
Dim desc As Long, created As Long, amt As Long, fee As Long