You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于同行L列是否为空,设置E列多行单元格最后一行加粗?

Hey there! Let's work through this conditional formatting issue you're hitting. It sounds like you've already got code that bolds the last row in column E, but adding the check for the corresponding column L cell being empty is throwing a data mismatch error—super frustrating, I get it.

First, let's break down why that error might be happening:

  • You might be referencing a row that doesn't exist (like if column E is completely empty, the "last row" would be 0, and trying to access L0 causes issues)
  • Your empty check might not account for cells with formulas that return blank, or cells with hidden spaces (so = "" doesn't work as expected)
  • There could be a type mismatch if column L contains error values (like #N/A, #VALUE!) that break the empty check

Here's a revised version of the code that fixes these edge cases, with comments explaining each step:

Sub BoldLastRowEWithLCheck()
    Dim lastRowE As Long
    ' Find the last row with data in column E
    lastRowE = Cells(Rows.Count, "E").End(xlUp).Row
    
    ' Exit early if there's no data in column E to avoid errors
    If lastRowE = 0 Then
        MsgBox "No data found in column E!"
        Exit Sub
    End If
    
    ' Grab the corresponding cell in column L
    Dim lCell As Range
    Set lCell = Cells(lastRowE, "L")
    
    ' Check if column L's cell is NOT empty (adjust this if you want to bold when it IS empty)
    ' This handles formulas that return blank, hidden spaces, and avoids errors
    If Not IsEmpty(lCell.Value) And Trim(lCell.Value) <> "" Then
        ' Bold the last row in column E
        Range("E" & lastRowE).Font.Bold = True
    Else
        ' Optional: Unbold if the condition isn't met
        Range("E" & lastRowE).Font.Bold = False
    End If
End Sub

A few key fixes here:

  1. We first check if column E has any data at all—this prevents trying to reference a non-existent row.
  2. We use IsEmpty combined with Trim to accurately check for empty cells, even if they have hidden spaces or formulas that return blank.
  3. If you actually want to bold the last row in E only when column L is empty, just flip the condition to:
    If IsEmpty(lCell.Value) Or Trim(lCell.Value) = "" Then
    

If your "multiple rows in column E" refers to groups of rows (like separate blocks of data, each needing their last row checked against column L), let me know and I can adjust the code to loop through each group!

内容的提问来源于stack exchange,提问作者Earl J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:42:39