Change interior colour of last non empty cell in a column

Eric Carolus

Board Regular
Joined
Sep 17, 2012
Messages
128
Office Version
  1. 2016
Platform
  1. Windows
Hi folks
The code below allows me to get the cell address, the row number and the value of the last non-empty cell
in column C of worksheet called Teachers.

I can select (LastCell.Select), but I cannot change the the cell fill
with LasCell.Interior.Color = vbYellow.

I have scanned the literature but cannot find suitable information.
I know that the cell's address (given by LastCell.Address) is a string, but I simply fail
to change the cells fill.

I can use all help I can get. Thanks in advance.

Private Sub cmdOneTeach_Click() [A button]
Dim WS As Worksheet
Dim LastCell As Range
Dim LastCellRowNumber As Long
Dim myrange As Range
Set WS = Worksheets("Teachers")
With WS
Set LastCell = .Cells(.Rows.Count, "C").End(xlUp)
' ActiveCell = LastCell.Select
'''''''' ActiveCell.Offset(0, 1).Interior.Color = vbYellow
LastCellRowNumber = LastCell.Row
LastCell.Select

End With
MsgBox "JW:PP is cell content: " & LastCell _
& vbCrLf _
& vbCrLf _
& "Number is " & LastCellRowNumber _
& vbCrLf _
& vbCrLf _
& "The cell address is: " & LastCell.Address

Eric
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
VBA Code:
LastCell.Offset(0, 1).Interior.Color = vbYellow
 
Upvote 0
VBA Code:
LastCell.Offset(0, 1).Interior.Color = vbYellow
Hi footoo

Thanks for the quick response
I changed the code as you suggested.

But it brings up an error that says:

Run-time error 1004

Application defined or object-defined error

Eric
 
Upvote 0
Hi footoo

Thanks for the quick response
I changed the code as you suggested.

But it brings up an error that says:

Run-time error 1004

Application defined or object-defined error

Eric
Where did you put it? Can you post the whole revised macro?
 
Upvote 0
Hi footoo and Fluff

That was it! I had totally forgot that I should unprotect and then protect
the sheet again.

Thank you guys. I really made a dumb mistake!

Eric
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,086
Members
449,206
Latest member
ralemanygarcia

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