+ Reply to Thread
Results 1 to 6 of 6

Marco for multiple web quiries

Hybrid View

  1. #1
    Registered User
    Join Date
    07-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2016
    Posts
    6

    Question Marco for multiple web quiries

    I successfully made my own macro which scraped infomation from

    URLs http://www.sda.gov.cn/WS01/CL0027/index_1.html...index_2.html...index_3.html etc.

    using these code
    Sub Macro1()
    Dim i As Integer
    For i = 1 To 83
    '
    ' Macro1 Macro
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://www.sda.gov.cn/WS01/CL0027/index_" & i & ".html", Destination:=Range("$A$1" _
            ))
            .Name = "index"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = False
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingAll
            .WebTables = "22"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
    Next i
    End Sub
    but I'm facing a little problem, actually the items in these URLs are added in a daily basis and the most recent info will be stored in
    http://www.sda.gov.cn/WS01/CL0027/index.html while the outdated one will be moved to the subsequent pages i.e. index_1, index_2....

    Thus, my questions are
    1. How can i included the first page http://www.sda.gov.cn/WS01/CL0027/index.html in my macro loop?
    2. Is it able to add a checking against the total number of pages so that the macro wont miss any newly added page?


    I m just new to the macro stuff, any help would be appreciated!
    Thanks in advance!

    Sam.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Marco for multiple web quiries

    Maybe:

    Sub Macro1()
    Dim i As Integer
    For i = 0 To 83
    '
    ' Macro1 Macro
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://www.sda.gov.cn/WS01/CL0027/index" & IIf(i=0, "", "_" & i) & ".html", Destination:=Range("$A$1" _
            ))
            .Name = "index"
            .FieldNames = True
            .RowNumbers = False
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    07-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: Marco for multiple web quiries

    wow~ it works like a charm! Thanks J!

    but then is it out of the power of excel macro to deal with the second question?

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Marco for multiple web quiries

    I didn't understand the second question. Perhaps someone else will.

  5. #5
    Registered User
    Join Date
    07-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2016
    Posts
    6

    Re: Marco for multiple web quiries

    Quote Originally Posted by JBeaucaire View Post
    I didn't understand the second question. Perhaps someone else will.
    Sorry for the confusing question:P

    let me make it clear

    Since the total NO. of URLs that this macro concerns would keep increasing in the future from 83...84....85...86...87......
    It there any way to automatically get the NO. of pages? i.e. the i value isnt pre-defined but input only after checking the NO. of pages.

    Thanks again!

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Marco for multiple web quiries

    Checking the number of pages how? If you provide the method of checking, we can certainly see if VBA can accomodate that information.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1