VBA Data Scrape Macro Using Frequency Analysis

htmagic

New Member
Joined
Dec 2, 2009
Messages
40
Office Version
  1. 2019
Platform
  1. Windows
Hello,

Although I have lurked on this forum for some time now, this is my first technical post to the group asking for programming advice.
I have some knowledge of VBA, learning it as I go, and some of the advanced statistical functions of Excel.
Some of the posts and replies of this group have gotten me this far as a result.

I have written a program to analyze the winning Powerball Lottery numbers. The first five groups of numbers use 1-59 whereas the Powerball number is 1-35. I am using the statistical function frequency to analyze the numbers and provide me the number of times a certain number has been "pulled" for the winning number.

My question is, I am doing a data scrape using a VBA macro (called Draw) of a website. I paste this into a worksheet called WebScrape (Sheet 1) which contains this raw data.
I would like to add a button to CHECK RESULTS which updates the data from the webpage. The data results come in as one string and then I use a macro (called Parse) to parse the data into a series of two digit numbers.

VBA Code looks like this:

HTML:
Sub Draw()
'
' Draw Macro
' Scrape data from website
'

'
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://www.tnlottery.com/winningnumbers/default.aspx#pwrball", _
        Destination:=Range("$A$1"))
        .Name = "default.aspx#pwrball_1"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = """dgPowerBallWinners"""
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Sub
Sub Parse()
'
' Parse Macro
' Parse data
'
' Keyboard Shortcut: Ctrl+t
'
    Application.Goto Reference:="R2C3"
    Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=ActiveCell.Offset(0, 3).Range("A1"), _
        DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter _
        :=True, Tab:=False, Semicolon:=False, Comma:=False, Space:=True, _
        Other:=True, OtherChar:="-", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array _
        (3, 1), Array(4, 1), Array(5, 1), Array(6, 1)), TrailingMinusNumbers:=True
End Sub

OK, now the WebScrape page looks like this:

[TABLE="width: 500"]
<tbody>[TR]
[TD]Draw Date[/TD]
[TD]
Draw Result
[/TD]
[TD]Details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]PB[/TD]
[/TR]
[TR]
[TD]7/11/2012[/TD]
[TD]05-22-36-49-55 23[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]3[/TD]
[TD]5[/TD]
[TD]29[/TD]
[TD]39[/TD]
[TD]59[/TD]
[TD]29[/TD]
[/TR]
[TR]
[TD]7/7/2012[/TD]
[TD]03-05-29-39-59 29[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]14[/TD]
[TD]19[/TD]
[TD]35[/TD]
[TD]39[/TD]
[TD]56[/TD]
[TD]33[/TD]
[/TR]
[TR]
[TD]7/4/2012[/TD]
[TD]14-19-35-39-56 33[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]7[/TD]
[TD]15[/TD]
[TD]20[/TD]
[TD]41[/TD]
[TD]44[/TD]
[TD]22[/TD]
[/TR]
[TR]
[TD]6/30/2012[/TD]
[TD]07-15-20-41-44 22[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]6[/TD]
[TD]34[/TD]
[TD]40[/TD]
[TD]46[/TD]
[TD]58[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]6/27/2012[/TD]
[TD]06-34-40-46-58 06[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]3[/TD]
[TD]41[/TD]
[TD]44[/TD]
[TD]53[/TD]
[TD]30[/TD]
[/TR]
[TR]
[TD]6/23/2012[/TD]
[TD]01-03-41-44-53 30[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]11[/TD]
[TD]17[/TD]
[TD]29[/TD]
[TD]56[/TD]
[TD]57[/TD]
[TD]14[/TD]
[/TR]
[TR]
[TD]6/20/2012[/TD]
[TD]11-17-29-56-57 14[/TD]
[TD]details[/TD]
[TD][/TD]
[TD]8[/TD]
[TD]14[/TD]
[TD]15[/TD]
[TD]16[/TD]
[TD]27[/TD]
[TD]26[/TD]
[/TR]
</tbody>[/TABLE]

The numbers to the right of the details are the parsed numbers. Can I do this automatically when I data scrape the webpage?

Then I have a worksheet (called Data) with the date and the winning numbers, with the Powerball (PB) number at the end. A third worksheet (called Calculations) analyzes the data table using the FREQUENCY function. On this page, I analyze the winning numbers (1-59) with a BIN (1-59) and then the Frequency (FREQ) using the following formula:

=FREQUENCY(Data!$C$2:$G$43,$B$2:$B$60)

I do a similar PB BIN (1-35) and PB FREQ for the Poweball (PB) numbers. The array works well and produces values. Then I have a column for the SORT BIN and SORT FREQ and SORT PB BIN and SORT PB FREQ. I copy and then paste the array values then sort according to FREQ, highest to lowest values.

My question here is can I append to this array, making it grow as I hit the button CHECK RESULTS, pulling the latest lottery numbers from the webpage and transferring them into the
Data worksheet? I would like to add to the array and let it grow larger as it adds more dates and numbers to the Data worksheet. Then the array would automatically update as more data is added.

I am sorry for the long post and complicated description but I cannot add my Excel file here as HTML Makeer apparently is no longer available. I also cannot post attachments.
Thank you in advance for your help on this problem.

MagicBill
 
Thanks, Bill!

But now I have a problem.

On the WebScrape worksheet, I added a button to work the Process macro.


[TABLE="class: grid, width: 650"]
<tbody>[TR]
[TD]Game Name[/TD]
[TD]Draw Date[/TD]
[TD]Draw Result[/TD]
[TD]Details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]7/14/2012[/TD]
[TD]04-16-32-37-46 13[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD="colspan: 2"]
Check Lotto Results (Button)

<tbody>
</tbody>
[/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]7/11/2012[/TD]
[TD]05-22-36-49-55 23[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]7/7/2012[/TD]
[TD]03-05-29-39-59 29[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]7/4/2012[/TD]
[TD]14-19-35-39-56 33[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]6/30/2012[/TD]
[TD]07-15-20-41-44 22[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]6/27/2012[/TD]
[TD]06-34-40-46-58 06[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Powerball[/TD]
[TD="align: right"]6/23/2012[/TD]
[TD]01-03-41-44-53 30[/TD]
[TD]details[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

So when I hit the Button, "Check Lotto Results", I get this on the Calculations page.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]BIN[/TD]
[TD]FREQ.[/TD]
[TD]PB FREQ.[/TD]
[TD]SORT BIN[/TD]
[TD]SORT FREQ[/TD]
[TD]SORT BIN[/TD]
[TD]SORT PB FREQ[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]7/4/2012[/TD]
[TD]14-19-35-39-56 33[/TD]
[TD="align: center"]########[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]3[/TD]
[TD="align: center"]########[/TD]
[TD]07-15-20-41-44 22[/TD]
[TD="align: center"]########[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]0[/TD]
[TD="align: center"]########[/TD]
[TD]06-34-40-46-58 06[/TD]
[TD="align: right"]7/7/2012[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]0[/TD]
[TD="align: center"]########[/TD]
[TD]05-22-36-49-55 23[/TD]
[TD="align: right"]7/4/2012[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]2[/TD]
[TD="align: center"]########[/TD]
[TD]04-16-32-37-46 13[/TD]
[TD="align: center"]########[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]7/7/2012[/TD]
[TD]03-05-29-39-59 29[/TD]
[TD="align: center"]########[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]1[/TD]
[TD="align: center"]########[/TD]
[TD]01-03-41-44-53 30[/TD]
[TD="align: center"]########[/TD]
[TD]details[/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]0[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

So instead of pasting the BIN, FREQ, and PB FREQ ranges from the Data worksheet, it is getting the data from the WebScrape worksheet, which is Sheet 1.
So how do I set it so it pastes from the right page? :confused:

MagicBill
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Can you provide the code that is behind the button?
Bill,

I would if I could find it. I went to Developer, Insert (Button) (form Control), and assigned the Process macro to it. I thought I could just add a button on the front page to run the Process macro but I'm obviously not doing it right. Could someone please walk me through how to do this?

Thanks,

MagicBill
 
Upvote 0
I looked into this button issue more.
When I run the macro Process, it runs fine, just like it is supposed to run.

When I added the button to run the Process macro, it is on the WebScrape worksheet.
I believe the macro still assumes it should be on the WebScrape page whereas in Sort routine, it should be copying data from the Calculations page, not the WebScrape page.
Now looking through the code, it appears that the UpdateCalcWS is copying from the right page whereas with the button it is obviously stuck on the wrong page.

Help! I'm sure this is a simple newbie error but I don't know where to look.

MagicBill
 
Upvote 0
Re: [SOLVED] VBA Data Scrape Macro Using Frequency Analysis

To save folks from digging through the posts, here is the latest code for the Process macro.

Code:
[COLOR=#0000ff]Option Explicit
Type typeRec
    Bin As Integer
    Freq As Integer
End Type
Sub Process()
    Dim WsWeb As Worksheet
    Dim WsData As Worksheet
    Dim WsCalc As Worksheet
    Dim qt As QueryTable
    
    Dim WsWebRowNo As Long
    Dim WsDataRowNo As Long
    Dim Draw As String
    Dim N As Variant
    Dim I As Integer
    
    Dim MaxDateData As Date
    
    Set WsWeb = ThisWorkbook.Worksheets("WebScrape")
    Set WsData = ThisWorkbook.Worksheets("Data")
    Set WsCalc = ThisWorkbook.Worksheets("Calculations")
    
    'Extract the informationn from the website
    Call DeleteQT
    WsWeb.Cells.Clear
    Call DrawNumbers
    Set qt = WsWeb.QueryTables(1)
    
    WsDataRowNo = WsData.Cells(WsData.Rows.Count, "A").End(xlUp).Row + 1
    MaxDateData = Application.WorksheetFunction.Max(WsData.Columns("B:B"))
    For WsWebRowNo = 2 To WsWeb.Cells(WsWeb.Rows.Count, "A").End(xlUp).Row
        If WsWeb.Cells(WsWebRowNo, "B") > MaxDateData Then
            WsData.Cells(WsDataRowNo, "A") = WsWeb.Cells(WsWebRowNo, "A")
            WsData.Cells(WsDataRowNo, "B") = WsWeb.Cells(WsWebRowNo, "B")
            
            Draw = Replace(WsWeb.Cells(WsWebRowNo, "C"), " ", "-")
            N = Split(Draw, "-")
            For I = 0 To UBound(N)
                WsData.Cells(WsDataRowNo, 3 + I) = N(I)
            Next I
            
            WsDataRowNo = WsDataRowNo + 1
        End If
    Next
    
    WsDataRowNo = WsData.Cells(WsData.Rows.Count, "A").End(xlUp).Row
    
    ThisWorkbook.Names.Add Name:="DrawRng", RefersToR1C1:="=Data!R2C3:R" & WsDataRowNo & "C7"
    ThisWorkbook.Names.Add Name:="PbRng", RefersToR1C1:="=Data!R2C8:R" & WsDataRowNo & "C8"
    
    
    Call UpdateCalcWS(WsCalc)
End Sub
Function UpdateCalcWS(WsCalc As Worksheet)
    'Add Formula Arrays to Calculations WorkSheet
    WsCalc.Range("C2:C60").FormulaArray = "=FREQUENCY(DrawRng,RC[-1]:R[58]C[-1])"
    WsCalc.Range("D2:D36").FormulaArray = "=FREQUENCY(pbRng,RC[-2]:R[34]C[-2])"
    
    'Copy the Draw Freqs to a new range
    WsCalc.Range("E2:F60").Value = Range("B2:C60").Value
    WsCalc.Range("G2:G36").Value = Range("B2:B36").Value
    WsCalc.Range("H2:H36").Value = Range("D2:D36").Value
    WsCalc.Range("E1:F60").Sort Key1:=WsCalc.Range("F1"), Order1:=xlDescending, _
                              Header:=xlYes, _
                              MatchCase:=False, _
                              Orientation:=xlTopToBottom
    WsCalc.Range("G1:H36").Sort Key1:=WsCalc.Range("H1"), Order1:=xlDescending, _
                              Header:=xlYes, _
                              MatchCase:=False, _
                              Orientation:=xlTopToBottom
End Function
Function SortIt(WsCalc As Worksheet, strRng As String, SortCol As String)
    Application.CutCopyMode = False
    
    WsCalc.Sort.SortFields.Clear
    WsCalc.Sort.SortFields.Add Key:=WsCalc.Columns(SortCol), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
    With WsCalc.Sort
        .SetRange Range(strRng)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Function
Function DrawNumbers()
    Dim WS As Worksheet
    
    Set WS = ThisWorkbook.Worksheets("WebScrape")
    With WS.QueryTables.Add(Connection:= _
        "URL;http://www.tnlottery.com/winningnumbers/default.aspx#pwrball", Destination:=WS.Range("$A$1"))
        .Name = "pwrball"
        .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 = "dgPowerBallWinners"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Function
Function DeleteQT()
    Dim qt As QueryTable
    For Each qt In ThisWorkbook.Worksheets("WebScrape").QueryTables
        qt.Delete
    Next
End Function
[/COLOR]

Now when I made the button to "Check Lotto Results" on the WebScrape worksheet, it obviously is staying on that sheet when the copying is performed. So it is in this snippet of code:

Code:
[COLOR=#008000]'Copy the Draw Freqs to a new range[/COLOR]
[COLOR=#0000ff]    WsCalc.Range("E2:F60").Value = Range("B2:C60").Value
    WsCalc.Range("G2:G36").Value = Range("B2:B36").Value
    WsCalc.Range("H2:H36").Value = Range("D2:D36").Value[/COLOR]

So the new range is being copied from WsWeb (WebScrape) worksheet, instead of the WsCalc sheet. So what I think is happening is the ranges on the right are implied to be WsCalc when in essence they are really WsWeb which is where the button to run the macro is located. So I modified the code to this:

Code:
 [COLOR=#008000]'Copy the Draw Freqs to a new range[/COLOR]
    [COLOR=#0000ff]WsCalc.Range("E2:F60").Value = WsCalc.Range("B2:C60").Value
    WsCalc.Range("G2:G36").Value = WsCalc.Range("B2:B36").Value
    WsCalc.Range("H2:H36").Value = WsCalc.Range("D2:D36").Value[/COLOR]

And now it works perfectly! So the entire issue I was having was not in the creating the macro button, but the relative referencing on the wrong page. And I actually figured it out myself! :laugh:

Oh, oh, if I keep this up, someone may think I'm an expert VBA programmer! :rofl:

Thanks again to Bill and SHG for their help. I have gone as far as I need right now. My program will analyze the Tennessee Powerball Lottery results and will post the most frequently used values. Not only does it sort the most frequently used numbers drawn (1-59) and the Powerball numbers (1-35), but it plots them on charts as well.

Maybe later on, I will look at the other states and have a Powerball Lottery Analyzer to analyze their frequently used numbers. Hopefully the draw numbers (1-59) and Powerball numbers (1-35) will be the same. If they are, then I can do a look-up table based upon the state which will have a URL listing for that state's Powerball Lottery numbers. But the concept and the rest of it should be similar.

But right now, let me play with what I have and see how it goes.

Thanks again.

MagicBill
 
Upvote 0
Re: [SOLVED] VBA Data Scrape Macro Using Frequency Analysis

To save folks from digging through the posts, here is the latest code for the Process macro.

Code:
[COLOR=#0000ff]Option Explicit
Type typeRec
    Bin As Integer
    Freq As Integer
End Type
Sub Process()
    Dim WsWeb As Worksheet
    Dim WsData As Worksheet
    Dim WsCalc As Worksheet
    Dim qt As QueryTable
    
    Dim WsWebRowNo As Long
    Dim WsDataRowNo As Long
    Dim Draw As String
    Dim N As Variant
    Dim I As Integer
    
    Dim MaxDateData As Date
    
    Set WsWeb = ThisWorkbook.Worksheets("WebScrape")
    Set WsData = ThisWorkbook.Worksheets("Data")
    Set WsCalc = ThisWorkbook.Worksheets("Calculations")
    
    'Extract the informationn from the website
    Call DeleteQT
    WsWeb.Cells.Clear
    Call DrawNumbers
    Set qt = WsWeb.QueryTables(1)
    
    WsDataRowNo = WsData.Cells(WsData.Rows.Count, "A").End(xlUp).Row + 1
    MaxDateData = Application.WorksheetFunction.Max(WsData.Columns("B:B"))
    For WsWebRowNo = 2 To WsWeb.Cells(WsWeb.Rows.Count, "A").End(xlUp).Row
        If WsWeb.Cells(WsWebRowNo, "B") > MaxDateData Then
            WsData.Cells(WsDataRowNo, "A") = WsWeb.Cells(WsWebRowNo, "A")
            WsData.Cells(WsDataRowNo, "B") = WsWeb.Cells(WsWebRowNo, "B")
            
            Draw = Replace(WsWeb.Cells(WsWebRowNo, "C"), " ", "-")
            N = Split(Draw, "-")
            For I = 0 To UBound(N)
                WsData.Cells(WsDataRowNo, 3 + I) = N(I)
            Next I
            
            WsDataRowNo = WsDataRowNo + 1
        End If
    Next
    
    WsDataRowNo = WsData.Cells(WsData.Rows.Count, "A").End(xlUp).Row
    
    ThisWorkbook.Names.Add Name:="DrawRng", RefersToR1C1:="=Data!R2C3:R" & WsDataRowNo & "C7"
    ThisWorkbook.Names.Add Name:="PbRng", RefersToR1C1:="=Data!R2C8:R" & WsDataRowNo & "C8"
    
    
    Call UpdateCalcWS(WsCalc)
End Sub
Function UpdateCalcWS(WsCalc As Worksheet)
    'Add Formula Arrays to Calculations WorkSheet
    WsCalc.Range("C2:C60").FormulaArray = "=FREQUENCY(DrawRng,RC[-1]:R[58]C[-1])"
    WsCalc.Range("D2:D36").FormulaArray = "=FREQUENCY(pbRng,RC[-2]:R[34]C[-2])"
    
    'Copy the Draw Freqs to a new range
    WsCalc.Range("E2:F60").Value = Range("B2:C60").Value
    WsCalc.Range("G2:G36").Value = Range("B2:B36").Value
    WsCalc.Range("H2:H36").Value = Range("D2:D36").Value
    WsCalc.Range("E1:F60").Sort Key1:=WsCalc.Range("F1"), Order1:=xlDescending, _
                              Header:=xlYes, _
                              MatchCase:=False, _
                              Orientation:=xlTopToBottom
    WsCalc.Range("G1:H36").Sort Key1:=WsCalc.Range("H1"), Order1:=xlDescending, _
                              Header:=xlYes, _
                              MatchCase:=False, _
                              Orientation:=xlTopToBottom
End Function
Function SortIt(WsCalc As Worksheet, strRng As String, SortCol As String)
    Application.CutCopyMode = False
    
    WsCalc.Sort.SortFields.Clear
    WsCalc.Sort.SortFields.Add Key:=WsCalc.Columns(SortCol), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    
    With WsCalc.Sort
        .SetRange Range(strRng)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Function
Function DrawNumbers()
    Dim WS As Worksheet
    
    Set WS = ThisWorkbook.Worksheets("WebScrape")
    With WS.QueryTables.Add(Connection:= _
        "URL;http://www.tnlottery.com/winningnumbers/default.aspx#pwrball", Destination:=WS.Range("$A$1"))
        .Name = "pwrball"
        .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 = "dgPowerBallWinners"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
End Function
Function DeleteQT()
    Dim qt As QueryTable
    For Each qt In ThisWorkbook.Worksheets("WebScrape").QueryTables
        qt.Delete
    Next
End Function
[/COLOR]

Now when I made the button to "Check Lotto Results" on the WebScrape worksheet, it obviously is staying on that sheet when the copying is performed. So it is in this snippet of code:

Code:
[COLOR=#008000]'Copy the Draw Freqs to a new range[/COLOR]
[COLOR=#0000ff]    WsCalc.Range("E2:F60").Value = Range("B2:C60").Value
    WsCalc.Range("G2:G36").Value = Range("B2:B36").Value
    WsCalc.Range("H2:H36").Value = Range("D2:D36").Value[/COLOR]

So the new range is being copied from WsWeb (WebScrape) worksheet, instead of the WsCalc sheet. So what I think is happening is the ranges on the right are implied to be WsCalc when in essence they are really WsWeb which is where the button to run the macro is located. So I modified the code to this:

Code:
 [COLOR=#008000]'Copy the Draw Freqs to a new range[/COLOR]
    [COLOR=#0000ff]WsCalc.Range("E2:F60").Value = WsCalc.Range("B2:C60").Value
    WsCalc.Range("G2:G36").Value = WsCalc.Range("B2:B36").Value
    WsCalc.Range("H2:H36").Value = WsCalc.Range("D2:D36").Value[/COLOR]

And now it works perfectly! So the entire issue I was having was not in the creating the macro button, but the relative referencing on the wrong page. And I actually figured it out myself! :laugh:

Oh, oh, if I keep this up, someone may think I'm an expert VBA programmer! :rofl:

Thanks again to Bill and SHG for their help. I have gone as far as I need right now. My program will analyze the Tennessee Powerball Lottery results and will post the most frequently used values. Not only does it sort the most frequently used numbers drawn (1-59) and the Powerball numbers (1-35), but it plots them on charts as well.

Maybe later on, I will look at the other states and have a Powerball Lottery Analyzer to analyze their frequently used numbers. Hopefully the draw numbers (1-59) and Powerball numbers (1-35) will be the same. If they are, then I can do a look-up table based upon the state which will have a URL listing for that state's Powerball Lottery numbers. But the concept and the rest of it should be similar.

But right now, let me play with what I have and see how it goes.

Thanks again.

MagicBill
thanks for helping on this code and sharing the latest code. do you have somehow the code with its document? I tried putting it in a macro enabled excel but somehow did not work. perhaps if someone has the working version of excel document. grateful if you could share
 
Upvote 0
Re: [SOLVED] VBA Data Scrape Macro Using Frequency Analysis

thanks for helping on this code and sharing the latest code. do you have somehow the code with its document? I tried putting it in a macro enabled excel but somehow did not work. perhaps if someone has the working version of excel document. grateful if you could share
I have uploaded it here https://skydrive.live.com/redir?resid=D7C00A2BF29043E0!259 I get N/A error on the calculation sheet frequency formula
 
Upvote 0
Re: [SOLVED] VBA Data Scrape Macro Using Frequency Analysis

Remember, it is an array. You treat arrays differently than other data.
Hitting just Enter will not work.
Study how arrays are treated.
It is not caused by array. I know array functions also tried with control shift enter. Did not work
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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