Public vD as object
'
Private Sub CommandButton1_Click()
If Application.FindFile = False Then
MsgBox "您沒有開啟母檔"
End If
Dim lRow&
if vD is nothing then Set vD = CreateObject("Scripting.Dictionary")
lRow = 3
vD.RemoveAll
While Cells(lRow, 1) <> ""
If Not vD.Exists(CStr(Cells(lRow, 1))) Then
EXCEL表單處理介面.ListBox1.AddItem CStr(Cells(lRow, 1))
vD(CStr(Cells(lRow, 1))) = lRow
End If
lRow = lRow + 1
Wend
End Sub作者: c_c_lai 時間: 2016-4-22 16:07
另有一個很多人使用的處理方法,
就是一開始就加個 On Error Resume Next,
這樣遇到錯誤就當做沒問題發生繼續跑下去,
運氣好就沒事...
但要是運氣不好的話...
比較正統且不易出現非預期結果的做法是針對錯誤做專門的處理. (請搜尋 On Error 陳述式 的說明)
Sub ChkFile()
...
On Error GoTo ErrorHandler ' 開啟錯誤處理程式。
vD.RemoveAll ' 發生錯誤需做處理的程式
On Error Goto 0 ' 關閉錯誤處理程式。
...
Exit Sub ' 離開程式,以避免進入錯誤處理程式。
ErrorHandler: ' 錯誤處理程式。
Select Case Err.Number ' 檢查錯誤代碼。
Case 424 ' 發生「此處需要物件」之錯誤。
Set vD = CreateObject("Scripting.Dictionary") ' 建立物件。(針對錯誤發生原因做補救或處理)
Case Else ' 用 Err.number 取得錯誤代碼, 用 Err.Description 取得錯誤說明文字. Err.Clear 清除錯誤旗標
MsgBox Prompt:=Err.Description, Buttons:=vbOK, Title:="發生錯誤"
Err.Clear
Exit Sub
End Select
Resume ' 將程式執行步驟回到原發生錯誤的陳述式中。
End Sub
jackyq 大大指證出你 ActiveX 的錯誤點了,原因是你輸錯字了
Set vD = CreateObject("scripting.dictonary") ,少一個 i ,
與大小寫無關,年紀大了畢竟視力退很多了,不仔細還真沒看到呢!
謝謝 jackyq 大大。作者: jackyq 時間: 2016-4-28 09:37
再另外
把 EXCEL表單處理介面 裡面的這段 加入藍字部分
Private Sub CommandButton1_Click()
If Application.FindFile = False Then
MsgBox "您沒有開啟母檔"
End If
Dim lRrow&
lRrow = 3
vD.RemoveAll
While Cells(lRow, 5) <> ""
If Not vD.Exists(CStr(Cells(lRow, 5))) Then
EXCEL表單處理介面.ListBox1.AddItem CStr(Cells(lRow, 5))
vD(CStr(Cells(lRow, 5))) = lRow
End If
lRow = lRow + 1 End With
End Sub作者: chaoyiho 時間: 2016-4-28 10:13
#51 樓 我看錯了 ( While 看成 With )
更正一下
大大真的是來考大家眼力的 ( 找不同的遊戲 ?? )
Private Sub CommandButton1_Click()
If Application.FindFile = False Then
MsgBox "您沒有開啟母檔"
End If
Dim lrow& ' c_c_lai 大大找出
lrow = 3
vD.RemoveAll
While Cells(lrow, 5) <> ""
If Not vD.Exists(CStr(Cells(lrow, 5))) Then
EXCEL表單處理介面.ListBox1.AddItem CStr(Cells(lrow, 5))
vD(CStr(Cells(lrow, 5))) = lrow
End If
lrow = lrow + 1
Wend
End Sub作者: c_c_lai 時間: 2016-4-28 10:23