+ Reply to Thread
Results 1 to 3 of 3

Script runs on other workbooks

  1. #1
    Registered User
    Join Date
    08-04-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    3

    Script runs on other workbooks

    I have a function that does some stuff to a bunch of cells every 15 seconds.

    EventMacro is called when the workbook in question is opened.
    The script does its job great on the workbook its supposed to run on.

    But the problem is this: if I have the book with this script open, then hit ctrl+n and open a new workbook, I suddenly get a "subscript out of range error" from my updateDates function. Whats causing this? why does my macro want to run on other sheets?
    Thanks for the advice.

    Sub EventMacro()
    Call updateDates
    alertTime = Now + TimeValue("00:00:15")
    Application.OnTime alertTime, "EventMacro"
    End Sub

    Private Sub updateDates()

    Dim ws As Worksheet
    Set ws = Worksheets("Data")

    If Not (ws Is Nothing) Then


    For Counter = 10 To 1000
    If (IsEmpty(ws.Cells(Counter, 2).Value)) Then 'if no date entered
    If Not IsEmpty(ws.Cells(Counter, 4)) Then 'and a value is in the cell
    ws.Cells(Counter, 2).Value = Now 'enter today's date
    Else
    GoTo last 'stops the script from running too far, if both date and data are blank skip out of the loop
    End If
    End If
    Next Counter

    End If

    last:
    End Sub

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Script runs on other workbooks

    Hi,

    Set ws = Worksheets("Data") works on the 'Active Workbook', which is the workbook you just opened.

    If the code you are running is in the file that you want to update, then you probably want:
    Please Login or Register  to view this content.
    'ThisWorkbook' refers to the workbook that contains the code that is running.

    Lewis

  3. #3
    Registered User
    Join Date
    08-04-2014
    Location
    United States
    MS-Off Ver
    2007
    Posts
    3

    Re: Script runs on other workbooks

    Thanks!! That did the trick.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Simple Script to hide rows - runs slow
    By davecox2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-02-2014, 01:35 PM
  2. Script works in Debugger but doesn't when Macro runs
    By sqeekypotato in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-02-2009, 11:31 AM
  3. If then statement to control when the script runs
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-29-2009, 06:42 PM
  4. Mouse Focus Changes After Script Runs
    By ClivePoole in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-12-2008, 02:45 AM
  5. Script that edits the text printed on the button that runs the script
    By petalred in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2008, 02:41 PM

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