Worksheet Selection Change - Limiting to Specific Worksheet

sam89665

New Member
Joined
May 17, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi,

I currently use a combination of VBA and conditional formatting to highlight the row the cursor is on for a specific worksheet.

VBA Sheet Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Calculate
End Sub

Conditional Formatting:
=CELL("row")=CELL("row",A1)

What I'm finding annoying is when I make a selection change on a sheet in a different workbook and then go back to this spreadsheet the row is no longer highlighted, due to me changing the selection in the other workbook.

Therefore, my question is, can I amend the VBA code to restrict it to that specific workbook/worksheet only (i.e. so that it ignores selection changes in other workbooks)?

Many thanks!

Sam
 

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.
Sorry, can't replicate your problem. As per your explanation the highlighting in first workbook doesn't disappear whatever I do in the second workbook.
Please attach a demo workbook with problem.
 
Upvote 0
Your conditional formatting rule appears to be flawed. If you don't include a cell reference in the CELL function, it will be applied to the last altered cell in any open workbook, which is not what you want. I think you'd be better off with a defined name that stores the current row number, and use that in the formula - eg =ROW()=rowNum
 
Upvote 0
Your conditional formatting rule appears to be flawed. If you don't include a cell reference in the CELL function, it will be applied to the last altered cell in any open workbook, which is not what you want. I think you'd be better off with a defined name that stores the current row number, and use that in the formula - eg =ROW()=rowNum

Thanks for the reply. So if I understand correctly, I should define a name as follows:


1621416423095.png



But how would I adapt the conditional formatting rule to incorporate this? The current rule is:

=CELL("row")=CELL("row",A1)

Appreciate your help, thanks.
 
Upvote 0
No, you'd define the rowNum name using a specific number (eg =1) and then your CF formula is =ROW()=rowNum. Your SelectionChange code updates the rowNum name and then recalculates the sheet.
 
Upvote 0
No, you'd define the rowNum name using a specific number (eg =1) and then your CF formula is =ROW()=rowNum. Your SelectionChange code updates the rowNum name and then recalculates the sheet.

Ok, so I've changed the name to this:


1621418421115.png



and the CF to this:


1621418446362.png



But that's not working. The formatting is stuck on Row 1, no matter where else I click. I'm obviously doing something wrong?

Once again, appreciate the help.
 
Upvote 0
I think you missed the part where I said "Your SelectionChange code updates the rowNum name". For example:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ThisWorkbook.Names("rowNum").RefersTo = "=" & Target.Row
Target.Calculate
End Sub
 
Upvote 0
Solution
I think you missed the part where I said "Your SelectionChange code updates the rowNum name". For example:

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ThisWorkbook.Names("rowNum").RefersTo = "=" & Target.Row
Target.Calculate
End Sub

I did, but wouldn't have known how to do that anyway as I'm new to VBA.

It's working now - so I'll try using that going forwards.

Many thanks for the help, and sorry for my dumb questions.
 
Upvote 0
Glad we could help, and there were no dumb questions there, believe me. :)
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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