[英]VBA Excel matching criteria across sheets and columns
I am trying to compare data across two sheets in the same workbook. 我试图比较同一工作簿中两张纸上的数据。 First sheet has a list of individual addresses and the second has a list of address ranges where one column is the starting address range and the second column is the ending address range.
第一页包含一个单独的地址列表,第二页包含一个地址范围列表,其中一列是起始地址范围,第二列是结束地址范围。 for example
例如
sheet1:
123 main st
230 main st
456 main st
Sheet2:
100 200 main st
400 500 main st
How do I find if an individual address falls within an address range? 如何查找单个地址是否在地址范围内? I have the below code that matches on the street name, but I need to add the criteria for the street number falling within that address range, otherwise it's not a match.
我有以下与街道名称匹配的代码,但是我需要添加该地址范围内的街道编号的条件,否则不匹配。 In this example, sheet1 rows 1 and 3 is a match and sheet1 row 2 is not a match.
在此示例中,sheet1第1行和第3行是匹配项,而sheet1第2行不是匹配项。
Sub matchcolumns()
Dim I, total, fRow As Integer
Dim found As Range
total = Sheets(1).Range("A" & Rows.Count).End(xlUp).row
For I = 2 To total
answer1 = Worksheets(2).Range("A" & I).Value
Set found = Sheets(1).Columns("H:H").Find(what:=answer1) 'finds a match
If Not found Is Nothing Then
Debug.Print "MATCH"
Else
Debug.Print "NO MATCH"
End If
Next I
End Sub
Loop through Sheet1 and check whether it exists in Sheet2. 遍历Sheet1,并检查它是否存在于Sheet2中。 In this instance, MATCH or NO MATCH is written in the third column.
在这种情况下,MATCH或NO MATCH被写入第三列。 Cheers.
干杯。
Option Explicit
Public Sub check()
Dim vDataSheet As Worksheet
Dim vDataRow As Long
Dim vRefSheet As Worksheet
Dim vRefRow As Long
Dim vFound As Boolean
Set vDataSheet = Application.ActiveWorkbook.Sheets("Sheet1")
Set vRefSheet = Application.ActiveWorkbook.Sheets("Sheet2")
vDataRow = 1
While vDataSheet.Cells(vDataRow, 1) <> ""
vFound = False
vRefRow = 1
While vRefSheet.Cells(vRefRow, 1) <> "" And Not vFound
If vDataSheet.Cells(vDataRow, 1) >= vRefSheet.Cells(vRefRow, 1) And _
vDataSheet.Cells(vDataRow, 1) <= vRefSheet.Cells(vRefRow, 2) And _
vDataSheet.Cells(vDataRow, 2) = vRefSheet.Cells(vRefRow, 3) Then
vFound = True
End If
vRefRow = vRefRow + 1
Wend
If vFound Then
vDataSheet.Cells(vDataRow, 3) = "MATCH"
Else
vDataSheet.Cells(vDataRow, 3) = "NO MATCH"
End If
vDataRow = vDataRow + 1
Wend
End Sub
Sheet1 Before Sheet1之前
Sheet2 工作表2
Sheet1 After Sheet1之后
@Mikku thanks I read the data as poorly formatted columns ... not as a single column. @Mikku,谢谢,我将数据读取为格式不正确的列……而不是单个列。 My mistake.
我的错。 Here is the updated code to work on single column data.
这是处理单列数据的更新代码。 I've made simplistic assumptions on data types (and left street numbers as strings given I've no idea on how they might really be structured) etc ... but works with the data examples used in question:
我对数据类型进行了简单化的假设(假设我不知道它们的真正结构,将街道编号作为字符串),但是可以使用所讨论的数据示例:
Option Explicit
Public Sub check()
Dim vDataSheet As Worksheet
Dim vDataRow As Long
Dim vStreetNumber As String
Dim vStreetName As String
Dim vRefSheet As Worksheet
Dim vRefRow As Long
Dim vFromNumber As String
Dim vToNumber As String
Dim vFirstSpace As Long
Dim vSecondspace As Long
Dim vRefName As String
Dim vFound As Boolean
Set vDataSheet = Application.ActiveWorkbook.Sheets("Sheet1")
Set vRefSheet = Application.ActiveWorkbook.Sheets("Sheet2")
vDataRow = 1
While vDataSheet.Cells(vDataRow, 1) <> ""
vStreetNumber = Left(vDataSheet.Cells(vDataRow, 1), InStr(1, vDataSheet.Cells(vDataRow, 1), " ") - 1)
vStreetName = Right(vDataSheet.Cells(vDataRow, 1), Len(vDataSheet.Cells(vDataRow, 1)) - InStr(1, vDataSheet.Cells(vDataRow, 1), " "))
vFound = False
vRefRow = 1
While vRefSheet.Cells(vRefRow, 1) <> "" And Not vFound
vFirstSpace = InStr(1, vRefSheet.Cells(vRefRow, 1), " ")
vFromNumber = Left(vRefSheet.Cells(vRefRow, 1), vFirstSpace - 1)
vSecondspace = InStr(vFirstSpace + 1, vRefSheet.Cells(vRefRow, 1), " ")
vToNumber = Mid(vRefSheet.Cells(vRefRow, 1), vFirstSpace + 1, vSecondspace - vFirstSpace - 1)
vRefName = Right(vRefSheet.Cells(vRefRow, 1), Len(vRefSheet.Cells(vRefRow, 1)) - vSecondspace)
If vStreetNumber >= vFromNumber And vStreetNumber <= vToNumber And _
vStreetName = vRefName Then
vFound = True
End If
vRefRow = vRefRow + 1
Wend
If vFound Then
vDataSheet.Cells(vDataRow, 2) = "MATCH"
Else
vDataSheet.Cells(vDataRow, 2) = "NO MATCH"
End If
vDataRow = vDataRow + 1
Wend
End Sub
Reference data on Sheet2 Sheet2上的参考数据
Matching outcome on Sheet1 Sheet1上的匹配结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.