Replacing parts of an URL with value from a column - vba

I'm trying to download datasheets for horses.
The only variable in the macro is a part of the URL.
I have all the variables (the horses numbers) listed in column "A".
I want to create a new sheet with the information collected from the website, and then collect information again using the numbers (new variable) from next row in column "A".
So far this is the code I have:
Sub Makro1()
'
' Makro1 Makro
'
Dim nummer As String
nummer = Sheets("Ark1").Range("A1:A10")
'
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://195.198.34.45/trav/hast/visa/" & nummer & "/resultat", Destination:=Range _
("$A$1"))
.CommandType = 0
.Name = "resultat"
.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 = False
.Refresh BackgroundQuery:=False
End With
Columns("D:D").Select
ActiveWindow.SmallScroll Down:=21
Columns("E:E").Select
Range("E22").Activate
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Columns("D:D").Select
Range("D22").Activate
Selection.TextToColumns Destination:=Range("D1"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=True, Semicolon _
:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="/", _
FieldInfo:=Array(1, 2), TrailingMinusNumbers:=True
End Sub
User sheet trying to describe problem

a proposition on how to handle multiple query, not sure about what you want to do with each result
Sub Makro1()
'
' Makro1 Makro
'
Set ws = ActiveSheet 'sheet containing your numbers in column A
ActiveWorkbook.Worksheets.Add
For Each nummer In ws.Range("A1:A10")
If nummer.Text = "" Then Exit For
For Each qt In ActiveSheet.QueryTables
qt.Delete
Next
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://195.198.34.45/trav/hast/visa/" & nummer.Text & "/resultat", Destination:=Range("$A$1"))
.Name = "resultat"
.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 = False
.Refresh BackgroundQuery:=False
nummer.Offset(0, 1) = ActiveSheet.Cells(21, 1)
End With
Next
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
End Sub

Related

I've ran into a syntax error in vba code for an Excel web query based on a table

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"))

VBA error 13 : Type mismatch

I am new to VBA.
I am trying to extract all columns from other file but the result is displayed with error 13. How can I solve it?
Sub generate_data()
Application.CutCopyMode = False
Sheets("Output").Activate
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=HKGHKGT_UBET;Description=HKGHKGT_UBET;UID=SiuH;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=HKHKDNO304NB;DATABASE=" _
), Array("UBet")), Destination:=Range("$A$1")).QueryTable
'CommandType = 0
.CommandText = Array("select * from UBet..cathy_fin where duns_no in(" & Sheets("Main").Range("A2").Value & ") order by stmt_date desc")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "HKGHKGT_UBET"
.Refresh BackgroundQuery:=False
End With
ActiveSheet.ListObjects("HKGHKGT_UBET").TableStyle = ""
Columns("B:B").Select
Selection.NumberFormat = "m/d/yyyy"
End Sub

VBA: printing on the last row of excel

I am planning to extract data from a table on the web. The code below was mostly from built-in web scraping function in Excel.
What I want is to find the last row with data and print on the first blank row.
Sub Sub1()
ActiveSheet.Cells.Clear
Dim lastRow As Long
Dim i As Integer
For i = 1 To 2
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.aastocks.com/tc/stocks/quote/symbolsearch.aspx?page=" & i & " &order=symbol&seq=asc", Destination _
:=Range("A,lastRow")) '???? I got an error here, what I want is to detect the last row and print on the first blank row.???
'.CommandType = 0
.Name = "symbolsearch_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 = "10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print lastRow
Next i
End Sub
This seems to work ok now. You were close, you should be able to edit as required.
Sub Sub1()
ActiveSheet.Cells.Clear
Dim lastRow As Long
Dim i As Integer
Dim sTicker As String
'set the first row to write to
lastRow = 2
For i = 1 To 2
sTicker = InputBox("Enter the code to search for")
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.aastocks.com/tc/stocks/quote/symbolsearch.aspx?page=" & sTicker & " &order=symbol&seq=asc", Destination _
:=Range("A" & lastRow)) 'uses the ticker value entered and fixed the destination part
'.CommandType = 0
.Name = "symbolsearch_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 = "10"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
lastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Next i
End Sub

How to loop my Current VBA Code

I need to modify with loop my existing VBA. I have a part of website link in Column A. I want to dynamic below this line.
ticjername = Sheet1.Range("A1").Value
I need to loop it, because I want to continue this code until data found in Column A.
My Code:
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+q
'
Dim ticjername As String
ticjername = Sheet1.Range("A1").Value
mURL$ = "http://www.example.com=" & ticjername
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & mURL, Destination:=Range("B1"))
.Name = " "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "1"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
Columns("B:B").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
Range("B1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Cut
Sheets("Sheet2").Select
ActiveCell.Select
ActiveSheet.Paste
Range("A" & Rows.Count).End(xlUp).Offset(1).Select
End Sub
Try below code
Dim rowID As Integer
rowID = 1
Do
'<Your code>
rowID = rowID + 1
Loop Until Sheet1.Cells(rowID, 1).Value = ""
Credit goes to www.stackoverflow.com

Import data from web: repeat loop

I have this VBA script that works:
Sub Basic_Web_Query()
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & ActiveSheet.Range("A2").Value, Destination:=Range("E2"))
.Name = "q?s=goog_2"
.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
End Sub
I want the macro to do this in a loop, i.e. go to A3 with destination E3, A4 with destination A4... etc.
any help will be appreciated
thansk!
I like to use 'offset' for these loops: OFFSET(rows down, columns right).
Negative values move up and left.
for example:
for p= 1 to 5
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;" & ActiveSheet.Range("A2").offset(p-1,0).Value, Destination:=Range("E2").offset(p-1,0))
'other code
End With
next p
google 'loop vba' and you will hit plenty of examples. Here's one:
Dim rw As Long
For rw = 1 To 5
Range("E" & rw).Value = Range("A" & rw).Value
'other stuff
Next rw