Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 14th, 2007, 01:06 PM
Registered User
 
Join Date: May 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default QueryTables.Add... name changes

I have a procedure that loops through a list of hyperlinks on one worksheet (wsSource) and calls an ImportWebData routine. The ImportWebData procedure uses QueryTable.Add to grab the data, place it in a 2nd worksheet (wsTarget) and name the range holding the import data as "ImportData" (using .Name). Following the query, the mother routine calls "ProcessImportData" and passes wsTarget and .range("ImportData")

After ProcessImportData, the mother procedure deletes the "ImportData" name to re-use it on the next query.

Therein lies the problem. Each iteration of ImportWebData names the range holding the imported data as "ImportData_1", "ImportData_2", etc. (even though the name "ImportData" no longer exists.)

Here is my question followed by the sample code: How can I force the web query to always name the imported data range as "ImportData" (without the appended numbers)?

Code:
Sub GetAllWebData()
    Dim wsT As Worksheet
    Dim wsS As Worksheet
    Dim hLink As Hyperlink
    Dim sWebAddr As String
    Const ImportRangeName As String = "ImportData"
    Const iTableNumber As Integer = 9

    Set wsT = Worksheets("TargetSheet")
    Set wsS = Worksheets("SourceSheet")

    For Each hLink In wsS.Hyperlinks
        sWebAddr = hLink.Address
        ImportWebData wsS, wsT, sWebAddr, iTableNumber, ImportRangeName
        ProcessImportData wsT
        Range(ImportRangeName).Delete
    Next hLink
End Sub

Sub ImportWebData(wsSource As Worksheet, wsTarget As Worksheet, _
        sWebAddr As String, iTableNumber As Integer, _
        ImportRangeName As String)
    Dim qTab As QueryTable

    Set qTab = wsTarget.QueryTables.Add(Connection:="URL;" & sWebAddr, _
            Destination:=wsTarget.Range("A1"))
    With qTab
        .Name = ImportRangeName
        .FieldNames = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = False
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingAll
        .WebTables = "11,12,13"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
    Application.qTab.Refresh BackgroundQuery:=False
End Sub

Sub ProcessImportData(ws As Worksheet)
'   statements to evaluate, manipulate and make decisions based upon
'   imported data located in ws.range(ImportRangeName)
End Sub

Thanks ahead of time for your help.






Similar Threads
Thread Thread Starter Forum Replies Last Post
QueryTables.Add - Sql parameter question deviltheone Excel VBA 0 September 26th, 2008 11:49 AM
QueryTables nested loop Sherman McCoy Excel VBA 0 June 4th, 2008 02:37 PM
Add a CDATA section; add nodeset to CDATA section kssudhish XSLT 3 January 3rd, 2008 07:13 AM
Help for (with ActiveSheet.QueryTables.Add Connec) chintu4u Excel VBA 0 June 13th, 2006 01:11 AM
Add Push button or Check Box in outlook add-ins capdevs VS.NET 2002/2003 0 January 7th, 2006 08:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.