- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 5# 198188
姓名-職業欄 字尾加*可搜查含此字串的資料
如圖 Sheet2 的程式碼
- Option Explicit
- Private Sub Worksheet_Change(ByVal Target As Range) '這是工作表的觸發事件
- Dim xlFind As Range, F As String, W As String
- Application.EnableEvents = False 'EnableEvents 屬性 如果指定物件能觸發事件,則本屬性為 True。讀/寫 Boolean。
- If Target.Row = 2 Then '改變輸入(資料)的儲存格列位=2
- If Target.Column >= 1 And Target.Column <= 7 Then '改變輸入(資料)的儲存格欄位介於 A欄:G欄 間
- 'If Target.Row = 2 And Target.Column >= 1 And Target.Column <= 7 Then '兩判斷式 可合併
- Cells(Rows.Count, "A").End(xlUp).CurrentRegion.Offset(1) = "" '清除舊有尋找的資料
- W = Replace(Target, "*", "") '去掉 "*"字串
- Set xlFind = Sheets("資料庫").Columns(Target.Column).Find(W, LOOKAT:=IIf(InStr(Target, "*"), xlPart, xlWhole))
- '在Sheets("資料庫").Columns(Target.Column) 的相同欄位中Target有"*" 尋找有xlPart(部份)相同
- If Not xlFind Is Nothing Then '尋找到
- F = xlFind.Address '設下第一個找到的位置
- Do
- With Cells(Rows.Count, "A").End(xlUp).Offset(1)
- Cells(.Row, "A") = xlFind.Parent.Cells(xlFind.Row, "A") 'xlFind.Parent: Parent 物件的父層
- Cells(.Row, "B") = xlFind.Parent.Cells(xlFind.Row, "B") 'xlFind.Row: 找到的列號
- Cells(.Row, "C") = xlFind.Parent.Cells(xlFind.Row, "C")
- ' Cells(.Row, "C") 前面沒加 . 是在這Sheet 的 Cells(儲存格)
- End With
- Set xlFind = Sheets("資料庫").Columns(Target.Column).FindNext(xlFind) '接著往下找
- Loop While F <> xlFind.Address '離開迴圈: 直到尋找回第一個找到的位置
- End If
- End If
- End If
- Application.EnableEvents = True
- End Sub
複製代碼 |
|