Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ron ron is offline
external usenet poster
 
Posts: 118
Default Assign Web Query Result to a Variable

I'm running the following web query and it works fine. As written it
pastes the web page onto the active sheet starting at cell A1. Is
there a way to bypass pasting the web page to the worksheet and
instead assign the web page HTML or text to a variable?..TIA, ron

' Connect to the site
With ActiveSheet.QueryTables.Add(Connection:= my_url,
Destination:=Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Assign Web Query Result to a Variable

Try this


My_Url = "URL;http://www.msn.com"

Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:=My_Url, _
Destination:=Range("A1"))

With MyQuery
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With


or this

My_Url = "http://www.msn.com"

Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:="URL;" & My_Url, _
Destination:=Range("A1"))

With MyQuery
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With


"ron" wrote:

I'm running the following web query and it works fine. As written it
pastes the web page onto the active sheet starting at cell A1. Is
there a way to bypass pasting the web page to the worksheet and
instead assign the web page HTML or text to a variable?..TIA, ron

' Connect to the site
With ActiveSheet.QueryTables.Add(Connection:= my_url,
Destination:=Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With

  #3   Report Post  
Posted to microsoft.public.excel.programming
ron ron is offline
external usenet poster
 
Posts: 118
Default Assign Web Query Result to a Variable

On Aug 16, 11:38*am, Joel wrote:
Try this

My_Url = "URL;http://www.msn.com"

Set MyQuery = ActiveSheet.QueryTables.Add( _
* *Connection:=My_Url, _
* *Destination:=Range("A1"))

* *With MyQuery
* * * * .BackgroundQuery = True
* * * * .TablesOnlyFromHTML = True
* * * * .Refresh BackgroundQuery:=False
* * * * .SaveData = True
* * End With

or this

My_Url = "http://www.msn.com"

Set MyQuery = ActiveSheet.QueryTables.Add( _
* *Connection:="URL;" & My_Url, _
* *Destination:=Range("A1"))

* *With MyQuery
* * * * .BackgroundQuery = True
* * * * .TablesOnlyFromHTML = True
* * * * .Refresh BackgroundQuery:=False
* * * * .SaveData = True
* * End With



"ron" wrote:
I'm running the following web query and it works fine. *As written it
pastes the web page onto the active sheet starting at cell A1. *Is
there a way to bypass pasting the web page to the worksheet and
instead assign the web page HTML or text to a variable?..TIA, ron


' Connect to the site
* * With ActiveSheet.QueryTables.Add(Connection:= my_url,
Destination:=Range("A1"))
* * * * .BackgroundQuery = True
* * * * .TablesOnlyFromHTML = True
* * * * .Refresh BackgroundQuery:=False
* * * * .SaveData = True
* * End With- Hide quoted text -


- Show quoted text -


Hi Joel...I had already assigned an URL to my_url earlier in my macro,
I just didn't show that step. My question is, rather than the web
page downloading to the "Destination Range" (A1 in this case), can it
be assigned to a variable instead?..Thanks, Ron
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Assign Web Query Result to a Variable

the best way is like this

set MyRange = Range("A1")

Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:=My_Url, _
Destination:=MyRange)



"ron" wrote:

On Aug 16, 11:38 am, Joel wrote:
Try this

My_Url = "URL;http://www.msn.com"

Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:=My_Url, _
Destination:=Range("A1"))

With MyQuery
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With

or this

My_Url = "http://www.msn.com"

Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:="URL;" & My_Url, _
Destination:=Range("A1"))

With MyQuery
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With



"ron" wrote:
I'm running the following web query and it works fine. As written it
pastes the web page onto the active sheet starting at cell A1. Is
there a way to bypass pasting the web page to the worksheet and
instead assign the web page HTML or text to a variable?..TIA, ron


' Connect to the site
With ActiveSheet.QueryTables.Add(Connection:= my_url,
Destination:=Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With- Hide quoted text -


- Show quoted text -


Hi Joel...I had already assigned an URL to my_url earlier in my macro,
I just didn't show that step. My question is, rather than the web
page downloading to the "Destination Range" (A1 in this case), can it
be assigned to a variable instead?..Thanks, Ron

  #5   Report Post  
Posted to microsoft.public.excel.programming
ron ron is offline
external usenet poster
 
Posts: 118
Default Assign Web Query Result to a Variable

On Aug 16, 1:32*pm, Joel wrote:
the best way is like this

set MyRange = Range("A1")

Set MyQuery = ActiveSheet.QueryTables.Add( _
* *Connection:=My_Url, _
* *Destination:=MyRange)



"ron" wrote:
On Aug 16, 11:38 am, Joel wrote:
Try this


My_Url = "URL;http://www.msn.com"


Set MyQuery = ActiveSheet.QueryTables.Add( _
* *Connection:=My_Url, _
* *Destination:=Range("A1"))


* *With MyQuery
* * * * .BackgroundQuery = True
* * * * .TablesOnlyFromHTML = True
* * * * .Refresh BackgroundQuery:=False
* * * * .SaveData = True
* * End With


or this


My_Url = "http://www.msn.com"


Set MyQuery = ActiveSheet.QueryTables.Add( _
* *Connection:="URL;" & My_Url, _
* *Destination:=Range("A1"))


* *With MyQuery
* * * * .BackgroundQuery = True
* * * * .TablesOnlyFromHTML = True
* * * * .Refresh BackgroundQuery:=False
* * * * .SaveData = True
* * End With


"ron" wrote:
I'm running the following web query and it works fine. *As written it
pastes the web page onto the active sheet starting at cell A1. *Is
there a way to bypass pasting the web page to the worksheet and
instead assign the web page HTML or text to a variable?..TIA, ron


' Connect to the site
* * With ActiveSheet.QueryTables.Add(Connection:= my_url,
Destination:=Range("A1"))
* * * * .BackgroundQuery = True
* * * * .TablesOnlyFromHTML = True
* * * * .Refresh BackgroundQuery:=False
* * * * .SaveData = True
* * End With- Hide quoted text -


- Show quoted text -


Hi Joel...I had already assigned an URL to my_url earlier in my macro,
I just didn't show that step. *My question is, rather than the web
page downloading to the "Destination Range" (A1 in this case), can it
be assigned to a variable instead?..Thanks, Ron- Hide quoted text -


- Show quoted text -


Thanks again for the reply Joel. If I understand your code, it will
paste the content of the "my_url" web page onto the active sheet
starting at the position defined by the variable "MyRange". I don't
want the results of the web query to be pasted into a worksheet.
Rather, I would like the result of the web query to be assigned to a
variable so that I can parse the variable with tools like InStr and
extract the data I need. Is there a way to avoid pasting the results
of a web query into a worksheet and rather assign the results to a
variable?

I know this can be dome using other web VBA approaches such as "GET/
POST" or "Set ie = CreateObject("InternetExplorer.Application")", but
can the web query method be used in this manner and avoid pasting data
into a worksheet?..TIA, Ron


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Assign Web Query Result to a Variable

Belwo is code I wrote a few weeks ago. It takes a couple of minutes to run
so let it sit. I put the classname, tagname, innertext, and innerhtml into a
worksheet so you can see what is available. You don't need to write this
data to a worksheet but it is useful when you are debugging. You can also
see the code by going to an Internet explorer and use menu View - Source
which will open a notepad window of the webpage. There is a lot more that
you can do. I can send you my entire code so yo can see what I actually did.


Sub GetDealers()
'Dim PageNumber As Object
CR = Chr(13)
LF = Chr(10)

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True

URL = "http://www.nissanusa.com/apps/dealerlocator"
Request = "?zipCode=07508&tool=Home.Locator"

'get web page
IE.Navigate2 URL & Request
Do While IE.readyState < 4
DoEvents
Loop

'get search button
Set but = IE.document.getElementById("mainSearchButton")
'put distance in listbox on webpage
Set radius = IE.document.getElementById("radius")
radius.Value = "100"


'search again a larger distance
'Select Search button and activate
but.Select
but.Click
Set SearchResults = IE.document.getElementById("searchResults")

On Error Resume Next ' Defer error handling.
Do
Err.Clear
Set PageNumber = IE.document.getElementById("pageNumber")
Pages = PageNumber.Value
DoEvents
Loop While Err.Number < 0
On Error GoTo 0

With Sheets("Sheet1")
RowCount = 1
For Each itm In IE.document.all
.Range("A" & RowCount) = itm.tagname
.Range("B" & RowCount) = itm.classname
.Range("C" & RowCount) = itm.innertext
.Range("D" & RowCount) = itm.innerhtml
RowCount = RowCount + 1
Next itm

End With
End Sub


"ron" wrote:

On Aug 16, 1:32 pm, Joel wrote:
the best way is like this

set MyRange = Range("A1")

Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:=My_Url, _
Destination:=MyRange)



"ron" wrote:
On Aug 16, 11:38 am, Joel wrote:
Try this


My_Url = "URL;http://www.msn.com"


Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:=My_Url, _
Destination:=Range("A1"))


With MyQuery
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With


or this


My_Url = "http://www.msn.com"


Set MyQuery = ActiveSheet.QueryTables.Add( _
Connection:="URL;" & My_Url, _
Destination:=Range("A1"))


With MyQuery
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With


"ron" wrote:
I'm running the following web query and it works fine. As written it
pastes the web page onto the active sheet starting at cell A1. Is
there a way to bypass pasting the web page to the worksheet and
instead assign the web page HTML or text to a variable?..TIA, ron


' Connect to the site
With ActiveSheet.QueryTables.Add(Connection:= my_url,
Destination:=Range("A1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SaveData = True
End With- Hide quoted text -


- Show quoted text -


Hi Joel...I had already assigned an URL to my_url earlier in my macro,
I just didn't show that step. My question is, rather than the web
page downloading to the "Destination Range" (A1 in this case), can it
be assigned to a variable instead?..Thanks, Ron- Hide quoted text -


- Show quoted text -


Thanks again for the reply Joel. If I understand your code, it will
paste the content of the "my_url" web page onto the active sheet
starting at the position defined by the variable "MyRange". I don't
want the results of the web query to be pasted into a worksheet.
Rather, I would like the result of the web query to be assigned to a
variable so that I can parse the variable with tools like InStr and
extract the data I need. Is there a way to avoid pasting the results
of a web query into a worksheet and rather assign the results to a
variable?

I know this can be dome using other web VBA approaches such as "GET/
POST" or "Set ie = CreateObject("InternetExplorer.Application")", but
can the web query method be used in this manner and avoid pasting data
into a worksheet?..TIA, Ron

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing result from Access query to Excel but the result only c. Edwin Excel Discussion (Misc queries) 0 March 16th 06 01:36 AM
put query result into variable andy[_6_] Excel Programming 1 October 24th 05 07:39 AM
how do I assign a symbol to the result of a part of a nested IF s. Lee Mitchell Excel Worksheet Functions 1 March 30th 05 05:32 PM
assign result of ExecuteExcel4Macro to an array keepitcool Excel Programming 6 May 28th 04 03:02 PM
Passing SQL Query Result Into A Variable Elliot[_2_] Excel Programming 4 January 9th 04 09:55 AM


All times are GMT +1. The time now is 02:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"