Sub Macro3()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://..." _
, Destination:=Range("Sheet6!$G$23"))
''// The line above fails with the error:
''// "Run-time error '-2147024809 (80070057)':
''// The destination range is not on the same worksheet
''// that the Query table is being created on."
.Name = _
"?tmp=toolbar_FlvTube_homepage&prt=..."
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
The recorded macro fails as described in the comment.
You recorded the macro while Sheet 6 was active, but are now trying to run it on a different sheet. To run the macro for the current active sheet, simply change the code as follows:
Sub Macro3()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://..." _
, Destination:=ActiveSheet.Range("$G$23"))
...
End With
End Sub
Edit: (in response to comment):
I need to be able to paste the results of the query to a different sheet then the active one since the macro can be run at any time and must be paste to the same location everytime. Perhaps there is a way to change your active sheet with code?
The error happens when the two sheets are different, so if you want the magic to happen on a particular sheet, you should specify that sheet instead of using ActiveSheet. The following code would always place the QueryTable on Sheet6:
Sub Macro3()
With Sheet6.QueryTables.Add(Connection:= _
"URL;http://..." _
, Destination:=Sheet6.Range("$G$23"))
...
End With
End Sub
Related
I had some exceptional requirement. That is i have set of value which will come in the drop down in an Excel cell. While changing the value it will hit the server and fetch data according the drop down selected in the excel.
Finally i have done with some simple steps. Please find it following.
Step 1:- Create your drop down list
Go to Data -> Data validation
Now you have created a drop down in F2 cell as follows:
Now on change we have to create a macro which will fetch data from server:
Step 2 :-
Now you have to click on View Code which will open a code window. There you have to paste the following code.
Private Sub Worksheet_Change(ByVal Target As Range)
'MsgBox "You just changed " & Target.Address
If Target.Address = "$F$2" Then
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;https://www.google.com/", Destination:= _
Range("A1"))
.Name = "MR07"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.WebDisableRedirections = True
.Refresh BackgroundQuery:=False
End With
End If
End Sub
In the above code you can replace your own URL instead of https://www.google.com/
A1 is the starting row of output, which is coming from server. $F$2 is validating the changes coming from drop down. if you not put this condition for all cell changes it will call this function which is not expected.
I am trying to update a Macro that I recorded so that content from the lines in the spreadsheet will update the web query target but have run into a a few errors. I've sorted through several of them but the current issue is a Runtime error. The following is the code for my query:
Sub GetAllPkgInfo()
Sheets("AllPkgs").Select
Range("A2").Select
Do Until ActiveCell.Value = ""
Call PkgInfo1
Loop
End Sub
Sub PkgInfo1()
'
' PkgInfo1 Macro
' Import RHEL package information from RPMFind
'
' Keyboard Shortcut: Ctrl+Shift+D
'
Selection.Copy
Sheets("WebQuery").Select
ActiveSheet.Cells.Clear
Range("$G$1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
With ActiveSheet.QueryTables.Add( _
Connection:="URL;https://rpmfind.net/linux/rpm2html/search.php?query=" & _
Range("$G$1").Value, Destination:=Range("$A$1"))
.CommandType = 0
.Name = "search.php?query=abrt_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
ActiveCell.Offset(1, -5).Range("A1").Select
Selection.Copy
Sheets("AllPkgs").Select
ActiveCell.Offset(0, 1).Range("CombinedPackages[[#Headers],[Column1]]").Select
ActiveSheet.Paste
Sheets("WebQuery").Select
ActiveCell.Cells.Select
Application.CutCopyMode = False
Selection.QueryTable.Delete
Selection.ClearContents
Sheets("AllPkgs").Select
ActiveCell.Offset(1, -1).Range("CombinedPackages[[#Headers],[Column1]]").Select
End Sub
The error message I receive states "Runtime error '1004': Paste Method of Worksheet Class Failed" and highlights the first code instance of
ActiveSheet.Paste
Can anybody help me get straightened out please?
With ActiveSheet.QueryTables.Add( _
Connection:= "URL;https://rpmfind.net/linux/rpm2html/search.php?query=" & _
Range("G1").Value, Destination:=Range("$A$1"))
I have an Excel sheet that I would like to serve as the backup of a Google sheet, which I'll be clearing out periodically to prevent it from slowing down. I'm attempting to write a macro which, after a set period of time, will find the next empty row in the Excel sheet, activate the cell in column "A", and import the data from the Google Sheet. I don't want to "refresh" the data in Excel, because the plan is to delete the data in the Google Sheet every so often while the Excel sheet serves as a continuous record. I would simply like to pull the current Google Sheet data into the first cell of the next empty row, and schedule this to repeat.
Here's what I've been trying:
Sub addData()
newCell = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Address
MsgBox newCell
Sheet1.QueryTables.Add(Connection:= _
"URL;googleSheetURL" _
, Destination:=Range(newCell))
.PostText = "transaction-data_1"
.Name = False
.FieldNames = False
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.HasAutoFormat = False
.RefreshOnFileOpen = 2
.BackgroundQuery = False
.TablesOnlyFromHTML = True
.SaveData = True
.Refresh BackgroundQuery:=False
.UseListObject = False
End With
End Sub
Where googleSheetURL is replaced with the published link of the sheet.
I just keep getting errors, debug mode highlights the Refresh BackgroundQuery line. I disabled background refresh because I didn't want the queries to update once I pulled them. Does anyone have any insight?
This code doesn't compile. You're missing a With in front of Sheet1:
Sub addData()
newCell = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Address
With Sheet1.QueryTables.Add(Connection:= _
"URL;googleSheetURL" _
, Destination:=Range(newCell))
.PostText = "transaction-data_1"
.Name = False
.FieldNames = False
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.HasAutoFormat = False
.RefreshOnFileOpen = 2
.BackgroundQuery = False
.TablesOnlyFromHTML = True
.SaveData = True
.Refresh BackgroundQuery:=False
.UseListObject = False
End With
End Sub
I've recorded a macro to query a database, and when I record the macro, the query runs properly. However, when I try to run the macro again on the same sheet or on a different one, I get the error:
runtime error 1004, "SQL syntax error"
on the line
.Refresh BackgroundQuery:=False".
Below is the recorded macro.
Sub Macro3()
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=Substation Prod;SRVR=SUBP;UID=U326357;", Destination:=Range("A1"))
.CommandText = Array( _
"SELECT FACT_MONTHLY.TIME_STAMP, FACT_MONTHLY.SYSTEM_NUMBER_VAL0, FACT_MONTHLY.SUBSTATION_NUMBER_VAL0, FACT_MONTHLY.MVA_MAX_OUT_VAL0, FACT_MONTHLY.MW_MAX_OUT_VAL0, FACT_MONTHLY.MVAR_MAX_OUT_VAL0, FACT_" _
, _
"MONTHLY.MW_MIN_OUT_VAL0, FACT_MONTHLY.MVAR_MIN_OUT_VAL0, FACT_MONTHLY.PF_MAX_VAL0, FACT_MONTHLY.PF_MIN_VAL0, FACT_MONTHLY.TOP_OIL_TANK_MAX_VAL0, FACT_MONTHLY.LOAD_FACTOR_VAL0, FACT_MONTHLY.PH1_TAP_MAX" _
, _
"_DRAG_VAL0, FACT_MONTHLY.WIND_TEMP_MAX_VAL0, FACT_MONTHLY.LTC_TEMP_MAX_VAL0, FACT_MONTHLY.PH2_TAP_MAX_DRAG_VAL0, FACT_MONTHLY.PH3_TAP_MAX_DRAG_VAL0, FACT_MONTHLY.BOT_OIL_TEMP_MAX_VAL0, FACT_MONTHLY.PH" _
, _
"1_TAP_MIN_DRAG_VAL0, FACT_MONTHLY.PH2_TAP_MIN_DRAG_VAL0, FACT_MONTHLY.PH3_TAP_MIN_DRAG_VAL0, FACT_MONTHLY.PH1_LOAD_MAX_KV_VAL0, FACT_MONTHLY.PH2_LOAD_MAX_KV_VAL0, FACT_MONTHLY.PH3_LOAD_MAX_KV_VAL0, FA")
.Name = "Query from Substation Prod"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
I've searched online for ways to fix this issue, but none of the other solutions I've found are working. Any help would be greatly appreciated.
All the previous lines inside the With statement are setting properties.
the
.Refresh BackgroundQuery := False
is a method call.
The refresh is supposed to refresh the results.
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh
For further assistance try VBA Excel QueryTables.add .Refresh BackgroundQuery Error
I would like to download the daily prices of our stock exchange using the following code in vba. Although the code works i cant seem to be able to get the sheets renamed to the corresponding day when the price list was obtained.
Dim DownloadDay As Date
DownloadDay = #3/4/2014#
Do While DownloadDay < #4/4/2014#
ActiveWorkbook.Worksheets.Add
Call website(Format(DownloadDay, "YYYYMMDD"))
'INCREMENT THE DAY
Sheets.Add.Name = "DownloadDay"
DownloadDay = DownloadDay + 1
Loop
End Sub
Sub website(sDate As String)
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://live.mystocks.co.ke/price_list/" & DownloadDay & "/", Destination:=Range("$A$1"))
.Name = DownloadDay
'To rename each work sheet with the corresponding day'
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
This line of VBA will set the name of your worksheet:
Sheets("Sheet2").Name = "NewName"
You can not use "/" the character in the name though, and the variable is set to a date not a string.
Try this.
.name = CSTR(FORMAT(DownloadDay,"YYYYMMMDD"))