简体   繁体   English

从单元格中提取文本

[英]Extracting Text from a cell

I have a search function which works perfectly for searching for Exact Numerical values, However I need to adapt it so it searches for text within a cell and only extracts that text. 我有一个搜索功能,可以完美地搜索精确数值,但是我需要对其进行调整,以便它在一个单元格中搜索文本并仅提取该文本。 For example it searches column 7. In column 7 there may be a cell containing the words Interface - HPT, SAS, LPT Ideally I would like to search for the word Interface - HPT then extract Only this text from the cell. 例如,它搜索第7列。在第7列中,可能会有一个包含“接口-HPT,SAS,LPT”一词的单元。理想情况下,我想搜索“接口-HPT”一词,然后仅从该单元中提取此文本。 I also need the search function to be able to do this for multiple different values. 我还需要搜索功能才能针对多个不同的值执行此操作。 So for example run a search for Interface - HPT Interface - SAS and Interface LPT separate from each other. 因此,例如,分别搜索接口-HPT接口-SAS和接口LPT。 Is this Possible ? 这可能吗 ?

Here is the code I have at the moment: 这是我目前的代码:

Sub InterfaceMacro()

Dim Headers() As String: Headers = _
    Split("Target FMECA,Part I.D,Line I.D,Part No.,Part Name,Failure Mode,Assumed System Effect,Assumed Engine Effect", ",")

    Worksheets.Add().Name = "Interface"
    Dim wsInt As Worksheet: Set wsInt = Sheets("Interface")
    wsInt.Move after:=Worksheets(Worksheets.Count)
    wsInt.Cells.Clear

    Application.ScreenUpdating = False

    With wsFHA
        For i = 0 To UBound(Headers)
            .Cells(2, i + 2) = Headers(i)
            .Columns(i + 2).EntireColumn.AutoFit
        Next i
        .Cells(1, 2) = "Interface TABLE"
        .Range(.Cells(1, 2), .Cells(1, UBound(Headers) + 2)).MergeCells = True
        .Range(.Cells(1, 2), .Cells(1, UBound(Headers) + 2)).HorizontalAlignment = xlCenter
        .Range(.Cells(1, 2), .Cells(2, UBound(Headers) + 2)).Font.Bold = True
    End With

    Dim SourceCell As Range, FirstAdr As String
    Dim RowCounter As Long: RowCounter = 3

    Dim SearchTarget() As String
    SearchTarget = Split("9.1,18.0", ",")

    For i = 0 To UBound(SearchTarget)
        If Worksheets.Count > 1 Then
            For j = 1 To Worksheets.Count - 1
            With Sheets(j)
                Set SourceCell = .Columns(7).Find(SearchTarget(i), LookAt:=xlWhole)
                If Not SourceCell Is Nothing Then
                    FirstAdr = SourceCell.Address
                    Do
                        wsInt.Cells(RowCounter, 2).Value = SearchTarget(i)
                        wsInt.Cells(RowCounter, 3).Value = .Cells(SourceCell.Row, 6).Value
                        wsInt.Cells(RowCounter, 4).Value = .Cells(3, 10).Value
                        wsInt.Cells(RowCounter, 5).Value = .Cells(2, 10).Value
                        wsInt.Cells(RowCounter, 6).Value = .Cells(SourceCell.Row, 2).Value
                        For k = 0 To SourceCell.Row - 1
                            If .Cells(SourceCell.Row - k, 3).Value <> "continued." Then
                                wsFHA.Cells(RowCounter, 7).Value = .Cells(SourceCell.Row - k, 3).Value
                                Exit For
                            End If
                        Next k
                        wsInt.Cells(RowCounter, 8).Value = .Cells(SourceCell.Row, 14).Value
                        Set SourceCell = .Columns(7).FindNext(SourceCell)
                        RowCounter = RowCounter + 1
                    Loop While Not SourceCell Is Nothing And SourceCell.Address <> FirstAdr
                End If
            End With
            Next j
        End If
    Next i


    End Sub

The part I believe needs editing is this section 我认为需要编辑的部分是此部分

Dim SourceCell As Range, FirstAdr As String
        Dim RowCounter As Long: RowCounter = 3

        Dim SearchTarget() As String
        SearchTarget = Split("9.1,18.0", ",")

        For i = 0 To UBound(SearchTarget)
            If Worksheets.Count > 1 Then
                For j = 1 To Worksheets.Count - 1
                With Sheets(j)
                    Set SourceCell = .Columns(7).Find(SearchTarget(i), LookAt:=xlWhole)
                    If Not SourceCell Is Nothing Then
                        FirstAdr = SourceCell.Address

You can define the array to search the same way as you define it for numbers. 您可以定义数组以与定义数字相同的方式搜索。

To search also part of the cell content you need to change .Find(SearchTarget(i), LookAt:=xlWhole) to .Find(SearchTarget(i), LookAt:=xlPart) . 要同时搜索单元格内容的一部分,您需要将.Find(SearchTarget(i), LookAt:=xlWhole)更改为.Find(SearchTarget(i), LookAt:=xlPart)

VBA looks in formulas / results the same way as it works in Find / Replace dialog. VBA在公式/结果中的查找方式与在“查找/替换”对话框中相同。 (set .LookIn to either xlValues or xlFormulas ) (将.LookIn设置为xlValuesxlFormulas

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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