Hi Team,
SQL Query not working for headers which starts with Numbers and backward slashes.
arr = Array("Weekly", "Monthly", "1Yr", "O/W")
Thanks
mg
SQL Query not working for headers which starts with Numbers and backward slashes.
arr = Array("Weekly", "Monthly", "1Yr", "O/W")
VBA Code:
Sub CopyData()
Dim Conn As New ADODB.Connection
Dim Rst As ADODB.Recordset
FilePath = ThisWorkbook.FullName
connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & _
";Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
Conn.Open connstr
Dim hdr As String
'hdr = "State"
Dim cnt As Long
Dim st As String
Dim ct As String
Dim ShtName As String
Dim sh As Worksheet
Set sht = ThisWorkbook.Worksheets("Sheet1")
ShtName = sht.Name
Dim arr As Variant
arr = Array("Weekly", "Monthly", "1Yr", "O/W")
st = "Maharashtra"
ct = "Pune"
For I = LBound(arr) To UBound(arr)
hdr = arr(I)
MsgBox hdr
Sql = "Select Sum(" & hdr & ") from [" & sht.Name & "$] Where (State = """ & st & """ and City = """ & ct & """)"
Set Rst = New ADODB.Recordset
Rst.Open Sql, Conn
cnt = Rst.Fields(0).Value
MsgBox cnt
On Error Resume Next
Rst.Close
Set Rst = Nothing
On Error GoTo 0
Next I
End Sub
State | City | Weekly | Monthly | 1Yr | o/w |
Maharashtra | Pune | 100 | 200 | 200 | 200 |
Maharashtra | Pune | 200 | 400 | 400 | 400 |
Maharashtra | Nagpur | 300 | 600 | 600 | 600 |
Gujarat | Ahmedabad | 400 | 800 | 800 | 800 |
Gujarat | Surat | 500 | 1000 | 1000 | 1000 |
Karnataka | Bangalore | 600 | 1200 | 1200 | 1200 |
Delhi | New Delhi | 800 | 1600 | 1600 | 1600 |
Thanks
mg