返回列表 上一主題 發帖

[發問] 請問一個UserForm與TextBox的問題

回復 1# dafa
  1. Private Sub UserForm_Initialize()
  2.     Dim E As Control
  3.     For Each E In Me.Controls
  4.         If InStr(E.Name, "TextBox") Then E.Locked = True
  5.     Next
  6. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2011-11-16 17:55 編輯

回復 6# dafa
VBA 中加入物件類別模組 如圖






UserForm的程式碼
  1. Option Explicit
  2. Dim ButtonClass() As New TheButton
  3. 'ButtonClass變數設定為 New TheButton物件類別模組
  4. Private Sub UserForm_Initialize()
  5.     Dim E As Control, i As Integer
  6.     For Each E In Me.Controls
  7.         If InStr(E.Name, "CommandButton") Then
  8.             i = i + 1
  9.             ReDim Preserve ButtonClass(1 To i)
  10.             Set ButtonClass(i).Button = E     '指定 TheButton.Button(i) 的物件
  11.         End If
  12.     Next
  13. End Sub
複製代碼
物件類別模組的程式碼
  1. Option Explicit
  2. Public WithEvents Button As MSForms.CommandButton
  3. Private Sub Button_Click()
  4.     Dim A As Integer
  5.     'UserForm 中  CommandButton.Name                名稱      '依序為CommandButton1,CommandButton2...
  6.     A = Replace(Button.Name, "CommandButton", "")
  7.     '''''''''''或者如此也行''''''''''''''''''''''''''''''''''''''''''
  8.     'UserForm 中  CommandButton.Caption 在物件上的修飾文字  '如依序為 按鈕1,按鈕2...
  9.     'A = Replace(Button.Caption, "按鈕", "")
  10.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  11.     Sheets(1).Cells(A, "A") = Sheets(1).Cells(A, "A") + 1

  12. End Sub
複製代碼

TOP

回復 9# dafa
ReDim Preserve 陣列( 1 To i )    重新配置動態陣列變數 的維數 仍然保有原來的資料的關鍵字

ReDim 陳述式:  在程序層次中用來重新配置動態陣列變數的儲存空間
Preserve 選擇性引數。當改變原有陣列最後一維的大小時,仍然保有原來的資料的關鍵字。

TOP

        靜思自在 : 能幹不幹,不如苦幹實幹。
返回列表 上一主題