2015-06-18 130 views
0

我有一个完美的搜索功能,可以完美地搜索精确的数值,但是我需要对其进行调整,以便在单元格内搜索文本并仅提取文本。例如,它搜索第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 
+0

请仅分享与您的问题相关的部分代码。首先想到我会建议'instr()'或正则表达式,你试过了吗? –

+0

我已编辑回复您的帖子的问题。它基本上是定义正在搜索的内容的代码的一部分,我需要进行一些解释。这个代码在搜索多个数值时工作得很好,但我正在寻找适应它来搜索和提取我正在处理的另一个项目的某些文本值。 – SeanBaird

+0

您可以将数组定义为按照您为数字定义的方式进行搜索。要搜索部分单元格内容,您需要将'.Find(SearchTarget(i),LookAt:= xlWhole)'改为'.Find(SearchTarget(i),LookAt:= xlPart)'。如果这不是您需要的解决方案,请添加一些示例数据和所需的输出以便更容易理解您的问题。 –

回答

1

可以定义阵列搜索方式一样定义它为数字。

要搜索部分单元格内容,您需要将.Find(SearchTarget(i), LookAt:=xlWhole)更改为.Find(SearchTarget(i), LookAt:=xlPart)

VBA在公式/结果中查找的方式与查找/替换对话框中的相同。 (将.LookIn设置为xlValuesxlFormulas

+0

我会添加。看起来像这样'.find(SearchTarget(i),LookAT:= xlPart,lookinxlValues?' – SeanBaird

+0

@seanbaird:'.find(SearchTarget(i),LookAT:= xlPart,lookin:= xlValues) –

相关问题