[英]How to run the VBA Excel to JSON for multiple reports?
我有一個Excel宏,可以為多個客戶生成每月報告。 我從別人的問題( 在VBA中將Excel表轉換為json )中得到了此代碼,但該代碼僅運行1次,每次生成1個同名文件。 我想將Excel工作表轉換為JSON,以便可以將其上傳到Azure Blob。
Sub export_in_json_format()
Dim fs As Object
Dim jsonfile
Dim rangetoexport As Range
Dim rowcounter As Long
Dim columncounter As Long
Dim linedata As String
' change range here
Set rangetoexport = Sheet2.Range("a2:b6")
Set fs = CreateObject("Scripting.FileSystemObject")
' change dir here
Set jsonfile = fs.CreateTextFile("C:\Users\Satwant\Desktop\" & "MonthlyReport.json", True)
linedata = "{""Output"": ["
jsonfile.WriteLine linedata
For rowcounter = 2 To rangetoexport.Rows.Count
linedata = ""
For columncounter = 1 To rangetoexport.Columns.Count
linedata = linedata & """" & rangetoexport.Cells(1, columncounter) & """" & ":" & """" & rangetoexport.Cells(rowcounter, columncounter) & """" & ","
Next
linedata = Left(linedata, Len(linedata) - 1)
If rowcounter = rangetoexport.Rows.Count Then
linedata = "{" & linedata & "}"
Else
linedata = "{" & linedata & "},"
End If
jsonfile.WriteLine linedata
Next
linedata = "]}"
jsonfile.WriteLine linedata
jsonfile.Close
Set fs = Nothing
End Sub
有沒有辦法針對具有唯一名稱的多個客戶端多次運行它?
更換線
Set jsonfile = fs.CreateTextFile("C:\Users\Satwant\Desktop\" & "MonthlyReport.json", True)
同
Dim s as string
s = inputbox("Enter Name you want to use")
Set jsonfile = fs.CreateTextFile("C:\Users\Satwant\Desktop\" & s & ".json", True)
讓它每次詢問您的文件名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.