Change format of cell based on value of cell in corresponding row?

EssKayKay

Active Member
Joined
Jan 5, 2003
Messages
413
Office Version
  1. 2007
Platform
  1. Windows
Hello,

I’m looking for a routine to change the status (format) of a cell in column I based on the status of a corresponding cell in the same row in column M for a range “M33:M2000” (or “I33:I2000”). I want the action to happen when the value of the cell in column M > 0. If the value of the cell in column M = 0 (or blank) then do nothing. Column M is actually formatted to Date. I would run this process via a command button.

Example: If the value in M44 is 06/12/23, then change the format of cell I44 to “Locked” and the font color to Black. Normally all cells in “I33:I2000” are unlocked & blue.

Thanks for viewing,
Steve K.
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Just curious as to why you want to have this with a command button vs a persistent review? Are you going to go back and change the formatting (but keep the data the same) after you've reviewed the record? If not conditional formatting would be a consideration?

Nevermind, I did not see that you want to turn protection on.

But, you cannot have negative values formatted as dates. So you'll get an error in column M when that happens:
So, the all the dates except 12/31/1899 or (1/0/1990) will have your highlight.


Book1
JM
1ValueValue as Date
2-5################
3-3################
441900-01-04
541900-01-04
651900-01-05
721900-01-02
811900-01-01
9-1################
10-4################
1121900-01-02
12-5################
1351900-01-05
1441900-01-04
15-1################
1601900-01-00
1731900-01-03
1821900-01-02
19-3################
20-4################
21-3################
2251900-01-05
2301900-01-00
24-4################
25-5################
26-5################
Sheet8
Cell Formulas
RangeFormula
J2:J26J2=M2#
M2:M26M2=RANDARRAY(25,1,-5,5,1)
Dynamic array formulas.
 
Last edited:
Upvote 0
I have another routine I received elsewhere that sort-of does what I want. The only problem is it does not do exactly what I want. It locks/unlocks the cells in column I in reverse (i.e., the ones I want locked are unlocked and the ones I want unlocked are locked. Also, I did not know how to change the color.

Here’s the code:

VBA Code:
Private Sub cmdTest1_Click()
Dim myLastRow As Long
Dim i As Long
Application.ScreenUpdating = False

     myLastRow = Cells(Rows.Count, "M").End(xlUp).Row
     For i = 33 To myLastRow
     If Cells(i, "M").Value > 0 Then Range(Cells(i, "I"), Cells(i, "I")).Locked = False
     Next i

Application.ScreenUpdating = True
End Sub
 
Upvote 0
Just curious as to why you want to have this with a command button vs a persistent review? Are you going to go back and change the formatting (but keep the data the same) after you've reviewed the record? If not conditional formatting would be a consideration?

Nevermind, I did not see that you want to turn protection on.

But, you cannot have negative values formatted as dates. So you'll get an error in column M when that happens:
So, the all the dates except 12/31/1899 or (1/0/1990) will have your highlight.


Book1
JM
1ValueValue as Date
2-5################
3-3################
441900-01-04
541900-01-04
651900-01-05
721900-01-02
811900-01-01
9-1################
10-4################
1121900-01-02
12-5################
1351900-01-05
1441900-01-04
15-1################
1601900-01-00
1731900-01-03
1821900-01-02
19-3################
20-4################
21-3################
2251900-01-05
2301900-01-00
24-4################
25-5################
26-5################
Sheet8
Cell Formulas
RangeFormula
J2:J26J2=M2#
M2:M26M2=RANDARRAY(25,1,-5,5,1)
Dynamic array formulas.

There will not be any negative dates in column M - only positive or blank cells.
 
Upvote 0
So, really, it is if there is a date present, not if the number is greater than zero.

Regardless,
Here is some VBA with the color and locking assigned:
I am not the greatest at VBA, there may be more efficient formaulas. But this has the color and locking you seek, you can use those lines to acheive that in the VBA in the earlier posts:

NOTE: this starts in I2.

VBA Code:
Sub ColorAndLockCells()
Dim ColI As Range
Dim ColIAddress As String
Dim CurSheetName As String

Application.ScreenUpdating = False
CurSheetName = ThisWorkbook.ActiveSheet.Index
Sheets(8).Select
Range("I2").Select
'Range.Font.Color = 0
ColIAddress = Range("I2").CurrentRegion.Offset(1, 0).Address
Set ColI = Range(ColIAddress)

For Each Cell In ColI
        If Cell.Offset(0, 4).Value > 0 Then
            Cell.Font.ColorIndex = 0
            Cell.Locked = True
        End If
    Next Cell
   
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you Awoohaw for your quick responses - much appreciated.
I tried your code. However, it errors with Compile error - Variable not defined on the line:
For Each cell In ColI

Not to press the subject but did you see my "old" code that sort of worked? Not sure if that makes any sense.
 
Upvote 0
Updating your code with the formatting and locking you wish:

VBA Code:
Private Sub cmdTest1_Click()
Dim myLastRow As Long
Dim i As Long
Application.ScreenUpdating = False
myLastRow = Cells(Rows.Count, "M").End(xlUp).Row
For i = 33 To myLastRow
If Cells(i, "M").Value > 0 Then [
Range(Cells(i, "I"), Cells(i, "I")).Locked = False
Range(Cells(i, "I"), Cells(i, "I")).Font.ColorIndex = 0
End if
Next i
Application.ScreenUpdating = True
End Sub

Regarding the code i suggested. Look at the sheet name/index. You need to be in a cell on the workbook you have the data in.
(It works for me, so it is referencing inside the code that is causing the problems.
Be sure the sheet name and the start of Column I is accurate).
 
Upvote 0
Solution
So, really, it is if there is a date present, not if the number is greater than zero.

Regardless,
Here is some VBA with the color and locking assigned:
I am not the greatest at VBA, there may be more efficient formaulas. But this has the color and locking you seek, you can use those lines to acheive that in the VBA in the earlier posts:

NOTE: this starts in I2.

VBA Code:
Sub ColorAndLockCells()
Dim ColI As Range
Dim ColIAddress As String
Dim CurSheetName As String

Application.ScreenUpdating = False
CurSheetName = ThisWorkbook.ActiveSheet.Index
Sheets(8).Select
Range("I2").Select
'Range.Font.Color = 0
ColIAddress = Range("I2").CurrentRegion.Offset(1, 0).Address
Set ColI = Range(ColIAddress)

For Each Cell In ColI
        If Cell.Offset(0, 4).Value > 0 Then
            Cell.Font.ColorIndex = 0
            Cell.Locked = True
        End If
    Next Cell
 
Application.ScreenUpdating = True
End Sub

Thanks again Awoohaw.

I'm still getting an error. Maybe I'm missing something (which is totally possible). It may be something to do with CurSheetNam. I'm not sure about this.
Here's what I'm seeing.


Error1.jpg
 
Last edited:
Upvote 0
Updating your code with the formatting and locking you wish:

VBA Code:
Private Sub cmdTest1_Click()
Dim myLastRow As Long
Dim i As Long
Application.ScreenUpdating = False
myLastRow = Cells(Rows.Count, "M").End(xlUp).Row
For i = 33 To myLastRow
If Cells(i, "M").Value > 0 Then [
Range(Cells(i, "I"), Cells(i, "I")).Locked = False
Range(Cells(i, "I"), Cells(i, "I")).Font.ColorIndex = 0
End if
Next i
Application.ScreenUpdating = True
End Sub

Regarding the code i suggested. Look at the sheet name/index. You need to be in a cell on the workbook you have the data in.
(It works for me, so it is referencing inside the code that is causing the problems.
Be sure the sheet name and the start of Column I is accurate).

One more issue Awoohaw,

Here's what I'm seeing when I run the revised Test routine. Again, I'm sure I'm probably missing something here.

Error2A.jpg

Error2B.jpg

Thank you once again,
Steve
 
Upvote 0
I noticed one mistake I had (that worked for me). But, you didn't change any of the sheet and cell references as I had instructed:
use this:

VBA Code:
Sub ColorAndLockCells()
Dim ColI As Range
Dim ColIAddress As String
Dim CurSheetName As String

Application.ScreenUpdating = False
CurSheetName = ThisWorkbook.ActiveSheet.Name
Sheets(CurSheetName).Select
Range("I2").Select
'Range.Font.Color = 0
ColIAddress = Range("I2").CurrentRegion.Offset(1, 0).Address
Set ColI = Range(ColIAddress)

For Each Cell In ColI
        If Cell.Offset(0, 4).Value > 0 Then
            Cell.Font.ColorIndex = 0
            Cell.Locked = True
        End If
    Next Cell
    
Application.ScreenUpdating = True
End Sub

these lines all need to be changed to fit your worksheet/workbook:
VBA Code:
Range("I2").Select
'Range.Font.Color = 0
ColIAddress = Range("I2").CurrentRegion.Offset(1, 0).Address
 
Upvote 0

Forum statistics

Threads
1,224,701
Messages
6,180,444
Members
452,985
Latest member
Hai Quynh

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