我从事采购工作,我正在尝试查看包含零件编号及其描述的单元格列表,但只从单元格中提取零件编号并将其放在另一列中。
部件号具有10个可能的前缀之一(即ABC,DDA,GHF,AH等)。细胞可能看起来像这样:
我希望能够搜索整个列表,只将以下信息提取到另一列:
从上面可以看出,挑战在于零件编号的长度各不相同,并且没有特定的约定。你唯一知道的就是他们可以拥有十个可能的前缀之一,如上所述(ABC,GHF等)。
我目前的解决方案看起来像这样:
C2=FIND("ABC", A2)
D2=FIND(" ", A2, C2)
E2=MID(A2, C2, D2)
当单元格A2包含完整的部件号和描述时,C2通过搜索其前缀找到部件号的起始位置,D2通过搜索空格找到部件号的结束位置,然后E2拉出子串在这两个地点之间。
正如您所看到的,这不是一个非常强大的解决方案,它只允许我搜索以ABC开头的部分。我希望能够搜索所有10个可能的前缀(ABC,DDA,GHF,AH等),但这不起作用。我尝试了以下方法:
C2=FIND({"ABC", "DDA", "GHF", "AH"}, A2)
但是只搜索ABC部分并忽略其他前缀。我们非常感谢您提供的任何帮助!!
答案 0 :(得分:0)
这不是最漂亮的公式,但它应该让你顺利。如果您对宏而不是公式感兴趣,那么肯定是VBA解决方案。
假设您的Tire Pressure ABC123873 Monitor
在A1中,请将其输入B1:
=MID(SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1),SEARCH("ABC",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1)),SEARCH(" ",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1))-SEARCH("ABC",SUBSTITUTE(SUBSTITUTE(A1," ",";",1)," ","-",1)))
它是如何工作的,需要你的A1,并首先使用Substitute
。我将第一个和第二个[空格]替换为;
和-
。 (注意,如果有多于两个的空格,这将遇到问题)。然后,我只使用Mid
从ABC
开始提取单元格的一部分,直到它到达空间。如果你没有" ABC"启动所需的值,您需要调整Search("ABC"...
部分(或在那里使用数组)。我仍在努力,但希望这有助于提出一个想法。
答案 1 :(得分:0)
This is how I would do it. I created a list of prefixes to search in column G. You could even put them on a separate page to keep it cleaner.
=MID(A1,FIND(G1:G2,A1,1),FIND(" ", A1, FIND(G1:G2, A1, 1))-FIND(G1:G2,A1,1))
I only tested two prefixes but just change G1:G2
to G1:G10
and put all you prefixes in them. The formula looks in A1. The first FIND
finds the beginning character number. The rest of the equation is calculating the end of the product number minus the beginning which returns the amount of characters to return. Its important to know that MID
isn't wanting the beginning and end character. It wants the beginning character and the number of characters after that to return. So FIND(" ", FIND(G1:G2, A1, 1))
does not work as it returns too many characters. Give it a try.
Oh and don't forget to do Ctrl+Shift+Enter
to enter the array formula
The VBA answer would be
Sub findProductCode()
' Variable Declarations
Dim productName As String
Dim productArray() As String
Dim wordCount As Integer
' Sets the active sheet to use
' Change Sheet1 to the name of your sheet
With Sheets("Sheet1")
' Selects the first cell to check
' Change A1 to the first cell with data
Range("A1").Select
' Loops through all rows until an empty row
' It will end if you have any empty rows in the midle of data
Do Until IsEmpty(ActiveCell)
' Reads text out of the cell
'Change A to the proper column
productName = Range("A" & ActiveCell.Row).Text
' Splits the string into individual words
productArray = Split(productName, " ")
' Loops through array to find the product number
For wordCount = LBound(productArray) To UBound(productArray)
' Check if the word has the desired 10 prefixes
' Add more Or Left(productArray(wordCount), 3) = "xxx" until you have 10 of your prefixes you need
If Left(productArray(wordCount), 3) = "ABC" Or Left(productArray(wordCount), 3) = "GHF" Then
' Sends the product number to its cell
' Change B to the correct destination Cell
Range("B" & ActiveCell.Row).Value = productArray(wordCount)
End If
Next
' Increments the active cell to the next cell down
ActiveCell.Offset(1, 0).Select
Loop
End With
End Sub
If you don't know how, you need to enable the developer tab in excel. File->Options->Customize Ribbon
Add the Developer to the Ribbon. The save your worksheet as .xlsm
It is the macro enabled workbook. Then go to the developer tab, Choose "Visual Basic" and double click "ThisWorkBook". Paste the code in. You can run the code from there for a one time deal or you can create a button on the sheet that you can run this macro whenever you want.
To add a button, go back to the sheet you want the button on. On the developer tab select Insert
and choose the first form control Button
. Draw the button on your sheet and a dialog box will appear. select the findProductCode
macro and select OK. Now every time you click the button it will run the macro.