我想就您的代码寻求帮助,我试图将Excel转换为Json文件,并且遇到错误变量未定义,有人可以告诉我缺少什么,可能是参考或缺少声明?
下面是我的代码:
Sub CreateJSONFile()
Dim jsonItems As New Collection
Dim jsonDictionary As New Dictionary
Dim jsonFileObject As New FileSystemObject
Dim jsonFileExport As TextStream
Dim i As Long
Dim cell As Variant
Dim colNum As Integer
Dim excelRange As Range
Set wsDst = ThisWorkbook.Sheets("Template")
Set excelRange = Cells(1, 1).CurrentRegion
colNum = 2
For i = 2 To excelRange.Rows.Count
If wsDst.Cells(8, colNum) <> "" And wsDst.Cells(7, colNum + 1) = "" Then
jsonDictionary("Object") = wsDst.Cells(8, colNum)
jsonDictionary("Xpath") = wsDst.Cells(9, colNum)
jsonDictionary("height") = wsDst.Cells(10, colNum)
jsonDictionary("background-color") = wsDst.Cells(11, colNum)
jsonDictionary("width") = wsDst.Cells(12, colNum)
jsonDictionary("font-family") = wsDst.Cells(13, colNum)
jsonDictionary("font-size") = wsDst.Cells(14, colNum)
jsonDictionary("font-weight") = wsDst.Cells(15, colNum)
jsonDictionary("font-style") = wsDst.Cells(16, colNum)
jsonDictionary("font-stretch") = wsDst.Cells(17, colNum)
jsonDictionary("line-height") = wsDst.Cells(18, colNum)
jsonDictionary("letter-spacing") = wsDst.Cells(19, colNum)
jsonDictionary("color") = wsDst.Cells(20, colNum)
jsonItems.Add jsonDictionary
Set jsonDictionary = Nothing
End If
colNum = colNum + 1
Next i
MsgBox JsonConverter.ConvertToJson(jsonItems, Whitespace:=3)
Set jsonFileExport = jsonFileObject.CreateTextFile(ThisWorkbook.Sheets("Template").Cells(1, 2).Value & "\" & ThisWorkbook.Sheets("Template").Cells(7, 2).Value & ".json", True)
jsonFileExport.WriteLine (JsonConverter.ConvertToJson(jsonItems, Whitespace:=3))
Set wsDst = Nothing
End Sub