Jones
Jones

Reputation: 1

How to run VBA code for particular worksheet(Only in sheet1) (Irrespective of the active sheet)?

I am a newbie for a VBA coding.I am building custom Stock screener. For that I want to print a stock prices at particular times to different cells for my strategy.I developed a code somehow.But I don't know how to make it work in a particular sheet(sheet 1) irrespective of active sheets. Following is the code which i pasted in a thisworkbook module.

Thanks in advance

Option Explicit


Private Sub Workbook_Open()
Call ScheduleTask
End Sub


Public Sub ScheduleTask()
Application.OnTime TimeValue("14:46:00"), "ThisWorkbook.Execute"

End Sub

Public Sub Execute()
Debug.Print "Executing task", Now
Range("D8:D57").Copy
Range("T8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, 
SkipBlanks:=False, Transpose:=False
Call ScheduleTask1

End Sub

Public Sub ScheduleTask1()

Application.OnTime TimeValue("14:47:00"),"ThisWorkbook.Execute1"

End Sub

Public Sub Execute1()

Debug.Print "Executing task", Now
Range("D8:D57").Copy
Range("U8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, 
SkipBlanks:=False, Transpose:=False
Call ScheduleTask2

End Sub

Public Sub ScheduleTask2()

Application.OnTime TimeValue("14:47:00"), "ThisWorkbook.Execute2"

End Sub

Public Sub Execute2()

Debug.Print "Executing task", Now
Range("D8:D57").Copy
Range("V8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, 
SkipBlanks:=False, Transpose:=False
Call ScheduleTask
End Sub`enter code here`

Upvotes: 0

Views: 1191

Answers (3)

Plutian
Plutian

Reputation: 2309

Specify the worksheet to be used for every Range statement like so:

Private Sub Workbook_Open()
Call ScheduleTask
End Sub


Public Sub ScheduleTask()
Application.OnTime TimeValue("14:46:00"), "ThisWorkbook.Dostuff"

End Sub

Public Sub Dostuff()
Dim Mysheet As Worksheet
Set Mysheet = ThisWorkbook.Worksheets("Enter sheet name here")
Debug.Print "Executing task", Now
Mysheet.Range("D8:D57").Copy
Mysheet.Range("T8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ScheduleTask1

End Sub

Public Sub ScheduleTask1()

Application.OnTime TimeValue("14:47:00"), "ThisWorkbook.Dostuff1"

End Sub

Public Sub Dostuff1()
Dim Mysheet As Worksheet
Set Mysheet = ThisWorkbook.Worksheets("Enter sheet name here")
Debug.Print "Executing task", Now
Mysheet.Range("D8:D57").Copy
Mysheet.Range("U8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ScheduleTask2

End Sub

Public Sub ScheduleTask2()

Application.OnTime TimeValue("14:47:00"), "ThisWorkbook.Dostuff2"

End Sub

Public Sub Dostuff2()
Dim Mysheet As Worksheet
Set Mysheet = ThisWorkbook.Worksheets("Enter sheet name here")
Debug.Print "Executing task", Now
Mysheet.Range("D8:D57").Copy
Mysheet.Range("V8").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Call ScheduleTask
End Sub

Also do not use Execute as a sub name as it is also a command and will cause clashes.

Edited with @Pᴇʜ 's suggestions (twice)

Upvotes: 0

Teamothy
Teamothy

Reputation: 2016

Also You can work with

dim wb as Workbook
dim SheetOne as Worksheet
dim RangeOne as Range

set wb = ThisWorkbook
set SheetOne = wb.Sheets("Sheet1")
set RangeOne = SheetOne.Range("D8:D57")

RangeOne.Copy SheetOne.Range("T8")

Upvotes: 1

Pᴇʜ
Pᴇʜ

Reputation: 57753

Specify a worksheet for every Range object. If you don't do that then VBA will use the ActiveSheet.

For example

Range("D8:D57").Copy

will copy from the ActiveSheet but

ThisWorkbook.Worksheets("Sheet1").Range("D8:D57").Copy

will copy from Sheet1 of the worbkook this code is in (represented by ThisWorkbook).

Upvotes: 2

Related Questions