匹配两个列,并在第1列旁边的第1列旁边的列中匹配第2列的第1列
尝试在VBA中解决此问题,我在这里检查了一些线程,但是无论我尝试的哪种变体我似乎都会遇到我无法解决的错误。
1张1 col A具有与纸2 col a匹配的值,如果匹配,则我想在1 col A中返回的值包含在2 col B中(col A旁边的单元格中的col A中的单元格)中。
目标是:
- Check Septh1 Col A VS Sheet2 Col A进行比赛。
- 如果匹配,则输出Sheet2 Col B(Sheep2 Col A偏移量(0,1))到Sheet1 Col B((Seept1 Col A偏移量(0,1))。
因此,基本上要匹配Sheet1 Col A与Sheet2 Col A,输出匹配从Sheet2 Col B到Sheet1 Col B的行值
(我感谢有错误,只是尝试显示逻辑):
'In this first one I'm trying to iterate through the rows to find the matches and then output offset 0,1 to offset 0,1
Sub loopMatch()
ReDim colA(1 To 22) As Variant
ReDim colB(1 To 27) As Variant
sheet1colA = Worksheets("sheet1").Range("A3:A25").Value
sheet2colA = Worksheets("sheet2").Range("A2:A27").Value
Dim rowCounter1 As Integer
Dim rowCounter2 As Integer
Dim valueFound As Boolean
Dim valueLost As String
valueLost = "The following item(s) are unmatched: "
For rowCounter1 = LBound(sheet1colA) To UBound(sheet1colA)
For rowCounter2 = LBound(sheet2colA) To UBound(sheet2colA)
If sheet2colA(rowCounter2, 1) = sheet1colA(rowCounter1, 1) Then
valueFound = True
Worksheets("sheet1colA").Offset(0, 1).Value = Worksheets("sheet2colA").Offset(0, 1).Value
End If
Next rowCounter2
If valueFound = False Then
valueLost = valueLost & sheet2colA(rowCounter1, 1) & "; "
End If
valueFound = False
Next rowCounter1
也尝试了类似的东西:
'This one is just a match search and attempt to output per offset
Sub LoopMatch1()
Dim match
match = Application.match(Worksheets("sheet1").Range("A3:A25").Value, Worksheets("sheet2").Range("A2:A27"), 0)
If Not IsError(match) Then
Worksheets("sheet1").Range("A3:A25").Offset(0, 1).Value = Worksheets("sheet2").Range("A2:A27" & match).Offset(0, 1).Value
Else
Worksheets("sheet1").Range("A3:A25").Offset(0, 1).Value = "Not Found"
End If
任何人对此有更好的了解?
Trying through this problem in VBA, I've checked a few of the threads here but whatever variant I try I seem to encounter errors I can't resolve.
Sheet 1 Col A has values to match with Sheet 2 Col A, and if matched then the value I want to return in Sheet 1 Col A is contained in Sheet 2 Col B (cell next to value in Col A).
The goal is to:
- Check Sheet1 Col A vs Sheet2 Col A for matches.
- If Match, output Sheet2 Col B (Sheet2 Col A Offset(0, 1)) to Sheet1 Col B ((Sheet1 Col A Offset(0, 1)).
And so basically to match Sheet1 Col A to Sheet2 Col A, output matched row values from Sheet2 Col B to Sheet1 Col B.
Current attempts (I appreciate there are errors contained, just trying to show logic):
'In this first one I'm trying to iterate through the rows to find the matches and then output offset 0,1 to offset 0,1
Sub loopMatch()
ReDim colA(1 To 22) As Variant
ReDim colB(1 To 27) As Variant
sheet1colA = Worksheets("sheet1").Range("A3:A25").Value
sheet2colA = Worksheets("sheet2").Range("A2:A27").Value
Dim rowCounter1 As Integer
Dim rowCounter2 As Integer
Dim valueFound As Boolean
Dim valueLost As String
valueLost = "The following item(s) are unmatched: "
For rowCounter1 = LBound(sheet1colA) To UBound(sheet1colA)
For rowCounter2 = LBound(sheet2colA) To UBound(sheet2colA)
If sheet2colA(rowCounter2, 1) = sheet1colA(rowCounter1, 1) Then
valueFound = True
Worksheets("sheet1colA").Offset(0, 1).Value = Worksheets("sheet2colA").Offset(0, 1).Value
End If
Next rowCounter2
If valueFound = False Then
valueLost = valueLost & sheet2colA(rowCounter1, 1) & "; "
End If
valueFound = False
Next rowCounter1
Also tried something like:
'This one is just a match search and attempt to output per offset
Sub LoopMatch1()
Dim match
match = Application.match(Worksheets("sheet1").Range("A3:A25").Value, Worksheets("sheet2").Range("A2:A27"), 0)
If Not IsError(match) Then
Worksheets("sheet1").Range("A3:A25").Offset(0, 1).Value = Worksheets("sheet2").Range("A2:A27" & match).Offset(0, 1).Value
Else
Worksheets("sheet1").Range("A3:A25").Offset(0, 1).Value = "Not Found"
End If
Anyone have a better idea of this? Quite new to VBA.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论