1
votes

I have recorded a Macro (name - updatetime) to refresh world time & copy paste it to the next cell. The macro is running automatically after 50 sec.

During this time, if I open another Workbook (Name- IV.xlsx) and do some other work, after completion of 50 sec the Macro (name - updatetime) is affecting the other workbook (Name- IV.xlsx)

I have one Excel workbook named time.xlsm

  1. in that there are 2 sheets, Sheet 1 & Sheet 2

  2. in sheet 1 I have a web link to update world time

  3. in sheet 2 I have some country names in 1 column & in the next column a Vlookup function to update the time from sheet 1

  4. Macro - after every 50 sec this time will get copy/pasted in next column and sheet 1 is refreshed

The code:

Sub timeupdate()

     'timeupdate Macro

     'Keyboard Shortcut: Ctrl+t

        Selection.Copy
        Range("D2").Select
        Application.CutCopyMode = False
        Range("C2:L37").Select
        Selection.Copy
        Range("D2").Select
        ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
            IconFileName:=False
            Call test
  End Sub

  Sub test()
     Application.OnTime Now + TimeValue("00:00:50"), "timeupdate"
  End Sub

I just want to run the macro on one specific worksheet, even after having opened a different Excel workbook.

1

1 Answers

0
votes

First rule of VBA -Never use Select

Your code needs to specify the sheet and workbook it wants to affect

With ThisWorkbook.Worksheets("name of your sheet goes here")
    .Range("C2:L37").Copy
    .Range("D2").PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, IconFileName:=False
End With
    Call test
End Sub