VBA|包含指定字符串的單元格自動篩選到指定區域

有工作表schedule:

VBA|包含指定字符串的單元格自動篩選到指定區域

以及工作表team:

VBA|包含指定字符串的單元格自動篩選到指定區域

在工作表team選定某一支球隊所在單元格,運行VBA過程即可在工作表schedule篩選數據複製到工作表team的右邊區域。如上圖所示:

VBA過程代碼:

VBA|包含指定字符串的單元格自動篩選到指定區域

核心代碼非常簡單,就是一個循環內一個選擇結構,用VBA函數InStr()判斷單元格是否包含指定字符串。

還可以在工作表team添加事件過程,當單元格選擇改變時,自動運行find()過程,代碼如下:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Call findtxt

End Sub

附代碼:

Sub findtxt()

Dim txt As String

Dim i, j, m As Integer

m = 2

txt = Trim(ActiveCell.Value)

If txt <> "" Then

Range("J2:M22") = ""

End If

For j = 2 To 64

i = InStr(Range("schedule!D" & j).Value, txt)

If i > 0 And txt <> "" Then

Range("J" & m).Value = Range("schedule!B" & j).Value

Range("K" & m).Value = Range("schedule!C" & j).Value

Range("L" & m).Value = Range("schedule!D" & j).Value

Range("M" & m).Value = Range("schedule!E" & j).Value

m = m + 1

End If

Next

End Sub

-End-


分享到:


相關文章: