简体   繁体   English

在 VBA 中搜索单元格文本并将文本复制到另一个单元格?

[英]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我有一个将其他列复制到另一个工作表的功能,但是对于此列,我需要执行以下操作

  • Search the cell and look for [P*]搜索单元格并查找 [P*]
  • The asterisk represents a number between 1 and 5星号代表 1 到 5 之间的数字
  • If it finds [P*] then copy P* to the sheet "Calculations" in column 4如果找到 [P*],则将 P* 复制到第 4 列中的“计算”表
  • Basically, remove everything from the cell except where there is a square bracket, followed by P, a number and a square bracket基本上,从单元格中删除所有内容,除了有方括号的地方,然后是 P、数字和方括号

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.我会编写一个函数,首先测试字符串中是否存在 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:在 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:需要注意的重要事项:

  • ? ? 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:如果 * 必须是 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.

相关问题 VBA将单元格值复制到另一个单元格文本字符串中 - VBA to copy cell value into another cell text string VBA:在单元格中搜索mm的书面文本并复制 - VBA: Search mm's written text in the cell and copy Excel VBA-搜索范围-如果单元格包含文本,则复制单元格-粘贴偏移量2,1 - Excel VBA - Search Range - If Cell Contains Text Then Copy Cell - Paste Offset 2,1 如果找到部分文本单元格,则复制Excel VBA - Copy if part of the text cell found excel VBA VBA:按单元格中的特定文本复制和粘贴 - VBA: copy and paste by specific text in a cell 使用VBA用另一个单元格中的文本替换单元格中的部分文本 - Replace part of text in a cell with text from another cell using VBA 在Excel VBA中将单元格中的文本与另一个单元格中的“ = text”进行比较 - Comparing text in cell with “=text” in another cell with excel VBA VBA用于将文本从单个未移动的单元格复制到粘贴到另一个单元格,每个粘贴/循环向下移动一个单元格 - VBA function to copy text from a single un-moving cell to paste to another cell, moving down one cell each paste/loop 如果同一行中的不同单元格包含文本,则将单元格复制到另一张工作表 - copy cell to another sheet if a different cell in the same row contains text 如何仅将单元格中的红色文本复制到另一个单元格? - How do I copy only the red text in a cell to another cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM