Offset interior color of a blank cell if adjacent cells have a value.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
774
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have part of a macro and I think I have a couple of concepts correct; however, I'm trying to pieces them with components I'm missing. If starting on K5 and going down, for all cells that have any value. In this case if the adjacent cells in Column L are blank then the cell will be filled in with yellow. For example Cell K8 has "1105" and L8 is blank it will be colored. If it is not blank then nothing happens.

I know I'll need an "else" statement, and I don't know where I'll put my If blank statement( "">

I think I have a couple things I can use in the macro below.

Thank you so much for your patience.

VBA Code:
Sub Offset_interior()
    Dim Kl As Range
   For Each Kl In ActiveSheet.Range("L5:L" & ActiveSheet.Cells(Rows.Count, 11).End(xlUp).Row)
      If K1.Value Like "*" Then K1.Offset(, 1).Interior.Color = RGB(255, 255, 0)
   Next K1
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
instead of VBA could you not just use conditional formatting?

Book1
KL
81234678
9644
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
L8:L100Expression=AND($K8<>"",$L8="")textNO
 
Upvote 0
I wish I could but this worksheet will be used everyday by numerous people. Part of this I have a macro which clears everything, so I can paste the new template. There are about 10 other macros in this worksheet. Multiple columns are cut, inserted and moved when my Run all option is triggered.

There is a generated PDF from another system which I then save as an excel which is then pasted on my worksheet containing my multiple macros and then my program kicks in.

Thank you
 
Upvote 0
instead of VBA could you not just use conditional formatting?

Book1
KL
81234678
9644
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
L8:L100Expression=AND($K8<>"",$L8="")textNO
I will try the conditional formatting, I will look into finding a VBA that I can still clear page without wiping out my conditional formatting.

Thank you,
 
Upvote 0
try this on the sheet code
right click on the sheet name
choose View code
and past the below.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rgK, rgL As Range
Set rgK = Range("K:K")
Set rgL = Range("L:L")

If Not Intersect(Target, rgK) Is Nothing Then
    If Target <> "" And Target.Offset(, 1) = "" Then
        Target.Offset(, 1).Interior.Color = RGB(255, 255, 0)
        Exit Sub
    End If
    If Target = "" Then
        Target.Offset(, 1).Interior.Color = xlNone
        Exit Sub
    End If
End If
If Not Intersect(Target, rgL) Is Nothing Then
    If (Target <> "" And Target.Offset(, -1) <> "") Or (Target = "" And Target.Offset(, -1) = "") Then
        Target.Interior.Color = xlNone
        Exit Sub
    End If
    If Target = "" And Target.Offset(, -1) <> "" Then
        Target.Interior.Color = RGB(255, 255, 0)
        Exit Sub
    End If
End If
End Sub

let me know if you encounter any issues
 
Upvote 0
For example Cell K8 has "1105" and L8 is blank it will be colored

If you want to run the macro, try the following option:
VBA Code:
Sub ColorColumnL()
  Dim lr1 As Long, lr2 As Long
  lr1 = Range("K" & Rows.Count).End(3).Row
  With ActiveSheet.Range("K4:L" & lr1)
    .Interior.ColorIndex = xlNone
    .AutoFilter Field:=1, Criteria1:="=*"
    .AutoFilter Field:=2, Criteria1:="="
    lr2 = Range("K" & Rows.Count).End(3).Row
    If lr2 > 4 Then Range("L5:L" & lr2).Interior.Color = vbYellow
    .AutoFilter
  End With
End Sub

If you want it automatic, put the following code in the events of your sheet. Every time you write something in column K or L it will highlight yellow if necessary.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim rng As Range, c As Range
  
  Set rng = Intersect(Target, Range("K5:L" & Rows.Count))
  If Not rng Is Nothing Then
    For Each c In rng
      With Range("L" & c.Row)
        If Range("K" & c.Row).Value <> "" And .Value = "" Then
          .Interior.Color = vbYellow
        Else
          .Interior.ColorIndex = xlNone
        End If
      End With
    Next
  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.
----- --
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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