Sub ShowSheetListForm()
Dim myForm As Object 'Declare a variable to hold the UserForm object
Dim ws As Worksheet 'Declare a variable to hold the Worksheet object
'Create a new UserForm
Set myForm = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
'Set properties of the UserForm
With myForm
.Properties("Caption") = "Select a sheet" 'Set the caption of the UserForm
.Properties("Width") = 300 'Set the width of the UserForm
.Properties("Height") = 200 'Set the height of the UserForm
End With
'Add a ListBox control to the UserForm
With myForm.Controls.Add("Forms.ListBox.1", "myListBox", True)
.Top = 20 'Set the position of the ListBox
.Left = 20
.Width = 260 'Set the size of the ListBox
.Height = 120
.ColumnCount = 1 'Set the number of columns in the ListBox
End With
'Add a command button to the UserForm
With myForm.Controls.Add("Forms.CommandButton.1", "myButton", True)
.Caption = "OK" 'Set the caption of the command button
.Top = 150 'Set the position of the command button
.Left = 20
End With
'Populate the ListBox with sheet names
For Each ws In ThisWorkbook.Worksheets
myForm.myListBox.AddItem ws.Name
Next ws
'Add a Click event handler for the command button
With myForm.CodeModule
'Add the code to move to the selected sheet
.InsertLines .CountOfLines + 1, "Private Sub myButton_Click()"
.InsertLines .CountOfLines + 1, " If myListBox.ListIndex >= 0 Then"
.InsertLines .CountOfLines + 2, " ThisWorkbook.Worksheets(myListBox.Value).Activate"
.InsertLines .CountOfLines + 2, " Unload Me"
.InsertLines .CountOfLines + 2, " Else"
.InsertLines .CountOfLines + 2, " MsgBox ""Please select a sheet."", vbInformation, ""Selection Error"""
.InsertLines .CountOfLines + 2, " End If"
.InsertLines .CountOfLines + 2, "End Sub"
End With
'Show the UserForm
myForm.Show
End Sub
'엑셀 > vba' 카테고리의 다른 글
잔액 구하기 chatgpt (0) | 2023.04.06 |
---|---|
userform 팝업메뉴 2 chatgpt (0) | 2023.03.17 |
userform 팝업메뉴 만들기 (0) | 2023.02.24 |
두셀의 값을 바꾸기 chatgpt (0) | 2023.02.23 |
통합문서 합치기 chatgpt (0) | 2023.02.22 |