简体   繁体   中英

Excel VBA conditional formatting based on next cell value

I am trying to do some conditional formatting in Excel for the font size but seeing as it can't be done from the format menu, it needs to be done with VBA.

I have a range B6 to however many rows and I want to look at the cell next to it and see if it's blank (column C). If it is then format the cell to Bold and 11pt. If it's not blank then it needs to be normal and 9pt.

My code at the minute only makes the last row Bold and 11pt and the rest of the column, even if column C is empty will be normal 9pt.

What is going wrong? BTW I'm using Excel 2003

Dim c As Range, rng
Dim LASTROW As Long

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

Set rng = Range("B6:B" & LASTROW)
For Each c In rng
    If Len(c.Offset(1, 0)) = 0 Then
        c.Font.Bold = True
        c.Font.Size = 11
    Else
        c.Font.Bold = False
        c.Font.Size = 9
    End If
Next c

Your Offset parameters are backwards. You are checking the cell below the current one.

这是如何做

Note the trick is to use a single rule, coded for the top-left cell

This doesn't need a macro - you can do it using a formula in Conditional Formatting.

Say you wanted to highlight the adjacent cell in column B red when the cell in column C had a value of "Red":

=IF(C6="Red",TRUE,FALSE)

then just fill down with the fill handle as usual.

Rule editor (2007):

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM