Web Query Column Change

ExcelRoy

Well-known Member
Joined
Oct 2, 2006
Messages
2,540
Office Version
  1. 365
Platform
  1. Windows
Hi,

I wonder if some very kind person could help me here, i require a very slight change to some code that James Lankford very kindly provided me with

Currently applies a web query and extract data from that web query and prints it starting from column E

This is fine, but i would like the player ID start in Column E, the Name to be in column G and value in column L?

Is this an easy fix?

The code is listed below

Please, please could some kind person solve my problem

Thanks in advance

Code:
Option Explicit
'************************************************
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
'************************************************
Sub StartHere()
    
    Dim playerIDIndex As Long
    
    For playerIDIndex = 1 To 999
        '''''''''''''''''''''''''''''
        ' some web pages aren't happy when you make a lot of requests really fast
        ' so we're going to use the MS Windows standard Sleep function to wait 1/2 second between each call
        ' this function is supplied by the normal default install of MS Windows
        ' it is not an excel function
        ' so the declare statement at the top let's excel know where to find it - kernel32
        '''''''''''''''''''''''''''''
        Sleep 500
        DisplayPlayerInfo _
            playerIDIndex, _
            ThisWorkbook.Worksheets("Sheet2").Range("E" & CStr(playerIDIndex))
    Next
    
End Sub
'************************************************
Sub DisplayPlayerInfo(playerID As Variant, location As Range)
    
    Dim v As Variant
    v = GetPlayerInfo(playerID)
    location.Resize(1, UBound(v)).Value = v
    
End Sub
'************************************************
Function GetPlayerInfo(playerID As Variant) As Variant
    
    Dim first As Long, last As Long
    Dim s As String
    Dim player_data(1 To 4) As String
    Dim wrksht As Excel.Worksheet
    Dim q As Excel.QueryTable
    Set wrksht = ThisWorkbook.Worksheets.Add
    
    '''''''''''''''''''''''''''''
    '   set player id
    '''''''''''''''''''''''''''''
    player_data(1) = CStr(playerID)
    
    s = ""
    
    With wrksht
        Set q = .QueryTables.Add _
                ( _
                    Connection:="URL;http://www.dreamteamfc.com/fantasyfootball/1011/ViewPlayerProfile.aspx?pid=" & playerID _
                    , Destination:=wrksht.Range("$A$1") _
                )
            With q
                .Name = "ViewPlayerProfile.aspx?pid=" & playerID
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .BackgroundQuery = True
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                '''''''''''''''''''''''''''''''''''''''''''''''''
                .SaveData = False ' changed this, don't know if it mkaes a difference
                '''''''''''''''''''''''''''''''''''''''''''''''''
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .WebSelectionType = xlEntirePage
                .WebFormatting = xlWebFormattingNone
                .WebPreFormattedTextToColumns = True
                .WebConsecutiveDelimitersAsOne = True
                .WebSingleBlockTextImport = False
                .WebDisableDateRecognition = False
                .WebDisableRedirections = False
                '''''''''''''''''''''''''''''''''''''''''''''''''
                ' added some error handling in case the web page isn't found
                '''''''''''''''''''''''''''''''''''''''''''''''''
                On Error Resume Next
                .Refresh BackgroundQuery:=False
                If Err.Number = 0 Then
                    '''''''''''''''''''''''''''''''''''''''''''''''''
                    ' the web page and player ID was found
                    ' the values we want should be on line 5
                    '''''''''''''''''''''''''''''''''''''''''''''''''
                    s = Trim(wrksht.Cells(5, 1).Value)
                    If Len(s) > 0 Then
                        '''''''''''''''''''''''''''''
                        '   get player name
                        '''''''''''''''''''''''''''''
                        first = InStr(s, "Name: ")
                        last = InStr(s, "Club:")
                        If first > 0 And last > 0 Then
                            player_data(2) = Mid(s, first + 6, last - first - 7)
                        End If
                        '''''''''''''''''''''''''''''
                        '   get player club
                        '''''''''''''''''''''''''''''
                        first = InStr(s, "Club: ")
                        last = InStr(s, "Value:")
                        If first > 0 And last > 0 Then
                            player_data(3) = Mid(s, first + 6, last - first - 7)
                        End If
                        '''''''''''''''''''''''''''''
                        '   get player value
                        '''''''''''''''''''''''''''''
                        first = InStr(s, "Value: ")
                        If first > 0 Then
                            player_data(4) = Mid(s, first + 8)
                        End If
                    End If
                Else
                    '''''''''''''''''''''''''''''''''''''''''''''''''
                    ' Err.Number <> 0
                    ' the web page and player ID was NOT found
                    ' we don't really do anything
                    ' just print the error message in DEBUG window
                    ' the function will continue on and return the player_data array which will contain blank strings
                    '''''''''''''''''''''''''''''''''''''''''''''''''
                    Debug.Print "Player ID: " & playerID & vbCrLf & Err.Description & vbCrLf & "Error Number: " & Err.Number
                End If
                '''''''''''''''''''''''''''''
                ' turn off the ability to check for errors
                ' let excel vba handle them
                ' any errors now will pop up in the standeard excel window
                '''''''''''''''''''''''''''''
                On Error GoTo 0
                '''''''''''''''''''''''''''''''''''''''''''''''''
            End With
            q.Delete
        Set q = Nothing
    End With
    
    wrksht.Cells.Clear
    
    Application.DisplayAlerts = False
    wrksht.Delete
    Application.DisplayAlerts = True
    
    Set wrksht = Nothing
    
    GetPlayerInfo = player_data
    
End Function
'************************************************
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I thought there may be a way to change a number dependant on the column

ie column E would be 5 etc, etc

Any takers offering help?
 
Upvote 0
Hmm... I think it takes a table and dumps it straight into the sheet starting from column A - there's nowhere to adjust where the individual columns of data go.

How about using the macro recorder to record yourself moving the columns manually, then insert the code it generates somewhere near the end of the existing macro to achieve the same effect?
 
Upvote 0
Hi,

i have tried this many times without success, unfortunatley can't seem to get it working!

Any other ideas? Anyone?

Thanks
 
Upvote 0
The following macro rearranges the columns of a worksheet. Call it like this: Call RearrangeColumns("F,E,D,C,B,A") as the last thing in your main routine, I suppose. That call will swap columns A-F back-to-front... if you see what I mean. If you run it twice, they all end up back where they started. Columns which aren't specified are left untouched. Change the sheet name as applicable. Test on a copy of your worksheet!
Code:
[FONT=Courier New][SIZE=1]Option Explicit[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]Sub RearrangeColumns(argSequence As String)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  Const MySheet As String = "[COLOR=red][B]Sheet1[/B][/COLOR]"
  
  Dim iLastRow As Long
  Dim iLastColumn As Long
  Dim iPtr As Long
  Dim arrSequence As Variant
  Dim iListNum As Integer[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  With Sheets(MySheet)
    iLastRow = .UsedRange.Rows.Count + .UsedRange.Row - 1
    iLastColumn = .UsedRange.Columns.Count + .UsedRange.Column - 1
    For iPtr = 1 To iLastColumn
      .Cells(iLastRow + 1, iPtr) = AlphaRange(iPtr)
    Next iPtr
  End With[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  ActiveWorkbook.Worksheets(MySheet).Sort.SortFields.Clear[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  arrSequence = Array(Split(argSequence, ","))
  Application.AddCustomList ListArray:=arrSequence
  iListNum = Application.GetCustomListNum(arrSequence)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  ActiveWorkbook.Worksheets(MySheet).Sort.SortFields.Add _
     Key:=Range("A" & CStr(iLastRow + 1) & ":" & AlphaRange(iLastColumn) & CStr(iLastRow + 1)), _
     SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal, CustomOrder:=iListNum[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]
  With ActiveWorkbook.Worksheets(MySheet).Sort
    .SetRange Range("A1" & ":" & AlphaRange(iLastColumn) & CStr(iLastRow + 1))
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlLeftToRight
    .SortMethod = xlPinYin
    .Apply
  End With[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  Application.DeleteCustomList iListNum[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  Sheets(MySheet).Rows(iLastRow + 1).ClearContents[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]End Sub
[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]Public Function AlphaRange(ByVal argColumn As Integer) As String[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  Dim intPrefix As Integer
  Dim strPrefix As String[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  intPrefix = 0[/SIZE][/FONT]

[FONT=Courier New][SIZE=1]  Do Until argColumn <= 26
    intPrefix = intPrefix + 1
    argColumn = argColumn - 26
  Loop
[/SIZE][/FONT]
[FONT=Courier New][SIZE=1]  If intPrefix > 0 Then strPrefix = Chr(intPrefix + 64)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]  AlphaRange = strPrefix & Chr(argColumn + 64)[/SIZE][/FONT]
[FONT=Courier New][SIZE=1][/SIZE][/FONT] 
[FONT=Courier New][SIZE=1]End Function[/SIZE][/FONT]
 
Upvote 0
hey Ruddles, thanks for the reply!

do i run your code after my other code?

also, does your code go into a module?

Thanks
 
Upvote 0
Call it like this: Call RearrangeColumns("F,E,D,C,B,A") as the last thing in your main routine, I suppose.

It could go in the worksheet module or a general module.
 
Upvote 0
Hi Ruddles,

I put your code into "This Workbook"

It doesn't show up as an active macro?

Have i done something wrong, Thanks
 
Upvote 0
Call it like this: Call RearrangeColumns("F,E,D,C,B,A") as the last thing in your main routine

I won't appear under Macros because it takes arguments. If you want to test it, call it from the immediate window: Call RearrangeColumns("F,E,D,C,B,A"). Eventually you'll be calling it from your main routine after the data has been imported.

It could go in the worksheet module or a general module.

But not the workbook module.
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,716
Members
449,093
Latest member
Mnur

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top