【问题标题】:How to check if empty cell in a range?如何检查范围内是否为空单元格?
【发布时间】:2020-10-04 10:51:08
【问题描述】:

我只想检查一个范围内是否有空行,例如,如果 S28 为“KO”或“OK”,则上面的行 (offset(-1,0) 不应为空。
如果为空白,则该功能应停止。
如果一个单元格是空白的,而上面的单元格是空白的,那没关系。

S中的每个单元格都有一个公式,countif函数。

代码说有空行,事实并非如此。我删除了 S28 中的数据,您可以在图片上看到。因此,不应该有 msgbox。第一行检查在 S12 中。

Private Function detecht_empty_rows() As Boolean
    
    Call DefineVariables
    
    Dim lrowS As Long
    Dim cell As Range
    Dim startingcell As String
    
    lrowS = shInput.cells(Rows.Count, 19).End(xlUp).Row
    
    For Each cell In shInput.Range("S13" & ":" & "S" & lrowS)
    
        startingcell = cell.Address
    
        If cell.Text = "" And IsEmpty(cell.Offset(-1, 0)) = True Then
    
        ElseIf cell.Text = "OK" Or cell.Text = "KO" And IsEmpty(cell.Offset(-1, 0)) = True Then
    
            MsgBox "Please remove the blank rows"
            Exit Function
       
        End If
    
    Next cell
    
End Function

【问题讨论】:

  • 两个 KO 之间的界限对我来说看起来很空旷。另外,您的 if 语句看起来很奇怪,因为 If 子句没有代码。
  • 我特意放了这张图片,在我运行代码的地方,我删除了空单元格。这张图片说明了我的代码目标。

标签: excel vba is-empty


【解决方案1】:

请测试下一个适配的功能。我假设 DefineVariables 定义了 shInput 工作表。出于测试原因,我的代码将讨论中的工作表定义为活动工作表。您可以删除/评论声明和价值分配:

Private Function detecht_empty_rows() As Boolean

'Call DefineVariables

Dim lrowS As Long, cell As Range, startingcell As String
Dim shInput As Worksheet, boolEmpty As Boolean, rowNo As Long

Set shInput = ActiveSheet 'use here your defined worksheet. 
                          'Clear the declaration if declared at the module level
lrowS = shInput.cells(rows.count, 19).End(xlUp).row
'new inserted code line:________________________________
lrowS = lastR(shInput.range("S13" & ":" & "S" & lrowS))
'_______________________________________________________

For Each cell In shInput.Range("S13" & ":" & "S" & lrowS)
    If cell.text = "" And cell.Offset(-1, 0) = "" Then
        boolEmpty = True: rowNo = cell.Offset(-1).row: Exit For
    ElseIf (cell.text = "OK" Or cell.text = "KO") And cell.Offset(-1, 0) = "" Then
        boolEmpty = True: rowNo = cell.Offset(-1).row: Exit For
    End If
 Next cell
 If boolEmpty Then MsgBox "Please remove the blank row (" & rowNo & ").": detecht_empty_rows = False: Exit Function

detect_empty_rows = True
End Function

下一个函数将以不同的方式计算要处理的最后一行:

Function lastR(rng As range) As Long
   Dim i As Long, lngStart As Long, lngEnd As Long, sh As Worksheet
   
   lngStart = rng.cells(1).Row: lngEnd = lngStart + rng.Rows.Count - 1
   Set sh = rng.Parent
   For i = lngStart To lngEnd
       If WorksheetFunction.CountIf(sh.range(sh.range("S" & i), sh.range("S" & lngEnd)), "OK") + _
            WorksheetFunction.CountIf(sh.range(sh.range("S" & i), sh.range("S" & lngEnd)), "KO") = 0 Then
            lastR = i - 1: Exit Function
       End If
   Next i
End Function

你必须改变

ElseIf cell.text = "OK" Or cell.text = "KO" And IsEmpty(cell.Offset(-1, 0)) 

ElseIf (cell.text = "OK" Or cell.text = "KO") And cell.Offset(-1, 0) = "" 

Or 条件必须像单个检查一样与IsEmpty 部分一起检查。

那么,startingcell = cell.Address 是无用和未使用的,它每次迭代都取一个新值。

不一定要使用IsEmpty(cell.Offset(-1, 0)) = True。使用IsEmpty(cell.Offset(-1, 0))就足够了。无论如何,该方法返回一个Boolean变量。

作为返回Boolean 的函数,它应该返回它。可以在调用函数的代码中使用。

但是在公式的情况下,即使返回空字符串(""),IsEmpty也不能使用。我的意思是,它不起作用,单元格不是空的。代码必须使用cell.Offset(-1, 0) = ""

请注意不要在“S12”处有空单元格...

【讨论】:

猜你喜欢
  • 2023-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多