如何为多个报告运行VBA Excel到JSON?

如何为多个报告运行VBA Excel到JSON?,json,vba,excel,Json,Vba,Excel,我有一个Excel宏,可以为多个客户端生成月度报告。我从其他人的提问()中得到了这段代码,但代码只运行了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

我有一个Excel宏,可以为多个客户端生成月度报告。我从其他人的提问()中得到了这段代码,但代码只运行了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)


让它每次都向您询问文件名。

非常感谢您的回复,有没有办法让它在不询问文件名的情况下获取当前工作表的名称?您可以这样向用户建议活动工作表名称:
s=inputbox(“输入您要使用的名称”,默认值:=activesheet.name)
或仅获取工作表名称,请使用此
s=activesheet.name
@KazimierzJawor。非常感谢,我会试试。谢谢你们,我已经知道该怎么做了。
Dim s as string
s = inputbox("Enter Name you want to use")    
Set jsonfile = fs.CreateTextFile("C:\Users\Satwant\Desktop\" & s & ".json", True)