Hi. I'm new to VBA so bear with me.
I'm trying to write VBA that updates a specific cell in Excel with data that I've queried from SQL Server. So far I have the following..
Sub Test1()
Dim qt As QueryTable
sqlstring = "Select ValueX From Data1 Where blah='blah'"
connstring = "My ODBC connection string"
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("D7"), Sql:=sqlstring)
ActiveSheet.QueryTables(1).FieldNames = False
.Refresh
End With
End Sub
The query I'm using returns a single value. The problem I'm having is that any data in cell D7 (and all of column D) gets shoved to the right (to column E) each time the macro has run. I'd like it to just overwrite the cell instead of creating a whole new column "D".
I'm trying to write VBA that updates a specific cell in Excel with data that I've queried from SQL Server. So far I have the following..
Sub Test1()
Dim qt As QueryTable
sqlstring = "Select ValueX From Data1 Where blah='blah'"
connstring = "My ODBC connection string"
With ActiveSheet.QueryTables.Add(Connection:=connstring, Destination:=Range("D7"), Sql:=sqlstring)
ActiveSheet.QueryTables(1).FieldNames = False
.Refresh
End With
End Sub
The query I'm using returns a single value. The problem I'm having is that any data in cell D7 (and all of column D) gets shoved to the right (to column E) each time the macro has run. I'd like it to just overwrite the cell instead of creating a whole new column "D".