在范围内查找空单元格,然后返回与空单元格在同一行中的另一个单元格的内容

问题描述 投票:0回答:1

我有一个从这里改编的excel vba代码(https://wellsr.com/vba/2016/excel/use-isempty-vba-to-check-if-cell-is-blank/),它确定给定范围内的任何单元格是否为空白。如果有任何返回msgbx,如果没有,则返回不同的msg。

我想知道是否可以返回(在msgbx中)同一行的E列中单元格的内容。这将指示哪个是需要寻址的空单元格的行。

它看两列。理想情况下,如果两个单元格在同一行中都是空的,则只返回E列中单元格的内容一次。

这是代码,你们可以帮我修改做我要问的吗?

Sub IsEmptyRange()
Dim cell As Range
Dim bIsEmpty As Boolean

bIsEmpty = False
For Each cell In Range("G26:H38,G25,G23:H24,G22,G6:H21,G5,G3:H4")
    If IsEmpty(cell) = True Then
    'An empty cell was found. Exit loop
    bIsEmpty = True
        Exit For
    End If
Next cell

If bIsEmpty = True Then
    MsgBox "One or more cells are empty"
Else   
    MsgBox "All cells are filled in"
End If
End Sub

谢谢!

WITH

excel vba
1个回答
0
投票

编辑。包含的评论请求。

Option Explicit

Sub IsEmptyRange()
Dim cell As Range
Dim bIsEmpty As Boolean
Dim emptyCellRow As Long
Dim arrRows() As Long, i As Long
Dim arrValues() As String
Dim emptyRow As Variant, emptyRows As String, emptyValues As String
bIsEmpty = False
i = 0
For Each cell In Range("G26:H38,G25,G23:H24,G22,G6:H21,G5,G3:H4")
    If IsEmpty(cell) = True Then
        'An empty cell was found. Exit loop
        emptyCellRow = cell.Row
        bIsEmpty = True

        ReDim Preserve arrRows(i)
        ReDim Preserve arrValues(i)
        arrRows(i) = emptyCellRow
        arrValues(i) = Cells(emptyCellRow, 5).Value
        i = i + 1
    End If
Next cell

If bIsEmpty = True Then
    For Each emptyRow In arrRows
        emptyRows = emptyRows & " " & emptyRow & " "
    Next emptyRow

    For Each emptyRow In arrValues
        emptyValues = emptyValues & " " & emptyRow & " "
    Next emptyRow
End If

If bIsEmpty = False Then
    MsgBox "All cells are filled in"
Else
    MsgBox "Empty rows: " & emptyRows
    MsgBox "Values in empty rows: " & emptyValues
End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.