【问题标题】:Compare rows in separate worksheets比较不同工作表中的行
【发布时间】:2013-10-13 22:07:10
【问题描述】:

我需要将一张纸的基行与另一张纸的另一行进行比较。基础工作表将始终使用范围 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

【问题讨论】:

  • 向我们展示您迄今为止的尝试。
  • 没有代码但需求密集的问题从来都不是很有趣;-)
  • 这是我想出来的。如果没有人提出更好的答案,我会将其添加到答案部分,以供其他希望解决相同问题的人使用。

标签: excel vba


【解决方案1】:

我假设你需要一个函数?代码如下:

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

如果您不需要函数,这至少可以帮助您入门。
我已将 cmets 放在特定行上,以指导您代码的作用。

【讨论】:

  • 串联的想法是为我做的。如果我能想到这一点,我一开始就不必寻求帮助。谢谢。我没有足够的声誉来为您的答案 +1,但我认为如果我只需要找出是否有匹配项而无需返回行号,它就会起作用。
【解决方案2】:
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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多