VBA Excel找到值旁边的第一个空单元格

时间:2016-03-01 09:47:21

标签: excel vba excel-vba

可以在项目上使用一些帮助。我的表看起来像这样:

WEEK1 TEST1 VALUE
WEEK1 TEST1 [空白]
WEEK1 TEST2 [空白]
WEEK2 TEST1 VALUE
WEEK2 TEST2 [空白]
WEEK2 TEST1 VALUE

不同版本中约有800行。

现在我需要在C中找到第一个空单元格,旁边有WEEK2和TEST2。怎么会这样做呢?目的是在该单元格中输入一个值,该值来自定义A和B的用户表单。

3 个答案:

答案 0 :(得分:1)

Sub FindMatch()
Dim sTxt1 As String, sTxt2 As String, vMatch As Variant

sTxt1 = """Week2"""
sTxt2 = """Test2"""

sformula = "MATCH(1,(A:A=" & sTxt1 & ")*(B:B=" & sTxt2 & "),0)"
vMatch = Evaluate(sformula)
If IsNumeric(vMatch) Then MsgBox Range("C" & vMatch).Address

End Sub

添加了另一个条件来检查Column-C是否为空?替换下面的代码行以验证列C部分。

sformula = "MATCH(1,(A:A=" & sTxt1 & ")*(B:B=" & sTxt2 & ")*(C:C=""""),0)"

答案 1 :(得分:0)

试试这段代码。

Sub CheckRows()
Dim RowNo As Long
RowNo = 1
    With ActiveWorkbook.Sheets(1)
        Do While .Cells(RowNo, 1).Value <> ""
                If UCase(.Cells(RowNo, 1).Value) = "WEEK2" And _
                UCase(.Cells(RowNo, 2).Value) = "TEST2" And _
                .Cells(RowNo, 3).Value = "" Then
                MsgBox "Found at Row Number " & RowNo
                Exit Sub
            Else
            RowNo = RowNo + 1
            End If
        Loop
    End With
End Sub

答案 2 :(得分:0)

Sub test()
    Dim x As Range, i&: i = [C:C].Find("*", , , , , xlPrevious).Row
    For Each x In Range("A1:A" & i)
        If UCase(x.Value2 & x.Offset(, 1).Value2 & _
                 x.Offset(, 2).Value2) = "WEEK2TEST2" Then
            MsgBox x.Offset(, 2).Address(0, 0):  Exit For
        End If
    Next x
End Sub