用于搜索行不断更改的列中的特定文本的宏脚本

时间:2017-10-18 14:16:10

标签: excel-vba vba excel

我正在尝试创建一个将通过列的宏让我们说列A并且在该列中有一行包含文本“cpu利用率为xx%”,如果宏可以剥离该行“XX%”并将此值放入同一工作簿中的任何位置。

请注意,行未固定,文本保持不变,但行将更改且列保持不变

Sub Test() 
Dim ws As Worksheet 
Dim rFind As Range 
Set ws = ThisWorkbook.Worksheets("sheet2") 
Set rFind = Range("A:A").Find("*TOTAL*", LookIn:=xlValues) 
Worksheets("sheet1").Range("B11") = rFind 
End Sub

1 个答案:

答案 0 :(得分:1)

这是一个非常简单的,所以我真的很快就做到了。请记住,像Gilles说的那样,请务必在发布之前向您展示一些努力或者您的问题可能会被关闭:有关详细信息,请参阅https://stackoverflow.com/help/mcve

Sub FindPhrase()
Dim i As Long, searchCol As Long, dCell As Range, ws As Worksheet

Set ws = ActiveSheet

'column A
searchCol = 1

'output cell
Set dCell = ws.Range("B1")

'search through cells in specified column
For i = 1 To ws.Cells(ws.Rows.Count, searchCol).End(xlUp).Row
    If InStr(ws.Cells(i, searchCol), "The cpu utilization is ") > 0 And InStr(ws.Cells(i, searchCol), "%") > 0 Then
        dCell.Value = CDbl(Replace(Mid(ws.Cells(i, searchCol), InStr(ws.Cells(i, searchCol), "%") - 2, 2), "%", ""))
        Exit For
    End If
Next i

End Sub