Highlight Cell Range

micha3l87

New Member
Joined
Apr 15, 2017
Messages
13
Hey Everyone,

I was hoping someone would be able to help me with what I think will be pretty simple.

When the user clicks a cell in column A, take that cells value subtract 10 and highlight that range in the column.

For example if I click in cell A21 (3113)-(10) = 3103
So highlight A21 to A17

I was going to upload my workbook, but I can't seem to find a attachment button
thank you for any help!
 

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.
Put the following code in the events of the sheet.
It will perform the subtraction.
The highlight part is missing because I do not understand why of A21 to A17. I understand A21, but why until A17?


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 Then
        If Target.Count > 1 Then Exit Sub
        Target.Value = Target.Value - 10
        Target.Interior.ColorIndex = 6
    End If
End Sub

SHEET EVENT

Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Last edited:
Upvote 0
RowColumn A
163104
173105
183106
193109
203111
213114
223117

<tbody>
</tbody>


Your code was subtracting 10 from the active cell and turning it yellow not exactly what I was wanting lol
I'm trying to select a row in column A, that value -10 will determine what the range is so in this example Cell A22 is 3117 - 10 so highlight cells A22-A18





Put the following code in the events of the sheet.
It will perform the subtraction.
The highlight part is missing because I do not understand why of A21 to A17. I understand A21, but why until A17?


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 1 Then
        If Target.Count > 1 Then Exit Sub
        Target.Value = Target.Value - 10
        Target.Interior.ColorIndex = 6
    End If
End Sub

SHEET EVENT

Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.
 
Last edited:
Upvote 0
Work sheet event is used.


Range used is A16:A22 . Change if required.
Code
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


Dim rng As Range, Cel As Range


Set rng = Range("A16:A22")
rng.Interior.Pattern = xlNone


If Not Intersect(Target, rng) Is Nothing Then


    For Each Cel In rng
        If Cel.Address <> Target.Address And Cel < Target And Cel >= Target - 10 Then
        Cel.Interior.Color = 65535
        End If
    Next Cel
End If


End Sub
To paste the code:
Right click on sheet tab --> View code
VBA window opens
Paste the code
Close the window.
 
Upvote 0
RowColumn A
163104
173105
183106
193109
203111
213114
223117

<tbody>
</tbody>


Your code was subtracting 10 from the active cell and turning it yellow not exactly what I was wanting lol
I'm trying to select a row in column A, that value -10 will determine what the range is so in this example Cell A22 is 3117 - 10 so highlight cells A22-A18

I'm sorry, obviously I understood something else.
Try this

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim r As Range
    Set r = Range("A16", Range("A" & Rows.Count).End(xlUp))
    If Target.Count > 1 Then Exit Sub
    If Not Intersect(Target, r) Is Nothing Then
        r.Interior.ColorIndex = xlNone
        c = Application.Match(Target.Value - 10, r, 1)
        If IsError(c) Then c = r(1).Row Else c = r.Cells(c).Row
        Range(Cells(c, "A"), Cells(Target.Row, "A")).Interior.ColorIndex = 6
    End If
End Sub
 
Upvote 0
Hey thats awesome! It works how I imagined, sometimes its wrong though, sometimes it highlights over 10
 
Last edited:
Upvote 0
I can fix it, but you have to be clearer with the example, explain what data you have, which cell you select, what the macro is doing and what should be the right thing to do.
 
Upvote 0
The hours in Column A are not 1,2,3,4,5,6 they jump somtimes more then 3 hours at a time.
The macro works perfectly when the selected sell -10 matches a a cell above it. So if you were to select 3270 it would highlight to 3260 perfectly but if there was not a 3260 in the column it will pass it instead of ending in the cell before.

Ideally if 3270 was selected and there isn't a perfect number like 3260 in the list it would stop in this case at 3261 not going over the 10

hopefully that makes sense?

3258
3261
3264
3265
3267
3269
3270
3271
3275

<tbody>
</tbody>
 
Upvote 0
The hours in Column A are not 1,2,3,4,5,6 they jump somtimes more then 3 hours at a time.
The macro works perfectly when the selected sell -10 matches a a cell above it. So if you were to select 3270 it would highlight to 3260 perfectly but if there was not a 3260 in the column it will pass it instead of ending in the cell before.

Ideally if 3270 was selected and there isn't a perfect number like 3260 in the list it would stop in this case at 3261 not going over the 10

hopefully that makes sense?

3258
3261
3264
3265
3267
3269
3270
3271
3275

<tbody>
</tbody>

Their examples are confusing.
In your previous example, you have this:
You select 3117 (A22), 3117-10 = 3107, but the 3107 does not exist, then you select cell A18, which has the number 3106, then you selected a number before 3107.


And now you want a later number.

That is what does not make sense.

RowColumn A
163104
173105
183106
193109
203111
213114
223117

<tbody>
</tbody>

Your code was subtracting 10 from the active cell and turning it yellow not exactly what I was wanting lol
I'm trying to select a row in column A, that value -10 will determine what the range is so in this example Cell A22 is 3117 - 10 so highlight cells A22-A18
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

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