+ Reply to Thread
Results 1 to 5 of 5

Run Macro If Cell Value = 1

Hybrid View

  1. #1
    Registered User
    Join Date
    01-23-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    50

    Run Macro If Cell Value = 1

    Hi

    I have a spreadsheet with several worksheets and I need to run a macro to copy a webpage and paste it into one of the worksheets. This I am doing fine already. What I would like to do is have the macro run automatically if a cell equals a certain value.

    The first Worksheet is named "Data" and if the value of cell AE1 = 1 I want the macro to activate. The second worksheet is named "Arg" and it is here that i want the webpage pasted.
    Here is the code I have:

    Private Sub Worksheet_Calculate()
    If [AE3] = 1 Then
         Sheets("Arg").Select
         Range("G3:BV1000") = "" ' erase previous data
        Range("G3").Select
         
        Set IE = CreateObject("InternetExplorer.Application")
        With IE
            .Visible = True
            .Navigate "http://www.google.com" ' should work for any URL
            Do Until .ReadyState = 4: DoEvents:  Loop
            End With
             
            IE.ExecWB 17, 0 '// SelectAll
            IE.ExecWB 12, 2 '// Copy selection
            ActiveSheet.Paste
            Range("G3").Select
            IE.Quit
            IE.Quit ' just to make sure
       
         Sheets("Data").Select
        Range("A1").Select
    End If
    End Sub
    what have I done wrong? Any help would be appreciated.

    Doug
    Last edited by boohah; 05-25-2010 at 09:50 AM. Reason: Code Tags

  2. #2
    Registered User
    Join Date
    01-23-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Run Macro If Cell Value = 1

    I have tried it a different way with the following code and still no luck:

    Private Sub Worksheet_Calculate()
    ' change RangeName to the range you need
    If Range("AE3").Value = 1 Then
    Sub Test()
        Dim IE As Object
         
         Sheets("Arg").Select
         Range("G3:BV1000") = "" ' erase previous data
        Range("G3").Select
         
        Set IE = CreateObject("InternetExplorer.Application")
        With IE
            .Visible = True
            .Navigate "http://www.leagueday.com/latest.asp?league=argentina" ' should work for any URL
            Do Until .ReadyState = 4: DoEvents:  Loop
            End With
             
            IE.ExecWB 17, 0 '// SelectAll
            IE.ExecWB 12, 2 '// Copy selection
            ActiveSheet.Paste
            Range("G3").Select
            IE.Quit
            IE.Quit ' just to make sure
            
         Sheets("Data").Select
        Range("A1").Select
        End Sub
    End If
    End Sub
    Any help would be appreciated

  3. #3
    Forum Contributor wamp's Avatar
    Join Date
    11-10-2008
    Location
    Norway
    MS-Off Ver
    97, 2000 & 2003 (diff. workstations)
    Posts
    184

    Re: Run Macro If Cell Value = 1

    in "ThisWorkbook" add the following code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Sheets("Data").Range("AE1").Value = 1 Then   ' specify the cell you want to activate your macro here
    Run "Test"
    End If
    End Sub
    then in a module, insert your macro. for example
    Sub Test()
        Dim IE As Object
         
         Sheets("Arg").select
         Range("G3:BV1000").clearcontents
         Range("G3").Select
         
        Set IE = CreateObject("InternetExplorer.Application")
        With IE
            .Visible = True
            .Navigate "http://www.leagueday.com/latest.asp?league=argentina" ' should work for any URL
            Do Until .ReadyState = 4: DoEvents:  Loop
            End With
             
            IE.ExecWB 17, 0 '// SelectAll
            IE.ExecWB 12, 2 '// Copy selection
            ActiveSheet.Paste
            Range("G3").Select
            IE.Quit
            IE.Quit ' just to make sure
            
         Sheets("Data").Select
        Range("A1").Select
        End Sub
    End If
    End Sub
    Last edited by wamp; 05-25-2010 at 03:10 AM.
    _______________________________________________
    Remember:

    Read the rules!

    Use code tags! Place [CODE] before the first line of code and [/CODE] after the last line of code.

    Mark your post [SOLVED] if it has been answered satisfactorily.
    To do this, click EDIT in your original post, click GO ADVANCED and set the PREFIX. It helps everybody!

    If you are approve (or disapprove) of any members response to your problem, please click the star icon underneath their post and comment. It matters.

  4. #4
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Run Macro If Cell Value = 1

    I think you'd be better off using a webquery (menubar/data/external data, webquery). one of its properties is the refreshtime.
    Last edited by snb; 05-25-2010 at 11:35 AM.

  5. #5
    Registered User
    Join Date
    01-23-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    50

    Re: Run Macro If Cell Value = 1

    Thanks snb, i think i am good with what you have said already. Thank you for your help

    Doug

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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