Public Function ExcelToJSON(rng As Range) As String
    ' Check there must be at least two columns in the Excel file
    If rng.Columns.Count < 2 Then
        ExcelToJSON = CVErr(xlErrNA)
        Exit Function
    End If
    
    Dim dataLoop, headerLoop As Long
    
    ' Get the first row of the Excel file as a header
    Dim headerRange As Range: Set headerRange = Range(rng.Rows(1).Address)
    
    ' Count the number of columns of targeted Excel file
    Dim colCount As Long: colCount = headerRange.Columns.Count
    Dim json As String: json = "["
    
    For dataLoop = 1 To rng.Rows.Count
        ' Skip the first row of the Excel file because it is used as header
        If dataLoop > 1 Then
            ' Start data row
            Dim jsonData As String: jsonData = "{"
            
            ' Loop through each column and combine with the header
            For headerLoop = 1 To colCount
                jsonData = jsonData & """" & headerRange.Value2(1, headerLoop) & """" & ":"
                jsonData = jsonData & """" & rng.Value2(dataLoop, headerLoop) & """"
                jsonData = jsonData & ","
            Next headerLoop
            
            ' Strip out the comma in last value of each row
            jsonData = Left(jsonData, Len(jsonData) - 1)
            
            ' End data row
            json = json & jsonData & "},"
        End If
    Next
    
    ' Strip out the last comma in last row of the Excel data
    json = Left(json, Len(json) - 1)
    json = json & "]"
    
    ExcelToJSON = json
End Function

