简体   繁体   中英

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]

W3 = [PROD][GD][1.0][P3]

W4 = Issues in production for customer

I have a function that is copying other columns into another sheet, however for this column, I need to do the following

  • Search the cell and look for [P*]
  • The asterisk represents a number between 1 and 5
  • If it finds [P*] then copy P* to the sheet "Calculations" in column 4
  • Basically, remove everything from the cell except where there is a square bracket, followed by P, a number and a square bracket

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.

Second Edit:

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

First EDIT:

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. 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

OLD POST:

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:

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:

=IF(ISNUMBER(SEARCH("[P?]",FirstSheet!$W2)), FirstSheet!$W2, "")

Important things to note:

  • ? is the wildcard symbol for a single character; you can use * if you're ok with multiple characters at that location
  • will display cell's original value if found, leave blank otherwise

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:

=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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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