if activecell not in range

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,060
Hi, can someone please help.

I am after a script that for instance, if the activecell is not within range A1:B500 then it will color the cell yellow.

If the activecell is within the range then it will color the cell red.

I do not want to use the worksheet selection change event as I want to be able to run the script in any worksheet at any time, the color of the cell yellow is just an example this is not actually what my script will do but if someone can help with the basics then I will modify it to suite.

Thanks :LOL:
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Jaye7.

When you say activecell does that mean the activecell in whichever worksheet happens to be active or the active cell in a specific worksheet ?
 
Upvote 0
Hi, can someone please help.

I am after a script that for instance, if the activecell is not within range A1:B500 then it will color the cell yellow.

If the activecell is within the range then it will color the cell red.

I do not want to use the worksheet selection change event as I want to be able to run the script in any worksheet at any time, the color of the cell yellow is just an example this is not actually what my script will do but if someone can help with the basics then I will modify it to suite.

Thanks :LOL:

Perhaps a Workbook-SheetSelectionChange Event?

Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Not Intersect(Target, Range("A1:B500")) Is Nothing Then

        Target.Interior.ColorIndex = 3
Else

        Target.Interior.ColorIndex = 6
End If
End Sub
 
Upvote 0
Hi Jaafer, yes it will be the activecell within whatever worksheet that I am in at the time, the cell that is selected in the worksheet.

John, thanks for the script, however as I am using the script within many different worksheets the worksheet selection change is not what I want as I would have to paste it into every single sheet of every workbook that I am using the script in and that would be painful.
 
Upvote 0
Hi Jaafer, yes it will be the activecell within whatever worksheet that I am in at the time, the cell that is selected in the worksheet.

John, thanks for the script, however as I am using the script within many different worksheets the worksheet selection change is not what I want as I would have to paste it into every single sheet of every workbook that I am using the script in and that would be painful.

I haven't checked John's code (logic, error handling etc) but he did provide you with a workbook level event handler (ie. Workbook_SheetSelectionChange) which would go in the ThisWorkbook code module. You would not need to paste it into every sheet of every workbook; it will cover all the sheets within that workbook.

If you require something at an application level (ie. all workbooks) then the code will need a little tweaking.
 
Upvote 0
Thanks Colin, I should have read John's script properly, however I want the script to not have to rely on selection change as I want to run the script as a normal macro when I click on the macro, I don't want the worksheet to change automatically every time I am within the range as some worksheets I don't want to change at all.
I don't know how to explain exactly what I am after, but just that it relies upon myself executing the code rather than the selection change executing it as it could cause majorly hassles on work sheets that I don't want changed.
As stated the cell colour thing is just an example it will actually be running a lot of code which could have devastating effects if it is activated in the wrong worksheet.
Thanks
 
Upvote 0
Hi,

I understand. You could just have a toolbar button (or something on the ribbon if using XL2007+) with a bit of VBA code attached which will check the activesheet whenever you click on it?
 
Upvote 0
Colin,

I am using 2003 and really want a macro that will check if the activecell is within the specified range, I want to use it for various scripts and different ranges for different projects.
 
Upvote 0
Yes, I'd say a toolbar button would be ideal for this? It can sit there on your toolbar all the time and, whenever you click on it (ie. click on the button), it will call a procedure which will do whatever you code it to?
 
Upvote 0
Colin,

It would be great for calling one procedure, however I will be using multiple procedures and also my files are in different folders on different computers, i.e. at work it is in one folder and at home it is in a different folder which buttons don't accomodate for, I am after an easy solution that I can use.
 
Upvote 0

Forum statistics

Threads
1,215,183
Messages
6,123,522
Members
449,103
Latest member
Michele317

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