EXCEL VBA.依查詢〔編號〕將資料載入〔輸入表〕並可〔上下筆〕輪動
<.准提部林.>
---------------------------------
範例檔簡介:
■程式碼:以下為〔第2版〕內容
Dim MyRng As Range, FindVal$
'↑加入〔頂端〕宣告變數
'============================
Sub 搜尋_第一筆()
If [D4] = "" Then MsgBox "未輸入〔病歷號〕!": Exit Sub
FindVal = [D4]: [P4] = ""
Set MyRng = [資料表!B:B].Find(FindVal, Lookat:=xlWhole)
If MyRng Is Nothing Then MsgBox "該編號未登錄過!": Exit Sub
[P4] = "=COUNTIF(資料表!B3:B" & MyRng.Row & ",D4)"
[P4] = [P4].Value
Call 搜尋_載入資料
End Sub
'============================
Sub 搜尋_下一筆()
If [D4] = "" Then MsgBox "未輸入〔病歷號〕!": Exit Sub
If MyRng Is Nothing Or FindVal <> [D4] Then
Set MyRng = Sheets("資料表").[B3]
FindVal = [D4]
End If
[P4] = ""
Set MyRng = [資料表!B:B].Find(FindVal, after:=MyRng, SearchDirection:=xlNext)
If MyRng Is Nothing Then MsgBox "該編號未登錄過!": Exit Sub
[P4] = "=COUNTIF(資料表!B3:B" & MyRng.Row & ",D4)"
[P4] = [P4].Value
Call 搜尋_載入資料
End Sub
'============================
Sub 搜尋_上一筆()
If [D4] = "" Then MsgBox "未輸入〔病歷號〕!": Exit Sub
If MyRng Is Nothing Or FindVal <> [D4] Then
Set MyRng = Sheets("資料表").[B65536]
FindVal = [D4]
End If
[P4] = ""
Set MyRng = [資料表!B:B].Find(FindVal, after:=MyRng, SearchDirection:=xlPrevious)
If MyRng Is Nothing Then MsgBox "該編號未登錄過!": Exit Sub
[P4] = "=COUNTIF(資料表!B3:B" & MyRng.Row & ",D4)"
[P4] = [P4].Value
Call 搜尋_載入資料
End Sub
■說明:第2版新功能
1.搜尋時可〔上.下筆〕一直循環。
2.在儲存格〔P4〕會顯示目前是〔第?筆〕。
3.每次〔新查詢〕輸入編號後,按〔上一筆〕即可查到〔最後一筆〕,
以利叫出〔最新〕的一筆資料。
---------------------------------
<範例檔>第1版:
<範例檔>第2版:
---------------------------------
- Aug 17 Fri 2012 13:12
Excel-VBA-從另外一個sheet多筆資料找出下一筆
close
LV超3A名牌購物網
全站熱搜
留言列表