Macro runs on active sheet instead of specified

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
I am want to run a macro on a specific sheet by pressing a command button that is in another sheet. The code I have is:

Sub FormatData ()
Dim Lr As Long
Dim ws As Worksheet
Set ws = Sheets("NewData")
Application.ScreenUpdating = False

the rest of the code after that.

The problem is that the macro runs on the active sheet (ie the sheet the button is on). I've looked at all the previous posts about this but, as there seems to be a different answer every time this question is asked, I am very confused as to how to correct/change my code so that the macro runs on the correct sheet.

Any help would be appreciated.
 
There is a fair bit of recorded code to plow through to tidy up which I won't have time to do tonight but might have a go at it if I get bored during the week :biggrin:

But in the meantime it is as I said, you are setting up a variable and then not using it so all the code is acting on the activesheet.

All the time you are using Select and Selection then the sheet has to be the active sheet as Select can only be used on the activesheet.

In the meantime where you have put in the new code you are duplicating the task so change

Code:
Dim Lr As Long
Dim ws As Worksheet
Set ws = Sheets("NewData")
Application.ScreenUpdating = False
Dim ShtName As String
ShtName = "NewData"


Sheets(ShtName).Activate
to just
Code:
Dim Lr As Long
Dim ws As Worksheet
Set ws = Sheets("NewData")
Application.ScreenUpdating = False
ws.Activate

and if nobody else posts anything I will try and post some updated code during the week.

Thank you. Much appreciated - I am learning all the time
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,223,958
Messages
6,175,633
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top