[英]Excel VBA to vlookup part of a cell
I want to create a Excel VBA macro that looks for "a123Apple873hhh" and knows that I just wanted to look for "Apple". 我想创建一个查找“ a123Apple873hhh”的Excel VBA宏,并且知道我只想查找“ Apple”。
It's easier to understand on an example: 在一个示例上更容易理解:
On sheet1 I have my fixed table array with a name and its code: 在sheet1上,我有一个固定的表数组,上面有一个名称及其代码:
Column A---Column B
12------ --Banana
20-------- Apple
44-------- Orange
On sheet2 I have what I want to look for: 在sheet2上,我有想要查找的东西:
Column A----------Column B
.......... -------ds$$Orange1111aaa
.......... -------22Apple999
.......... -------22Watermelon
.......... -------9q9Orange7ab
etc...
I want a VBA that looks on sheet2/Column B, finds what name is on sheet1/Column B and give its code on sheet2/Column A. So, the final result is: 我想要一个在sheet2 / Column B上查找,在sheet1 / Column B上找到什么名称并在sheet2 / Column A上给出其代码的VBA。因此,最终结果是:
Column A------Column B
44 -----------ds$$Orange1111aaa
20 -----------22Apple999
*BLANK* ------22Watermelon
44 -----------9q9Orange7ab
etc...
My code don't work because it just find exact results: 我的代码不起作用,因为它只能找到确切的结果:
Sub FindCode()
Const COLUMN As String = "E"
Dim i As Long
Dim iLastRow As Long
Dim cell As Range
Dim sh As Worksheet
With ActiveSheet
iLastRow = .Cells(.Rows.Count, COLUMN).End(xlUp).Row
For i = 6 To iLastRow
If .Cells(i, "E") = "" Then
.Cells(i, "A").Value = ""
Else
.Cells(i, "A").Value = Application.VLookup(.Cells(i, "E").Value, Range("etc!A:B"), 2, False)
End If
Next i
End With
End Sub
I used the exact same data you provided. 我使用了您提供的完全相同的数据。 Sheet1 look like this: Sheet1看起来像这样:
Sheet2 is as follows: Sheet2如下:
I used this code 我用了这段代码
Sub SearchProduct()
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets(1)
Dim ws2 As Worksheet: Set ws2 = ThisWorkbook.Sheets(2)
Dim fruit As Range: Set fruit = ws1.Range("B2", ws1.Cells(ws1.Rows.Count, "B").End(xlUp))
Dim fruitCode As Range: Set fruitCode = ws2.Range("B2", ws2.Cells(ws2.Rows.Count, "B").End(xlUp))
Dim f As Range, s As Range
For Each s In fruit
For Each f In fruitCode
If InStr(s.Text, f.Text) <> 0 Then
s.Offset(0, -1).Value = f.Offset(0, -1).Value
GoTo SkipTheRest
End If
Next f
SkipTheRest:
Next s
End Sub
Which yielded the following result on Sheet2 在Sheet2上产生以下结果
Some of the limitations are as follows: 一些限制如下:
Replace()
. 这可以通过使用Replace()
轻松解决。 this should do: 这应该做:
Option Explicit
Sub main()
Dim fruitRng As Range, cell As Range, found As Range
Dim firstAddress As String
With Worksheets("Sheet1")
Set fruitRng = .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants)
End With
With Worksheets("Sheet2")
With .Range("B1", .Cells(.Rows.Count, 2).End(xlUp)).SpecialCells(XlCellType.xlCellTypeConstants)
For Each cell In fruitRng
Set found = .Find(what:=WorksheetFunction.Trim(cell.Value), lookat:=xlPart, LookIn:=xlValues)
If Not found Is Nothing Then
firstAddress = found.Address
Do
found.Offset(, -1).Value = cell.Offset(, -1).Value
Set found = .FindNext(found)
Loop While found.Address <> firstAddress
End If
Next cell
.Offset(, -1).SpecialCells(xlCellTypeConstants, xlTextValues).ClearContents
End With
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.