+ Reply to Thread
Results 1 to 2 of 2

Macro runs while in focus of targeted workbook but out-of-range once I change of wb

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-17-2016
    Location
    Montreal
    MS-Off Ver
    2013
    Posts
    115

    Macro runs while in focus of targeted workbook but out-of-range once I change of wb

    Hey,

    I've been lurking the forums and the web but have yet to find how to solve this..
    The code as it stands looks like this, in a module:
    Sub my_Procedure()
    Dim lRow As Long
    Worksheets("MarketTrack").Activate
        lRow = Worksheets("MarketTrack").Range("D" & Rows.Count).End(xlUp).Row
        Worksheets("MarketTrack").Range("B" & lRow + 1).Value = CDate(Format(Now, "hh:mm"))
        Worksheets("MarketTrack").Range("D" & lRow + 1).Value = Worksheets("MarketTrack").Range("D9").Value
        Worksheets("MarketTrack").Range("F" & lRow + 1).Value = Worksheets("MarketTrack").Range("F9").Value
        Worksheets("MarketTrack").Range("H" & lRow + 1).Value = Worksheets("MarketTrack").Range("H9").Value
        Worksheets("MarketTrack").Range("J" & lRow + 1).Value = Worksheets("MarketTrack").Range("J9").Value
        Worksheets("MarketTrack").Range("L" & lRow + 1).Value = Worksheets("MarketTrack").Range("L9").Value
        Worksheets("MarketTrack").Range("N" & lRow + 1).Value = Worksheets("MarketTrack").Range("N9").Value
        Call timer 'starting timer again
    End Sub
    
    Sub timer()
        Application.OnTime Now + TimeValue("00:01:00"), "my_Procedure"
    End Sub
    This works as long as my MarketTracker.xlsm/MarketTrack(sheet) is in focus, though as-soon-as I change of workbook it throws me an error "out-of-range" (I'm assuming it's because the macro is trying to run the My_Procedure but it's no longer in the right place...) That said, all my workbooks (excel-files) are open at all times.

    Is there a way to get the Macro to always run on a specific workbook/worksheet in the background?

    Thanks for your time!

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Macro runs while in focus of targeted workbook but out-of-range once I change of wb

    - avoid selecting the sheet and the problem should vanish
    - used next row instead of last row - calculate once instead of several times (changed variable name to match)

    Try this method (untested) ..
    Sub my_Procedure()
    
    Dim nRow As Long
        With ThisWorkbook.Sheets("MarketTrack")
            nRow = .Range("D" & Rows.Count).End(xlUp).Row + 1           'calculate ONCE
            .Range("B" & nRow).Value = CDate(Format(Now, "hh:mm"))
            .Range("D" & nRow).Value = .Range("D9").Value
            .Range("F" & nRow).Value = .Range("F9").Value
            .Range("H" & nRow).Value = .Range("H9").Value
            .Range("J" & nRow).Value = .Range("J9").Value
            .Range("L" & nRow).Value = .Range("L9").Value
            .Range("N" & nRow).Value = .Range("N9").Value
        End With
         Call timer 'starting timer again
    End Sub
    
    Sub timer()
        Application.OnTime Now + TimeValue("00:01:00"), "my_Procedure"
    End Sub
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

+ 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. [SOLVED] Ensuring that VBA code runs on records in a targeted Excel Worksheet
    By patdools in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-27-2019, 05:08 PM
  2. Macro runs when header / footer change
    By Dturazza in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-26-2018, 05:12 AM
  3. Variables values change after macro runs
    By chriska416 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-26-2016, 06:47 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. Change cell value and macro runs
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-23-2007, 04:18 AM
  6. Cell value change runs a macro?
    By Steach91 in forum Excel General
    Replies: 6
    Last Post: 06-18-2006, 03:20 PM
  7. [SOLVED] Run macro when excel workbook gains focus
    By Perry in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-31-2006, 07:45 AM

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