2

I need help, my code will not pull data into the new sheet that it creates from the website. It comes up as blank. It is really frustrating. The query table will not pull the data in after I assigned the string variable "counties" as a website address. I have looked all over the internet and haven't found the answer of how to fix this.

counties = Range("HTML").Offset(x, 0) shows up to equal 08/08001.html which is part of the website address.

    Sub Macro6()

     Dim x As Integer
     Dim counties As String
     For x = 1 To 3

        Sheets("RawData").Select
        counties = Range("HTML").Offset(x, 0)
        Sheets.Add.Name = "DataTemp"

        With ActiveSheet.QueryTables.Add(Connection:="URL;http://quickfacts.census.gov/qfd/states/" & counties & ".html", Destination:=Range("$A$1"))
            .Name = "08001"
            .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 = "3,4,5"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False

        End With

'This part moves the data from the newly created "DataTemp" sheet into the "Demographics" sheet.

Columns("A:B").Select
ActiveWindow.ScrollColumn = 2
Range("A:B,D:D").Select
Range("D1").Activate
Selection.ClearContents
Range("C1:C63").Select
Selection.Copy
Sheets("Demographics").Select
Cells(6, x + 2).Select
ActiveSheet.Paste
Columns("C:C").EntireColumn.AutoFit
ActiveSheet.Previous.Select
Application.CutCopyMode = False
Application.DisplayAlerts = False
ActiveWindow.SelectedSheets.Delete
Application.DisplayAlerts = True

Next x

End Sub

  • Beyond closing the For ... Next statement, you are looping three times but each time you reDim the string variable and each new query table is given the same name. You could also edit your question to provide an example of what is found in `ActiveSheet.Cells(x, 1).Value`. –  Dec 03 '15 at 04:33
  • Okay, I made some changes to the code, how about now? Even for the first value the query table doesn't show values on the new sheet called "DataTemp". – Patrick Sullivan Dec 03 '15 at 04:40
  • I'm attempting a code cleanup but the latter portion of recorded code that transfers the data is confusing with all of the commands that were recorded but actually serve no purpose. Could you provide a narrative that describes what you are trying to do there? –  Dec 03 '15 at 04:48
  • Thanks so much for all you have done so far to help me. I am attempting to get the census data for all 44 counties in Colorado. My original thought was to pull the data one county at a time into a new worksheet, then take that data and paste it into the "Demographics" worksheet which would serve as the master collection of all of the data. This would make it organized and easy to read. The only data that I need though is the data which shows up in the column under the county name. That is why I deleted everything else and tried to paste it into the "Demographics" sheet. – Patrick Sullivan Dec 03 '15 at 07:04
  • The commands in the second part deleted the unnecessary data in the newly created worksheet and pasted each subsequent set of data into a new column in the "Demographics" sheet each time the program ran through its loop. – Patrick Sullivan Dec 03 '15 at 07:07
  • My answer below should be sufficient to get you started; just expand the upper limit of the For ... Next. Query tables are an easy way to get started with 'scraping' information from public web pages and you can learn better methods like XMLHTTP once you get more comfortable with the actual HTML code behind the page. –  Dec 03 '15 at 07:07

1 Answers1

1

Here is a quick rewrite of your code with the primary intent of removing reliance on .Select and .Activate commands¹ in favor of direct worksheet and cell addressing. It isn't complete but does pull in the first three groups of three tables and should provide a framework on which you can build.

Sub get_County_Census_Data()

    Dim x As Long, lr As Long, nr As Long
    Dim counties As String, sURL As String

    For x = 1 To 3

        sURL = "http://quickfacts.census.gov/qfd/states/×C×.html"
        counties = Worksheets("RawData").Range("HTML").Offset(x, 0) 'e.g. 08/08001
        sURL = Replace(sURL, "×C×", counties)

        On Error GoTo bm_New_TMP_ws  'if DataTemp doesn't exist, go create one
        With Worksheets("DataTemp")
            On Error GoTo 0
            .Cells(1, 1).CurrentRegion.Clear

            With .QueryTables.Add(Connection:="URL;" & sURL, _
              Destination:=.Range("$A$1"))   'associate A1 with the DataTemp worksheet (e.g. .Range not Range)
                .Name = Right(counties, 5)  'unique name to the connection
                .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 = "3,4,5"
                .WebPreFormattedTextToColumns = True
                .WebConsecutiveDelimitersAsOne = True
                .WebSingleBlockTextImport = False
                .WebDisableDateRecognition = False
                .WebDisableRedirections = False
                .Refresh BackgroundQuery:=False
            End With

            With Worksheets("Demographics")
                nr = Application.Max(6, .Cells(Rows.Count, x + 2).End(xlUp).Offset(1, 0).Row)
            End With
            lr = .Cells(Rows.Count, 3).End(xlUp).Row
            .Cells(1, 3).Resize(lr, 1).Copy _
                Destination:=Worksheets("Demographics").Cells(nr, x + 2)
            With Worksheets("Demographics")
                .Columns(x + 2).EntireColumn.AutoFit
            End With

            'no need to retain this; delete the connection and the worksheet
            Application.DisplayAlerts = False
            .Parent.Connections(.Parent.Connections.Count).Delete
            .Delete
            Application.DisplayAlerts = True
        End With
    Next x

    GoTo bm_Safe_Exit  'skip over the worksheet creation routine

bm_New_TMP_ws:
    On Error GoTo 0
    With Worksheets.Add(After:=Sheets(Sheets.Count))
        .Name = "DataTemp"
    End With
    Resume

bm_Safe_Exit:
    '
End Sub

There really isn't a necessity to delete the DataTemp worksheet every cycle; clearing the data and deleting the connection should be sufficient. However, this demonstrates a method of creating a worksheet 'on-the-fly' repetitively and that may be important to learn.


¹ See How to avoid using Select in Excel VBA macros for more methods on getting away from relying on select and activate to accomplish your goals.

Community
  • 1
  • 1