Isoka
Isoka

Reputation: 31

How to run a vba macro on a specific worksheet without leaving the current worksheet

I would like to have a VBA code run on one, or more, specific worksheets of a workbook in Excel. But I do not wish to activate the worksheets where the code will run, I wish to have it done in the background and have the results shown on the current worksheet, where there will be a button do call the code. All the responses I have seen to such question require visiting the worksheet where the macro will run but I would like to avoid that. Perhaps putting the code inside a With statement will do the job? I have investigated it but cannot figure out all the dots in front of the lines. I tried to post the code here but I keep getting indentation errors so I will omit it for now.

Thanks in advance.

Upvotes: 1

Views: 6768

Answers (3)

James Ryan
James Ryan

Reputation: 21

You can try this:

dim x as variant    
set x = sheets("Sheet1")

Just change the sheet name based on what you have.

Upvotes: 2

Isoka
Isoka

Reputation: 31

Thanks guys! I ended up activating every sheet (was in a hurry). Best regards,

Upvotes: 0

kalyanbk
kalyanbk

Reputation: 153

You should be able to access any worksheet and any cell in any worksheet in the workbook through VBA.

val = ActiveWorkbook.worksheets("Sheet2").cells(row, col).Value

Upvotes: 3

Related Questions