簡體   English   中英

查找在兩個不同列中包含兩個單元格值的行

[英]Finding the row that contains two cell values in two different columns

我編寫了一對If語句來檢查兩個單元格的兩列中的值(例如,檢查Column A中的B3.Value,檢查Column G中的B2.Value),只是意識到這不會基於這些返回結果存在於同一行中的值,但前提是它們完全存在於表中。

我的目標是讓代碼檢查,看是否一排B3.Value存在於列A B2.Value在列G.這些列位於表的DataBodyRange(因為范圍將是動態的)。 現在,我得到的錯誤代碼是

Dim tblData As ListObject
Dim checkDate As Date
Dim reportSup As Range
Dim reportDate As Range

Set tblData = Worksheets("Data").ListObjects(1)
Set reportSup = Worksheets("Daily").Range("B2")
Set reportDate = Worksheets("Daily").Range("B3")

  checkDate = WorksheetFunction.Max(Worksheets("Data") _
    .ListObjects("Data") _
    .ListColumns("Date") _
    .DataBodyRange)

  If reportDate.Value = checkDate Then

    If Not IsError(Application.Match(reportSup.Value, _
      tblData.ListColumns(7).DataBodyRange, 0)) Then

      MsgBox "You have already reported for this period. " & _
        "Please enter a different date."
      reportDate.Select
      Exit Sub

    End If

  End If

我不確定是否應該使用EVALUATE或INDEX來找到滿足兩個條件的行,以便如果該行存在,則彈出MsgBox,否則繼續其余代碼。

如果我正確理解了您的問題,那么您就可以做到。 它使用“查找”在列A中查找B3值,然后檢查相應的列G值。 我假設這只能發生一次,因此如果不是這種情況,則需要修改代碼。

Sub x()

Dim tblData As ListObject
Dim reportSup As Range
Dim reportDate As Range, rFind As Range, s As String

Set tblData = Worksheets("Data").ListObjects(1)
Set reportSup = Worksheets("Daily").Range("B2")
Set reportDate = Worksheets("Daily").Range("B3")

With tblData.ListColumns(1).DataBodyRange
    Set rFind = .Find(What:=reportDate, LookAt:=xlWhole, MatchCase:=False, SearchFormat:=False)
    If Not rFind Is Nothing Then
        s = rFind.Address
        Do
            If rFind.Offset(, 6).Value = reportSup.Value Then
                MsgBox "Both values found in row " & rFind.Row
                Exit Sub
            Else
                Set rFind = .FindNext(rFind)
            End If
         Loop While rFind.Address <> s
    End If
End With

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM