[英]Merge 2 columns and find text VBA
我有一张桌子和一个文件。 我可以在表格内的文件中找到特定位置的文本。 但是,文本并非一直都是唯一的,所以我决定在文件中合并 2 个单元格并尝试在表格中查找。 除非,我找不到一种方法来组合表中的 2 列以将其与文件中组合的 2 个单元格匹配。
您可能会在下面看到示例表。
我的目标是在单元格的下一个单元格中添加日期。 所以我尝试找到 A1234 而不是 1234,因为 1234 不是唯一的。
FindString = wb.Sheets("1").Range("E4").Value & wb.Sheets("1").Range("E5").Value
If Trim(FindString) <> "" Then
With Wb2.Sheets("Sheet1").Range("A:A") 'this section need to be amended and need combine column A&B
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Rng.Offset(0, 1).Value = wb.Sheets("1").Range("I4") ' if column A&B combining completed then next cell probably will not work
Else
MsgBox "Nothing found in the list"
End If
这类似于评论中提到的变体策略——尝试使用 For 循环和 If 语句遍历您的数据,以查找要匹配的两个值。 这是一个显示概念的示例代码
Sub test()
Dim s As Worksheet, findstring1 As String, findstring2 As String
Dim firstrow As Integer, lastrow As Integer, i As Integer
Set s = Sheets("test")
findstring1 = "A "'replace this with the Customer reference (what to search for)
findstring2 = "1234" 'replace this with the unit reference
firstrow = 2 ' row number for first cell with data
lastcell = s.Cells(2, 1).End(xlDown).Row 'find last cell row number (end of data)
For i = firstrow To lastcell
If s.Cells(i, 1) = findstring1 And s.Cells(i, 2) = findstring2 Then
'do something with found values
End If
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.