- 帖子
- 522
- 主題
- 36
- 精華
- 1
- 積分
- 603
- 點名
- 0
- 作業系統
- win xp sp3
- 軟體版本
- Office 2003
- 閱讀權限
- 50
- 性別
- 男
- 註冊時間
- 2012-12-13
- 最後登錄
- 2021-7-11
|
2#
發表於 2015-11-6 17:50
| 只看該作者
本帖最後由 yen956 於 2015-11-6 17:52 編輯
試試看:
Q1:- Private Sub CommandButton1_Click()
- Dim sCel As Range
- Dim inTxt, First1 As String
- Dim LastRow As Long
- Range("K2:N" & Rows.Count & "").ClearContents '清除先前搜尋的資料"
- inTxt = InputBox("請輸入搜尋班級", "搜尋班級")
- If inTxt = "" Then Exit Sub '若使用者按 [取消] 則離開
- Set sCel = [A:A].Find(What:=inTxt, LookAt:=xlWhole)
- If sCel Is Nothing Then
- MsgBox ("未找到你要搜尋的班級"), vbCritical
- Exit Sub
- End If
- First1 = sCel.Address '保留第一個搜尋到的位址
- Do
- LastRow = Cells(Rows.Count, 11).End(xlUp).Row + 1
- sCel.Resize(1, 4).Copy Cells(LastRow, 11)
- Set sCel = [A:A].FindNext(sCel) '尋找下一個
- Loop Until First1 = sCel.Address '下一個的位置=第一個的位置(回到第一個的位置)
- End Sub
複製代碼 Q2:- Private Sub Worksheet_Change(ByVal Target As Range)
- Dim sh1, sh2 As Object
- Dim sCel As Range
- Dim First1 As String
- Dim LastRow As Long
- Set sh1 = Sheets("工作表1")
- Set sh2 = Sheets("工作表2")
- If Intersect(Target, sh2.[I1]) Is Nothing Then Exit Sub
- If Target = "" Then Exit Sub
- sh2.Range("H4:K" & sh2.Rows.Count & "").ClearContents '清除先前搜尋的資料"
- sh1.Activate
- With sh1
- Set sCel = .[A:B].Find(What:=Target, LookAt:=xlPart)
- If sCel Is Nothing Then
- MsgBox ("未找到你要搜尋的資料"), vbCritical
- Exit Sub
- End If
- First1 = sCel.Address '保留第一個搜尋到的位址
- Do
- LastRow = sh2.Cells(sh2.Rows.Count, 8).End(xlUp).Row + 1
- sCel.Resize(1, 4).Copy sh2.Cells(LastRow, 8)
- Set sCel = .[A:B].FindNext(sCel) '尋找下一個
- Loop Until First1 = sCel.Address '下一個的位置=第一個的位置(回到第一個的位置)
- End With
- sh2.Activate
- End Sub
複製代碼 |
|