Я почти новичок в Excel VBA.Я пытаюсь отобразить значения некоторых ячеек из рабочего листа в UserForm2 и выгрузить форму, когда пользователь нажимает CommandButton1.Чтобы избежать создания класса для моих элементов управления, я начинаю с черновика пользовательской формы, в котором все необходимые мне компоненты размещены на нем.Я устанавливаю позиции текстовых полей, заполняю их, устанавливаю позицию CommandButton1.Все это хорошо, все показывает, как ожидалось / запланировано.когда я нажимаю CommandButton1 в форме, я слышу «Дин», и сообщение «нажал CB1», которое я ожидаю увидеть, не отображается.Что я делаю неправильно или чего не хватает?
Option Explicit
Public leftOfForm As Long
Public topOfForm As Long
Public widthOfForm As Long
Public heightOfForm As Long
' ***********************************
Dim numInfoMessages As Long
Dim heightBox As Long
Dim lengthTextbox As Long
Dim lengthCommandButton As Long
Dim numberOfScreenRows As Long
Dim horizantalPadding As Long
Dim verticalPadding As Long
Dim indexLoop As Integer
Dim contr As Control
Dim theFontSize As Integer
Public Sub CommandButton1_Click()
'
' Just to confirm it is ok --- current userform will be unloaded
'
MsgBox "clicked CB1"
End Sub
Public Sub UserForm_initialize()
'
' Save forms current info - not needed in fact
'
theFontSize = 12
leftOfForm = UserForm2.Left
topOfForm = UserForm2.top
widthOfForm = UserForm2.Width
heightOfForm = UserForm2.Height
numInfoMessages = 6
'
' Calculate userform size based on number of lines to be displayed
'
heightBox = 25
lengthTextbox = 500
lengthCommandButton = 90
verticalPadding = 20
horizantalPadding = 15
numberOfScreenRows = (numInfoMessages + 1) * 2 + 1
'
' Resize form according to the results
'
UserForm2.Width = horizantalPadding * 3 + lengthTextbox + 10
UserForm2.Height = verticalPadding * (numberOfScreenRows + 2)
UserForm2.top = 0
UserForm2.Left = 0
'
' Fill in and allocate the text boxes
'
indexLoop = 1
For Each contr In UserForm2.Controls
If TypeName(contr) = "TextBox" Then
contr.top = (indexLoop) * 2 * verticalPadding
contr.Left = horizantalPadding
contr.Width = lengthTextbox
contr.Height = heightBox
contr.BorderStyle = 1
contr.Text = ThisWorkbook.Worksheets(ThisWorkbook.messagesPageName).Range("B" & indexLoop + 1).Value
contr.WordWrap = False
contr.MultiLine = False
contr.Font.Size = theFontSize
indexLoop = 1 + indexLoop
End If
Next
'
' Allocate the command button
'
For Each contr In UserForm2.Controls
If TypeName(contr) = "CommandButton" Then
contr.top = (indexLoop) * 2 * verticalPadding
contr.Width = lengthCommandButton
contr.Height = heightBox
contr.Width = lengthCommandButton
contr.Left = UserForm2.Width / 2 - contr.Width / 2
contr.top = (indexLoop) * 2 * verticalPadding
contr.Font.Size = theFontSize
End If
Next
End Sub