Select Change code that highlights only the dynamic row

Oberon70

Board Regular
Joined
Jan 21, 2022
Messages
160
Office Version
  1. 365
Platform
  1. Windows
I have the below code, which does two things.

1. it highlights the selected row, which helps keep track of manual receipting.
2. This enters the date when selecting an empty cell under the Date Column. The code will work on different reports as the column could be in a different location on other reports. (This works).

However, is it was possible to highlight only the table? And can I make this dynamic?

The other slight headache, but I will live with it unless someone knows a solution. If I fill in a cell with a colour, it is set back to no fill when I click on the spreadsheet due to the highlighting code.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)



Dim PortfolioCode As Variant

Dim ws As Worksheet

Dim LRow As Integer



Application.ScreenUpdating = False



Set ws = ThisWorkbook.Sheets(2)



PortfolioCode = ws.Range("B1").Value

LRow = Cells(Rows.Count, 1).End(xlUp).Row



Debug.Print ws.Name



Cells.Interior.ColorIndex = xlNone

With Cells(ActiveCell.Row, 1).Resize(1, 20).Interior

.ColorIndex = 24

.Pattern = xlSolid

End With



Select Case PortfolioCode

Case "BATINC", "BATINCT2", "BATINCRC", "BATINC", "ROBINIAE", "ROBINMV2", "ROBINRC", "ROBINT2", "ROBINXSE", "ROBINE", "ROBINUE", "ROBINAH"

If Not Application.Intersect(Target, Range("F1:F" & LRow)) Is Nothing Then

Application.EnableEvents = False

Target.Value = Date

Application.EnableEvents = True

End If

Case "ROBINIAG", "ROBINMVG2", "ROBINRCG", "ROBINXSE", "ROBING", "ROBINUG"

If Target.Column = 4 Then

Application.EnableEvents = False

Target.Value = Date

Application.EnableEvents = True

End If

If Target.Column = 6 Then

Application.EnableEvents = False

Target.Value = Date

Application.EnableEvents = True

End If

Case "ROBINIA", "ROBINMVA2", "ROBINXSA", "ROBINA", "ROBINUA"

MsgBox Anubis

Case Else

'MsgBox "Invalid code"

End Select

Application.ScreenUpdating = True



End Sub
 

Attachments

  • Screenshot 2022-02-19 083626.jpg
    Screenshot 2022-02-19 083626.jpg
    151.5 KB · Views: 16

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
You can try something like this but I have found it doesn't work if you click on a cell which is surrounded by blank cells.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Cells.Interior.ColorIndex = xlNone

    With Intersect(ActiveCell.EntireRow, ActiveCell.CurrentRegion).Interior
        .ColorIndex = 24
        .Pattern = xlSolid
    End With

End Sub
 
Upvote 0
You can try something like this but I have found it doesn't work if you click on a cell which is surrounded by blank cells.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Cells.Interior.ColorIndex = xlNone

    With Intersect(ActiveCell.EntireRow, ActiveCell.CurrentRegion).Interior
        .ColorIndex = 24
        .Pattern = xlSolid
    End With

End Sub
This fixes the cell surrounded by blank cells problem. Don't know whether it covers the OP's requirements:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Cells.Interior.ColorIndex = xlNone
    If Intersect(ActiveCell.EntireRow, ActiveSheet.UsedRange) Is Nothing Then
        With Range(Cells(Target.Row, "A"), Target).Interior
            .ColorIndex = 24
            .Pattern = xlSolid
        End With
    Else
        With Intersect(ActiveCell.EntireRow, ActiveSheet.UsedRange).Interior
            .ColorIndex = 24
            .Pattern = xlSolid
        End With
    End If
End Sub
 
Upvote 0
thanks for the help. I will try out the above and see how it works. I had to remove the Date being entered automatically, as it had a major headache. It worked great when I enter the designated cell, but it caused the row to be over written when I would select a whole row.
 
Upvote 0
I had to remove the Date being entered automatically, as it had a major headache. It worked great when I enter the designated cell, but it caused the row to be over written when I would select a whole row.
If Columns 4 and 6 are the date columns then this might work for those two:
VBA Code:
Cells(Target.Row,Target.Column) = Date

For the 1 that is intersect with F you can use either of these
VBA Code:
Cells(Target.Row,6) = Date
'OR
Cells(Target.Row,"F") = Date

I don't know if you want to add a check to only do that if that field is blank / empty in case there is already a date there.
eg If Cells(Target.Row,6) = "" Then
 
Upvote 0
Solution

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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