Excel在列中查找文本并在新列

时间:2017-03-03 11:52:14

标签: excel vba excel-vba

您好我有一个excel列(标题 - 详细信息),其中包含我要查找某些文字或词组的长文本,当它找到相应行中的新插入列时,应标记为YES 。

第二个目标是使用用户输入的文本重命名标题

我有'详情'仅限列用户运行宏并显示消息框以询问他想要查找的内容,然后无论用户输入什么,都会查看详细信息列,并插入带有YES标志的新列。所以例如在三次运行的宏用户中首先输入Peach Then Banana然后Apple和最终结果如下图所示。

任何人都可以通过macro-vb或vba

解决问题

enter image description here

我的代码到目前为止

Dim colNum As Integer
colNum = ActiveSheet.rows(1).Find(What:="Details", LookAt:=xlWhole).Column

     ActiveSheet.Columns(colNum + 1).Insert


ActiveSheet.Cells(1, colNum + 1).Value = "VARIABLE Entered by user"
    Dim colRange As Range

EndSub

我不能依赖公式作为专栏'详细信息'改变整个班级教师计算机中的职位

目前我遇到了部分匹配查询' WIN'标记单词' WIDOW'即使' N' N' N'不存在我希望如果我的所有查询字符都匹配在一个单词中(即使它们是完整的单词),我可以得到YES标志

enter image description here

4 个答案:

答案 0 :(得分:2)

以下是使用正则表达式的VBA解决方案,它允许简单设置字边界,以便区分,例如berryblueberry

如果速度是个问题,可以通过在VBA阵列中加速来加快速度。

我会留给你格式化列标题。

编辑:正则表达式不区分大小写

Option Explicit
Sub FlagWord()
    Dim R As Range, WS As Worksheet
    Dim RE As Object
    Dim C As Range, D As Range
    Dim S As String
    Dim I As Long, J As Long

S = InputBox("Enter desired word")

'Current filled in range
Set WS = Worksheets("sheet1")
With WS
    Set R = .Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    Set R = R.Resize(columnsize:=.Cells(1, .Columns.Count).End(xlToLeft).Column)
End With

If Not S = "" Then

'If S not present then add column
With WS.Rows(1)
    Set C = .Find(what:=S, after:=.Cells(1, 1), LookIn:=xlValues, _
        lookat:=xlWhole, searchorder:=xlByColumns, searchdirection:=xlNext, MatchCase:=False)
End With

'Add column if not already present
    If C Is Nothing Then
        Set R = R.Resize(columnsize:=R.Columns.Count + 1)
        R(1, R.Columns.Count) = S
    End If

End If 'no new column if S is blank

'do the word match
'Clear the data area
With R
    .Offset(1, 1).Resize(.Rows.Count - 1, .Columns.Count - 1).ClearContents
End With

'fill in the data
'use regex to allow for easy word boundaries
Set RE = CreateObject("vbscript.regexp")
With RE
    .Global = False 'only need a single match
    .ignorecase = True
    For Each C In R.Columns(1).Offset(1, 0).Resize(R.Rows.Count - 1).Cells
        For Each D In R.Rows(1).Offset(0, 1).Resize(columnsize:=R.Columns.Count - 1).Cells
        .Pattern = "\b" & D.Text & "\b"
            If .test(C.Text) = True Then
                R(C.Row, D.Column) = "YES"
            End If
        Next D
    Next C
End With

End Sub

答案 1 :(得分:1)

这是解决方案@ maxhob17只讨论使用Excel公式而不是VBA:

enter image description here

单元格B2中的公式为:

=IF(ISERROR(SEARCH(B$1,$A2)),"","YES")

将公式复制到其他单元格,然后就完成了。

答案 2 :(得分:1)

使用VBA表单结构。解决方案绝对不是一般的,但作为一个开始可能是好的

修改 现在它显示搜索词的输入窗口,并选择(最后 - 如果有更多)列,在该列中找到给定的术语

@app.route('/transactions', methods=['GET', 'POST'])
def transactions():
    if request.method == 'GET':
        return render_template('Transactions.html')
    sale = Transactions1(Item=request.form["Item"],Shack=request.form["Shack"],Paym_Reference=request.form["Paym_Reference"],Amount=request.form["Amount"])
    db.session.add(sale)
    db.session.commit()
    return render_template('Transactions.html')

<强> EDIT2:

Option Explicit
Sub Ingredients()
    Dim i As Integer, j As Integer
    Dim srcStr As String

    srcStr = Application.InputBox("Enter a searchterm")

    i = 2
    While (Cells(i, 1).Value <> "")
        j = 2
        While (Cells(1, j).Value <> "")
            If InStr(1, Cells(i, 1).Value, Cells(1, j).Value) Then Cells(i, j).Value = "YES"

            If Cells(1, j).Value = srcStr Then Columns(j).Select
            j = j + 1
        Wend
        i = i + 1
    Wend

End Sub

答案 3 :(得分:0)

    'you must break down your problem in to 2 parts
    '1. Find the word in the srting and store the name in the next column
    'for you must use VBA-built-in functoin InStr
    '2.Loop through next column, and use select-case logic to put the name in the corresponding column
    'for this you may use select-case statement or any other suitable loop and logic