- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
7#
發表於 2011-11-16 17:50
| 只看該作者
本帖最後由 GBKEE 於 2011-11-16 17:55 編輯
回復 6# dafa
VBA 中加入物件類別模組 如圖
UserForm的程式碼- Option Explicit
- Dim ButtonClass() As New TheButton
- 'ButtonClass變數設定為 New TheButton物件類別模組
- Private Sub UserForm_Initialize()
- Dim E As Control, i As Integer
- For Each E In Me.Controls
- If InStr(E.Name, "CommandButton") Then
- i = i + 1
- ReDim Preserve ButtonClass(1 To i)
- Set ButtonClass(i).Button = E '指定 TheButton.Button(i) 的物件
- End If
- Next
- End Sub
複製代碼 物件類別模組的程式碼- Option Explicit
- Public WithEvents Button As MSForms.CommandButton
- Private Sub Button_Click()
- Dim A As Integer
- 'UserForm 中 CommandButton.Name 名稱 '依序為CommandButton1,CommandButton2...
- A = Replace(Button.Name, "CommandButton", "")
- '''''''''''或者如此也行''''''''''''''''''''''''''''''''''''''''''
- 'UserForm 中 CommandButton.Caption 在物件上的修飾文字 '如依序為 按鈕1,按鈕2...
- 'A = Replace(Button.Caption, "按鈕", "")
- ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
- Sheets(1).Cells(A, "A") = Sheets(1).Cells(A, "A") + 1
- End Sub
複製代碼 |
|