Macro doesn't run in full from button control?

adambc

Active Member
Joined
Jan 13, 2020
Messages
373
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I have a macro that does a number of updates to my source data and finally sorts by column A and highlights duplicates in column A using this code ...

VBA Code:
'Sort by ID

Dim rg As Range: Set rg = Range("A2:A" & lRow)
rg.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes

'Highlight duplicate IDs

'clear any existing conditional formatting
rg.FormatConditions.Delete

'identify duplicate values in rg
Dim uv As UniqueValues: Set uv = rg.FormatConditions.AddUniqueValues
uv.DupeUnique = xlDuplicate

'apply conditional formatting to duplicate values
uv.Interior.Color = vbCyan

It runs perfectly from the VBA "console", but when I assign the macro to a control button, this final part of the code doesn't run (the previous code runs fine!) ...

Any ideas?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
A couple of things. "lRow" has not been set and you need to specify the sheet...
Code:
Sheets("Sheet1").Range("A2:A" & lRow)
HTH. Dave
 
Upvote 0
Solution
A couple of things. "lRow" has not been set and you need to specify the sheet...
Code:
Sheets("Sheet1").Range("A2:A" & lRow)
HTH. Dave
lRow is set higher up in the code - do I need to set it again?
 
Upvote 0
FIXED!

I needed to activate the Worksheet where the data is - even though the rest of the code had already executed on the correct WS - strange!!!
 
Upvote 0
FIXED!

I needed to activate the Worksheet where the data is - even though the rest of the code had already executed on the correct WS - strange!!!
The reason it worked when you activated the worksheet is you originally used the Range object without its parent, and the parent is set to the active sheet as default. I have changed the marked solution post because @NdNoviceHlp provided the correct method and actually code to make it work correctly. This thread will be more helpful to the future readers in this way.

When you use the Range object with its parent then you don't need to activate the worksheet but directly refer to the worksheet that needs to be used. It is the best practice while creating applications in VBA.
VBA Code:
Sheets("Sheet1").Range("A2:A" & lRow)
 
Upvote 0
The reason it worked when you activated the worksheet is you originally used the Range object without its parent, and the parent is set to the active sheet as default. I have changed the marked solution post because @NdNoviceHlp provided the correct method and actually code to make it work correctly. This thread will be more helpful to the future readers in this way.

When you use the Range object with its parent then you don't need to activate the worksheet but directly refer to the worksheet that needs to be used. It is the best practice while creating applications in VBA.
VBA Code:
Sheets("Sheet1").Range("A2:A" & lRow)
Duh, I knew that and forgot!!!

Thanks for reminding me …
 
Upvote 0

Forum statistics

Threads
1,215,157
Messages
6,123,341
Members
449,097
Latest member
thnirmitha

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