[英]Search cell for text and copy text to another cell in VBA?
I've got a column which contains rows that have parameters in them.我有一列,其中包含带有参数的行。 For example例如
W2 = [PROD][FO][2.0][Customer] W2 = [生产][FO][2.0][客户]
W3 = [PROD][GD][1.0][P3] W3 = [生产][GD][1.0][P3]
W4 = Issues in production for customer W4 = 客户的生产问题
I have a function that is copying other columns into another sheet, however for this column, I need to do the following我有一个将其他列复制到另一个工作表的功能,但是对于此列,我需要执行以下操作
Does anyone know how I can do this?有谁知道我怎么能做到这一点? Alternatively, it might be easier to copy the column across and then remove everything that doesn't meet the above criteria.或者,可能更容易复制该列,然后删除不符合上述条件的所有内容。
I edited here to use a regular expression instead of a loop.我在这里编辑以使用正则表达式而不是循环。 This may be the most efficient method to achieve your goal.这可能是实现目标的最有效方法。 See below and let us know if it works for you:请参阅下文,让我们知道它是否适合您:
Function MatchWithRegex(sInput As String) As String
Dim oReg As Object
Dim sOutput As String
Set oReg = CreateObject("VBScript.RegExp")
With oReg
.Pattern = "[[](P[1-5])[]]"
End With
If oReg.test(sInput) Then
sOutput = oReg.Execute(sInput)(0).Submatches(0)
Else
sOutput = ""
End If
MatchWithRegex = sOutput
End Function
Sub test2()
Dim a As String
a = MatchWithRegex(Range("A1").Value)
If a = vbNullString Then
MsgBox "None"
Else
MsgBox MatchWithRegex(Range("A1").Value)
End If
End Sub
My solution would be something as follows.我的解决方案如下。 I'd write a function that first tests if the Pattern exists in the string, then if it does, I'd split it based on brackets, and choose the bracket that matches the pattern.我会编写一个函数,首先测试字符串中是否存在 Pattern,如果存在,我会根据括号对其进行拆分,并选择与模式匹配的括号。 Let me know if that works for you.让我知道这是否适合您。
Function ExtractPNumber(sInput As String) As String
Dim aValues
Dim sOutput As String
sOutput = ""
If sInput Like "*[[]P[1-5][]]*" Then
aValues = Split(sInput, "[")
For Each aVal In aValues
If aVal Like "P[1-5][]]*" Then
sOutput = aVal
End If
Next aVal
End If
ExtractPNumber = Left(sOutput, 2)
End Function
Sub TestFunction()
Dim sPValue As String
sPValue = ExtractPNumber(Range("A2").Value)
If sPValue = vbNullString Then
'Do nothing or input whatever business logic you want
Else
Sheet2.Range("A1").Value = sPValue
End If
End Sub
In VBA, you can use the Like
Operator with a Pattern to represent an Open Bracket, the letter P, any number from 1-5, then a Closed Bracket using the below syntax:在 VBA 中,您可以使用带有模式的Like
运算符来表示开括号、字母 P、1-5 之间的任何数字,然后使用以下语法表示闭括号:
Range("A1").Value LIke "*[[]P[1-5][]]*"
EDIT: Fixed faulty solution编辑:修复了错误的解决方案
If you're ok with blanks and don't care if *>5, I would do this and copy down column 4:如果你对空格没问题并且不在乎 *>5,我会这样做并复制第 4 列:
=IF(ISNUMBER(SEARCH("[P?]",FirstSheet!$W2)), FirstSheet!$W2, "")
Important things to note:需要注意的重要事项:
Afterwards, you can highlight the column and remove blanks if needed.之后,您可以突出显示该列并根据需要删除空白。 Alternatively, you can replace the blank with a placeholder string.或者,您可以用占位符字符串替换空白。
If * must be 1-5, use two columns, E and D, respectively:如果 * 必须是 1-5,则分别使用两列 E 和 D:
=MID(FirstSheet!$W2,SEARCH("[P",FirstSheet!$W2)+2,1)
=IF(AND(ISNUMBER($E2),$E2>0,$E2<=5,MID($W2,SEARCH("[P",FirstSheet!$W2)+3,1))), FirstSheet!$W2, "")
where FirstSheet is the name of your initial sheet.其中 FirstSheet 是初始工作表的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.