使用instr从另一个表行中出现的源表行中查找3个匹配的单元格值

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

我很擅长使用它进行编码,下面看到的内容可能看起来非常糟糕。

此代码目前所执行的操作是在单独的工作表中搜索出现在单个且绝对定义的(用于测试目的)另一个表的行中的所有3个必需值。从该行复制A单元格值,将其粘贴到当前正在搜索的源表格行旁边的单元格中,并使用绿色填充对其进行颜色编码。

我想要它做的是,要知道在另一个工作表中有另外一个数据表,并让它逐行搜索给定行中匹配的所有3个必需值。

一旦获得精确命中,我希望它输出已被确认为所有3个所需值的匹配的行的A单元格值。

另一张表中的表格是动态的,因为它每天的总行数增加或减少。

有人能够帮助我吗?

现在,这是我的代码新手mishmash:

Private Sub Match_Click()
Dim i As Integer, row As Integer, narrative1 As String, transDate As Date, 
amount As Double, result As String
row = 2
i = 1
narrative1 = Worksheets("Sheet2").Range("D" & row)
transDate = Worksheets("Sheet2").Range("B" & row)
amount = Worksheets("Sheet2").Range("J" & row)


Do While Cells(i, 1).Value <> ""

If narrative1 > "" Then
  If InStr(1, UCase(Worksheets("Sheet1").Range("D22")), UCase(narrative1)) And 
  InStr(1, Worksheets("Sheet1").Range("B22"), transDate) And InStr(1, 
  Worksheets("Sheet1").Range("H22"), amount) Then
  result = Worksheets("Sheet1").Range("A3").Value
Else
  result = ""
End If
End If

i = i + 1

If Worksheets("Sheet2").Range("A" & row).Value = "" Then result = ""
Worksheets("Sheet2").Range("K" & row).Value = result

If result <> "" Then Worksheets("Sheet2").Range("K" & row).Interior.Color = 
RGB(198, 224, 180)

If Worksheets("Sheet2").Range("A" & row).Value = "" Then 
Worksheets("Sheet2").Range("K" & row).Interior.ColorIndex = xlNone

row = row + 1
narrative1 = Worksheets("Sheet2").Range("D" & row)
transDate = Worksheets("Sheet2").Range("B" & row)
amount = Worksheets("Sheet2").Range("J" & row)
Loop

End Sub
excel excel-vba search vba
1个回答
2
投票

我认为下面的代码会做你期望的,我已经评论它让你知道它在做什么(我没有测试过,但我很确定它能完成这项工作):

Private Sub Match_Click()
Dim i As Long 'These should be Long instead of Integer, as Excel has more cells than Integer has values.
Dim row As Long
Dim LastRow As Long
Dim LastRow2 As Long
Dim narrative1 As String
Dim transDate As String
Dim amount As Double
Dim result As String
Dim val1 As Integer
Dim val2 As Integer
Dim val3 As Integer

LastRow2 = Worksheets("Sheet2").Cells(Worksheets("Sheet2").Rows.Count, "A").End(xlUp).row 'get the lastrow of Sheet2
LastRow = Worksheets("Sheet1").Cells(Worksheets("Sheet1").Rows.Count, "A").End(xlUp).row 'get the lastrow of Sheet1

For i = 1 To LastRow2 'loop from row 1 to last on Sheet2
narrative1 = Worksheets("Sheet2").Range("D" & i) 'get the variables to compare
transDate = Worksheets("Sheet2").Range("B" & i)
amount = Worksheets("Sheet2").Range("J" & i)
    For x = 1 To LastRow ' loop through row 1 to last on Sheet1
        If narrative1 <> "" Then
            val1 = InStr(Worksheets("Sheet1").Cells(x, 4).Value, narrative1) 'number 4 represents column D
            val2 = InStr(Worksheets("Sheet1").Cells(x, 2).Value, transDate) 'number 2 represents column B
            val3 = InStr(Worksheets("Sheet1").Cells(x, 8).Value, amount) 'number 8 represents column H

            If val1 > 0 And val2 > 0 And val3 > 0 Then 'if all three have been found
                result = Worksheets("Sheet1").Cells(x, 1).Value 'get result
                Worksheets("Sheet2").Range("K" & LastRow2 + 1).Value = result 'paster result into next free row on Sheet2 column K
                If Worksheets("Sheet2").Cells(x, 1).Value <> "" Then Worksheets("Sheet2").Range("K" & LastRow2 + 1).Interior.ColorIndex = 4
            Else
                result = ""
            End If
        End If
     Next x
Next i
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.