Hi, if a code generate something like second picture from the first one, does that meet your needs.


Sub SqlTest()
strConnection = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"User ID=Admin;" & _
"Data Source='" & ThisWorkbook.FullName & "';" & _
"Mode=Read;" & _
"Extended Properties=""Excel 12.0 Macro;"";"
strQuery = _
"TRANSFORM COUNT(*)-100 SELECT [Location] FROM [Sheet1$] GROUP BY [Location] PIVOT QUARTER"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open strConnection
Set objRecordSet = objConnection.Execute(strQuery)
RecordSetToWorksheet ActiveSheet, objRecordSet
objConnection.Close
For Each cell In Range("A1").CurrentRegion.Cells
If cell.Value = -99 Then cell.ClearContents: cell.Interior.ColorIndex = 10
Next
End Sub
Sub RecordSetToWorksheet(objSheet As Worksheet, objRecordSet As Object)
With objSheet
.Cells.Delete
For i = 1 To objRecordSet.Fields.Count
.Cells(1, i).Value = objRecordSet.Fields(i - 1).Name
Next
.Cells(2, 1).CopyFromRecordset objRecordSet
.Cells.Columns.AutoFit
End With
End Sub
Note that VBA is no longer supported. It's recommended to post VBA programming questions to Stack Overflow by using the vba tag, along with any other relevant tags.
Disclaimer: Microsoft provides no assurances and/or warranties, implied or otherwise, and is not responsible for the information you receive from the third-party linked sites or any support related to technology.