使用Find
搜索2個值,不是「內置的」,您需要編寫一些額外的代碼。
下面的代碼使用Find
函數來查找範圍內的所有「Average」。所以你需要運行另一個循環來找到「StDev」。
Sub FindAverage()
Dim c As Range
Dim col As Long
Dim firstAddress As String
With Worksheets("Sheet3").Rows(7)
Set c = .Find("*Average*", LookIn:=xlValues)
If Not c Is Nothing Then
col = 1
firstAddress = c.Address
Columns(c.Column).Copy Destination:=Sheets("Sheet4").Columns(col)
Do
Set c = .FindNext(c)
If c Is Nothing Then
Exit Sub
End If
C.EntireColumn.Copy Destination:=Sheets("Sheet4").Columns(col)
col = col + 1
Application.CutCopyMode = False
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
選項2:使用佔用範圍內的普通循環中第7行:
Sub FindAverageansStdev()
Dim C As Range
Dim col As Long, LastCol As Long
With Worksheets("Sheet3")
LastCol = .Cells(7, .Columns.Count).End(xlToLeft).Column '<-- get last column with data in row 7
col = 1
For Each C In .Range(.Cells(7, 1), .Cells(7, LastCol)) ' <-- loop through occupied cells only in row 7
If C.Value Like "*Average*" Or C.Value Like "*StDev*" Then
C.EntireColumn.Copy Destination:=Sheets("Sheet4").Columns(col)
' option to pastespecial (values only)
C.EntireColumn.Copy
Sheets("Sheet4").Columns(col).PasteSpecial xlPasteValues
col = col + 1
End If
Next C
End With
End Sub
你的意思containm細胞'Average'和'StDev'他們的公式?或者只是一個普通的'String'? –
嗨,只是一個普通的字符串Average和StDev是一些列的標題 – Dave