0

我需要将一张纸的基行与另一张纸的另一行进行比较。基础工作表将始终使用范围 A8、B8、C8 和 D8。Sheet 2 的行将随着行的添加或删除而动态变化,但始终使用 A、B、C 和 D 列。例如,这次它可能有 3 行,并包含 5 行用于下一次比较。但是,比较总是从工作表 2 中的第 3 行开始,直到匹配或用完行。如果基础表的 A8 与表 2 的 A3 匹配,则检查基础表的 B8 与表 2 的 B3。如果 A8 与 A3 不匹配,则移动到下一行并检查 A8 与 A4,依此类推。我正在检查基行的 A 列是否与工作表 2 的 A 列匹配(B 匹配 B,C 匹配 C,D 匹配 D)。如果来自基础工作表的范围与来自另一张工作表的范围不匹配,检查下一行进行比较,直到 match = true 并返回 true,否则返回 false。基础工作表上的 A 列永远不会匹配工作表 2 中的 B、C 或 D 列。基础工作表的 B 永远不会匹配工作表 2 的 A、C 或 D 列,依此类推。

在此先感谢您的帮助。如果您需要我提供更多信息,请告诉我。

你是对的。我正在寻找一个函数来返回匹配的行号,如果没有找到匹配项,则返回 -1。我喜欢你的连接想法,所以我在想这样的事情。如果我离基地很远并且有更简单的方法可以做到这一点,请告诉我。我的自尊心不容易伤痕累累。

Public Function RangesMatchRow(RefSheet As Worksheet) As Integer
''I need to be able to return matching row number 
Dim Rng, rng2, val As Range
Dim baseStr, refStr As String
Dim lastRow, i As Integer
Dim BaseSheet As Worksheet

Set BaseSheet = Sheets("Base")
'Get the range you want to compare
Set Rng = BaseSheet.Range("A8:D8")
'And concantenate it
For Each val In Rng.Cells
    baseStr = baseStr & val.Value
Next val

lastRow = RefSheet.Range("A").Offset(sheetName.Rows.Count - 1, 0).End(xlUp).Row
For i = 3 To lastRow ''It will always start with row three and go until the last row for column A
    rng2 = sheetName.Range("Ai:Di") ''Not sure if this is right but i represents the row number
    For Each val In rng2
        refStr = refStr & val.Value
    Next val
    If StrComp(UCase(baseStr), UCase(refStr), vbTextCompare) = 0 Then ''If they match Then
        RangesMatchRow = i ''Set RangesMatchRow equal to i
        Return ''And return
    End If
Next
    RangesMatchRow = -1 ''If no matches are found then return -1

End Function
4

2 回答 2

0

我假设您需要一个功能?这是代码:

Function FIND_PLUS(lookup_value As Range, reference As Range) As Boolean

Dim rng, val, rng2  As Range
Dim str_2_match, formula As String
Dim row_count, i As Double
Dim search_fld() As Variant

Set rng = lookup_value
'first get the string to look for.
'here we concatenated all the values instead of comparing cell by cell
For Each val In rng.Cells
    str_2_match = str_2_match & val.Value
Next val

row_count = reference.Rows.Count
ReDim search_fld(1 To row_count) 'resize the array

'here we made an array of the concatenated values of the range you want to search
'we used simple resize and offset to go through all the rows of your selected range
For i = 1 To row_count
    Set rng2 = reference.Resize(1).Offset(i - 1)
    For Each val In rng2.Cells
        search_fld(i) = search_fld(i) & val.Value
    Next val
Next i
'here is where we performn the actual look up
With Application

Select Case IsError(.Match(str_2_match, search_fld, 0))
Case False
    FIND_PLUS = True
Case Else
    FIND_PLUS = False
End Select

End With

End Function

如何使用?将代码粘贴到模块中。
您现在可以使用 UDF 'FIND_PLUS'。
在任何单元格中使用公式。
第一个参数是您要搜索的范围(在您的情况下是 Sheet1!A8:D8)
第二个参数是您要搜索匹配项的范围。(Sheet2!A3:D?)
在您输入公式的单元格中,TRUE如果有匹配项,
FALSE则返回。

如果您不需要功能,这至少可以帮助您入门。
我已经在特定行上添加了注释,以指导您代码的作用。

于 2013-10-10T02:58:22.540 回答
0
Public Function RangesMatchRow(textInColA As String) As Integer
Dim rng, rng2, val As Range
Dim baseStr, refStr As String
Dim lastRow, i As Integer
Dim sheetName, baseSheet As Worksheet

Set sheetName = Sheets(textInColA)
Set baseSheet = Sheets("Base")
'Get the base range you want to compare
Set rng = baseSheet.Range("A8:D8")
'And concantenate it
For Each val In rng.Cells
    baseStr = baseStr & val.Value
Next val

lastRow = sheetName.Range("A1").Offset(sheetName.Rows.Count - 1, 0).End(xlUp).Row
''Gives me the last row anything was entered in column A
For i = 3 To lastRow
    Set rng2 = sheetName.Range("A" & i & ":D" & i)
    ''Concantenate reference row each time through the loop
    For Each val In rng2.Cells
        refStr = refStr & val.Value
    Next val
    ''Convert everything to uppercase to make it case insensitive
    ''Compare the rows and return the row number if there is a match
    If StrComp(UCase(baseStr), UCase(refStr), vbTextCompare) = 0 Then
        RangesMatchRow = i
        Exit Function
    End If
Next i
    ''Return -1 if no matches are found
    RangesMatchRow = -1

End Function
于 2013-10-13T14:07:10.263 回答