Home > Forum Home > Automating Data Analysis Tasks > Run Macro IF Specific URL Changes Share

Run Macro IF Specific URL Changes

Excel Help for Run Macro If Specific Url Changes in Automating Data Analysis Tasks


Forum TopicPost Reply Login

Run Macro If Specific Url Changes

Rate this:
(3/5 from 1 vote)
ConfusedFire Macro Only IF Specific URL Address Change Is Requested From WebBrowser Object?

I am using a WebBrowser Sub that runs a macro when the URL Address changes.

The problem is it fires the macro each time the browser is refreshed because it changes (or refreshes back to itself and runs the macro).

I would like the macro only to run ONLY when a SPECIFIED URL is requested.

The code beneath works great.
All are welcome to try this code for yourself (click on the developer tab>insert tools>more tools>microsoft web browser>draw browser>paste code beneath in vba)

VBA Code:
Private Sub Worksheet_Activate()

WebBrowser1.Navigate  “[http://www.google.com/]”                         ‘this is home page I want the web browser to start with but NOT run the macro

End Sub

Private Sub WebBrowser1_TitleChange(ByVal sText As String)

MsgBox “Macro Runs Once Web Page Change Occurs”                 ‘this code works, but the problem is I need the macro to only launch with a specific URL only

End Sub

The only problem is (as stated above), it launches each time the page is opened or refreshed.  I am in need of a way to code this the right way so that it only runs the macro if a specified URL address is requested only.  This way the macro will not fire up at start up or when the page is refreshed, but yet only when the specified page is loaded with the web browser.
VBA Code:
Private Sub WebBrowser1_TitleChange(ByVal sText As String)

 If address.Equals("http://www.google.com") Then

MsgBox "Macro Runs Because Of Specified URL"                             ‘I would like the MACRO to RUN because it EQUALS the SPECIFIED URL

        If address.Equals("http://www.YAHOO.com") Then

Do Nothing and Exit Sub                                                                                     ‘I would like it to DO NOTHING and EXIT SUB if the page changes to YAHOO  or anything else for the matter

End Sub

Can anyone help me with the correct working syntax for this code?
 Posted by on
 
Replies - Displaying 1 to 3 of 3Order Replies By: Most recent | Chronological | Highest Rated
Oops
Rate this:
(3/5 from 1 vote)
Well, the VBA code is running in the Excel application which means that it can access all of the objects and methods assocated with Excel including the cell value.  The WebBrowser object is an ActiveX object that is also being accessed from the VBA project; therefore both can be managed from the VBA code.
 Excel Business Forums Administrator
 Posted by on
Confused
Rate this:
(3/5 from 1 vote)
How would my web browser object communicate with the cell?
 Posted by on
Shocked
Rate this:
(3/5 from 1 vote)
The easiest way would be to store the request URL in a cell somewhere and interrogate this in your macro.  A cell could contain the URL and a separate named cell a formula which returns 1 or 0 depending on whether to run the macro. e.g. =IF(A1="http://www.google.com", 1, 0).  The cell could be named "blrun" for example.

In the macro, you then add the conditional statement.

VBA Code:
If Range("blrun") = 1 Then
  'run the routine
Else
  Exit Sub
End If
 Excel Business Forums Administrator
 Posted by on
 Displaying page 1 of 1 

Find relevant Excel templates and add-ins for Run Macro IF Specific URL Changes in the