+ Reply to Thread
Results 1 to 28 of 28

VBA URL In Range varies and can be any number of row

Hybrid View

  1. #1
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    VBA URL In Range varies and can be any number of row

    Still unsuccessful. Have not got any reply.

    Let me try to explain it to you again so that it can be clear. Here are the conditions:
    1. sheet1 Column B contains the list of web addresses. The no. of hyperlink is unknown or better to say it can be upto any row no.
    2. VBA code will web fetch data from each hyperlink in sheet2 from a table.
    3. Table name is common in all sheets but table row number varies.
    4. Web table column name need to be appear only once.


    Query() 
     
    Dim URL As Range 
     
    For Each URL In Range ("A1:A10").Cells 
        ActiveWorkbook.Worksheets.Add 
        With ActiveSheet.QueryTables.Add(Connection:= _ 
            "URL;http://www.website.com/" & URL & "secondpartofurl" _ 
            , Destination:=Range("$A$1")) 
            .Name = "placeholder" 
            .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 = "16" 
            .WebPreFormattedTextToColumns = True 
            .WebConsecutiveDelimitersAsOne = True 
            .WebSingleBlockTextImport = False 
            .WebDisableDateRecognition = False 
            .WebDisableRedirections = False 
            .Refresh BackgroundQuery:=False 
        End With 
    Next 
    End Sub
    Last edited by maria.blue44; 03-11-2013 at 08:00 PM. Reason: to make it clear

  2. #2
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    hi maria
    what exactly are you asking to be resolved?
    Are you asking how to only check the cells with a url in rather than the whole range?
    ie if rows 1 to 8 have url then check those rather than 1 to 10 as in your range?

    going out for a couple of hours, but if you tell me what you want i will take a look when im back home
    Regards
    Sean

    Please add to my reputation if you think i helped
    (click on the star below the post)
    Mark threads as "Solved" if you have your answer
    (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code:
    [code] Your code here [code]
    Please supply a workbook containing example Data:
    It makes its easier to answer your problem & saves time!

  3. #3
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Yes Sean Thomas. Check the cells with a url in becasue my URLs can be up to any no. of rows.

  4. #4
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    This might do what you want

    Sub checkURL()
        Dim URL, MyRange As Range
        Set MyRange = Sheet1.Range("A1:A" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row)
        For Each URL In MyRange
            If URL.Value <> "" Then
                ActiveWorkbook.Worksheets.Add
                With ActiveSheet.QueryTables.Add(Connection:= _
                    "URL;http://www.website.com/" & URL & "secondpartofurl" _
                    , Destination:=Range("$A$1"))
                    .Name = "placeholder"
                    .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 = "16"
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .WebSingleBlockTextImport = False
                    .WebDisableDateRecognition = False
                    .WebDisableRedirections = False
                    .Refresh BackgroundQuery:=False
                End With
            End If
        Next
    End Sub

  5. #5
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Yes. The VBA code is working on first link only and not going to the the 2nd, 3rd, 4th, 5th..... links. I want the output in a single sheet.
    Last edited by maria.blue44; 03-12-2013 at 09:31 AM.

  6. #6
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    I mean it is not going to the next links available.

  7. #7
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    Hi, i havent had much experience with web queries but i think you need to add this line after End With

    ActiveSheet.QueryTables.Delete
    this should delete the current web query before moving onto the next one.

    Also are all the URL's in your range actually URL's?

  8. #8
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Not working.

  9. #9
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    Can you upload a copy of your workbook with URL's?

  10. #10
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Sorry but I can not mention the links.

  11. #11
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    Hi, sorry as previously mentioned the web query part i dont have much experience with.
    If you can give me a bit more info as to what is happening i will try and solve it for you.
    Otherwise as i have answered your original question, you may be best to start a new thread regards to your web query problem.

  12. #12
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    I think this information is valuable. You got the problem right.
    I have kept the URLs in column A of sheet 1
    When I am running the above VBA code you wrote It is giving the same output in separate pages.
    The same output in every page is coming from fisrt URL.

    Thanks in advance.

  13. #13
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    try this

    Sub checkURL()
        Dim URL, MyRange As Range
        Set MyRange = Sheet1.Range("A1:A" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row)
        For Each URL In MyRange
            If URL.Value <> "" Then
                ActiveWorkbook.Worksheets.Add
                With ActiveSheet.QueryTables.Add(Connection:= _
                    "URL;http://www.website.com/" & URL & "secondpartofurl" _
                    , Destination:=Range("$A$1"))
                    .Name = "placeholder"
                    .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 = "16"
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .WebSingleBlockTextImport = False
                    .WebDisableDateRecognition = False
                    .WebDisableRedirections = False
                    .Refresh BackgroundQuery:=False
                End With
                Call DeleteAllQueries
            End If
        Next
    End Sub
    Sub DeleteAllQueries() 'breaks quieres 
        Dim qt As QueryTable
        Dim WSh As Worksheet
        
        For Each WSh In ThisWorkbook.Worksheets
        For Each qt In WSh.QueryTables
        qt.Delete
        Next qt
        Next WSh
    End Sub

  14. #14
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Call DeleteAllQueries didn't worked

  15. #15
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    Well i tried the code with 3 different URL address's and it it created 3 new worksheets and andded the data from each URL to seperate sheets.
    All worked perfectly ok.
    dont understand why yours would be any different.

    you did place this sub on the same sheet?

    Sub DeleteAllQueries() 'breaks quieres 
        Dim qt As QueryTable
        Dim WSh As Worksheet
        
        For Each WSh In ThisWorkbook.Worksheets
        For Each qt In WSh.QueryTables
        qt.Delete
        Next qt
        Next WSh
    End Sub

  16. #16
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Worked. Thank you very much. Using a different macro I want to get the different page data in one sheet now. And delete the others.

  17. #17
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    Hi, glad it finally worked.
    I dont have anymore time at the moment, so suggest you mark this as resolved and then start a new thread.

    Please add to reputation by using the star symbol.

  18. #18
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    I want to add another feature to this code: if a web link is not inactive let it go to next link or VBA will stop here.
    Last edited by maria.blue44; 03-13-2013 at 09:14 PM.

  19. #19
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: VBA URL In Range varies and can be any number of row

    Hi Maria, try this.
    if the web page is not found it will delete all query connections, exit sub and give message as to what URL was not found
    Sub checkURL()
        Dim URL, MyRange As Range
        Set MyRange = Sheet1.Range("A1:A" & Sheet1.Range("A" & Rows.Count).End(xlUp).Row)
        For Each URL In MyRange
            If URL.Value <> "" Then
                ActiveWorkbook.Worksheets.Add
                on error goto errorhandler
                With ActiveSheet.QueryTables.Add(Connection:= _
                    "URL;http://www.website.com/" & URL & "secondpartofurl" _
                    , Destination:=Range("$A$1"))
                    .Name = "placeholder"
                    .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 = "16"
                    .WebPreFormattedTextToColumns = True
                    .WebConsecutiveDelimitersAsOne = True
                    .WebSingleBlockTextImport = False
                    .WebDisableDateRecognition = False
                    .WebDisableRedirections = False
                    .Refresh BackgroundQuery:=False
                End With
                Call DeleteAllQueries
            End If
        Next
        Exit Sub
    errorhandler:
        Err.Clear
        Call DeleteAllQueries
        MsgBox URL & " Web page not found",vbinformation, "Error"
    End Sub
    
    Sub DeleteAllQueries() 'breaks quieres 
        Dim qt As QueryTable
        Dim WSh As Worksheet
        
        For Each WSh In ThisWorkbook.Worksheets
        For Each qt In WSh.QueryTables
        qt.Delete
        Next qt
        Next WSh
    End Sub
    Last edited by Sean Thomas; 03-14-2013 at 03:39 AM. Reason: missed a word out

  20. #20
    Forum Contributor
    Join Date
    09-19-2012
    Location
    NYC
    MS-Off Ver
    Excel 2010
    Posts
    137

    Re: 2# VBA URL In Range varies and can be any number of row

    You might want to use VBA help to learn about the sendkeys command for the active application. you will have to add your sendkeys code at the point in your script immediately after the point where the dialogues pop up. Sorry for such a general reply, but i don't have excel on this computer.

    Best,

    Willardio

  21. #21
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: 2# VBA URL In Range varies and can be any number of row

    You might be able to do something by automating IE or using XMLHTTP.

    Wthout the links it's kind of hard to help with that though.
    If posting code please use code tags, see here.

  22. #22
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: 2# VBA URL In Range varies and can be any number of row

    Sorry I can not share links.

  23. #23
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: 2# VBA URL In Range varies and can be any number of row

    The below part is not working can any body help me now ?

    Sub select()
    
    Dim IE As Object
    Dim sel
    
    
    Set IE = CreateObject("InternetExplorer.Application")
    
    With IE
    .Visible = True
    .navigate "http://www.website.com"
    While .Busy Or .readyState <> 4: DoEvents: Wend
    Set doc = .document
    End With
    
    With doc
    
    'Select Country
    
    Set sel = .getElementById("region")
    If Not sel Is Nothing Then
    sel.Value = "United Kingdom"
    '------------------------------------------------------------------Works upto here but the below part is not working
    
    Click.Button.getElementById ("go")
    
    'popup window for conformation
    
    Set sel = .getElementById("inner")
    If Not sel Is Nothing Then
    Click.Button.getElementById ("ok")
    End If
    
    End With
    
    End Sub

  24. #24
    Valued Forum Contributor Sean Thomas's Avatar
    Join Date
    03-25-2012
    Location
    HerneBay, Kent, UK
    MS-Off Ver
    Excel 2007,2016
    Posts
    971

    Re: 2# VBA URL In Range varies and can be any number of row

    Hi Maria,
    i have tried this code.
    As long as you list your URL's starting with www. this works fine.
    if you add your long URL's which take you direct to the window with the table on this will work

    if you need to go through the main page and then need send key entries, this is going to be very difficult.
    let me know how you get on. i dont have a great deal of time at the moment but let me know if you have problems.

    Sean
    Attached Files Attached Files

  25. #25
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    I want to add a Code block to add in the macro to consider every page format as text before performing this operation.

  26. #26
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    Cant I get the output in the same sheet ?

  27. #27
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: VBA URL In Range varies and can be any number of row

    I want a modified Code to add in the macro to consider every page format as text before performing this operation. Because some text is converting into numbers

  28. #28
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    80

    Re: 2# VBA URL In Range varies and can be any number of row

    each created sheet needed to be formatted as text as soon as it it created in code.

+ 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