Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何检查范围内是否有空单元格?_Excel_Vba_Is Empty - Fatal编程技术网

Excel 如何检查范围内是否有空单元格?

Excel 如何检查范围内是否有空单元格?,excel,vba,is-empty,Excel,Vba,Is Empty,我只想检查一个范围内是否有空行,例如,如果S28为“KO”或“OK”,则上面的行(偏移量(-1,0)不应为空。 如果为空,则该功能应停止。 如果一个单元格为空,而上面的单元格为空,则可以 S中的每个单元格都有一个公式countif函数 代码显示有空行,但事实并非如此。我删除了S28中的数据,您可以在图片上看到。因此,应该没有msgbox。第一行检查在S12中 专用函数detecht\u empty\u rows()作为布尔值 调用定义变量 暗淡的光线和长的一样 暗淡单元格作为范围 暗淡的起始单元

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

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

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

专用函数detecht\u empty\u rows()作为布尔值
调用定义变量
暗淡的光线和长的一样
暗淡单元格作为范围
暗淡的起始单元格为字符串
lrowS=shInput.cells(Rows.Count,19).End(xlUp).Row
对于shInput.范围内的每个单元格(“S13”和“:”和“S”和lrowS)
startingcell=单元格地址
如果cell.Text=”“和IsEmpty(cell.Offset(-1,0))=True,则
ElseIf cell.Text=“OK”或cell.Text=“KO”和IsEmpty(cell.Offset(-1,0))=则为True
“请删除空白行”
退出功能
如果结束
下一个细胞
端函数

请测试下一个调整的函数。我假设
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)) 

必须将
条件与
IsEmpty
零件一起检查,就像一次检查一样

然后,
startingcell=cell.Address
是无用和未使用的,它为每次迭代获取一个新值

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

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

但对于公式,即使它返回空字符串(
”),也不能使用
IsEmpty
。我的意思是,它不起作用,单元格不能为空。代码必须使用
单元格。偏移量(-1,0)=


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

两个KO之间的行在我看来很空。加上你的if语句看起来很奇怪,因为if子句没有代码。我故意在运行代码的地方放了这张图片,我已经删除了空单元格。这张图片说明了我的代码目标。注释不用于扩展讨论;这段对话已经结束。
ElseIf (cell.text = "OK" Or cell.text = "KO") And cell.Offset(-1, 0) = ""