Worksheet Selection Change - Limiting to Specific Worksheet

sam89665

New Member
Joined
May 17, 2021
Messages
4
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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

rollis13

Active Member
Joined
Jul 30, 2012
Messages
486
Office Version
  1. 2016
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,809
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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
 

sam89665

New Member
Joined
May 17, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,809
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

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.
 

sam89665

New Member
Joined
May 17, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,809
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

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
 
Solution

sam89665

New Member
Joined
May 17, 2021
Messages
4
Office Version
  1. 365
Platform
  1. Windows
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.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,809
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Glad we could help, and there were no dumb questions there, believe me. :)
 

Forum statistics

Threads
1,148,145
Messages
5,745,049
Members
423,917
Latest member
Frank1931

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
Top