简体   繁体   English

Excel VBA到单元格的vlookup部分

[英]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

This code isn't very flexible, and has some game breaking limitations, but it does do what you're asking. 这段代码不是很灵活,并且有一些突破游戏的限制,但是确实可以满足您的要求。

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: 一些限制如下:

  1. If you have something like Green Apple, it won't find the value because of the Space. 如果您拥有绿色苹果之类的产品,那么由于空间的原因,它将找不到价值。 This can be easily fixed by using Replace() . 这可以通过使用Replace()轻松解决。
  2. If you have something like Watermelon, and another item as Melon, it's going to give the Melon ID # to both. 如果您有西瓜之类的东西,而瓜类又有另一种东西,它将为两者都提供瓜ID#。 Some fancier coding (a bit of it actually) would be needed to avoid this. 为避免这种情况,将需要一些更高级的编码(实际上是一些编码)。
  3. There's a few other issues that may come up depending on the values you're using but they are for the most part small edits to the code. 根据您所使用的值,可能还会出现其他一些问题,但是大多数情况下,它们只是对代码的少量修改。 The above two issues (namely 2.) is going to be pretty difficult to avoid.. 以上两个问题(即2)将很难避免。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM