我有一個完美的搜索功能,可以完美地搜索精確的數值,但是我需要對其進行調整,以便在單元格內搜索文本並僅提取文本。例如,它搜索第7列。第7列中可能有一個單元格,其中包含單詞Interface - HPT,SAS,LPT理想情況下,我想搜索Interface - HPT一詞,然後從單元格中提取僅此文本。我還需要搜索功能才能對多個不同的值執行此操作。因此,例如運行搜索接口 - HPT 接口 - SAS和接口LPT彼此分開。這可能嗎 ?從單元格中提取文本
這裏是我此刻的代碼:
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
我認爲需要修改的部分是本節
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
請僅分享與您的問題相關的部分代碼。首先想到我會建議'instr()'或正則表達式,你試過了嗎? –
我已編輯回覆您的帖子的問題。它基本上是定義正在搜索的內容的代碼的一部分,我需要進行一些解釋。這個代碼在搜索多個數值時工作得很好,但我正在尋找適應它來搜索和提取我正在處理的另一個項目的某些文本值。 – SeanBaird
您可以將數組定義爲按照您爲數字定義的方式進行搜索。要搜索部分單元格內容,您需要將'.Find(SearchTarget(i),LookAt:= xlWhole)'改爲'.Find(SearchTarget(i),LookAt:= xlPart)'。如果這不是您需要的解決方案,請添加一些示例數據和所需的輸出以便更容易理解您的問題。 –