How to access a VBA Userform Button control code programmatically - excel

I want whenever I click on my ActiveX command button on Sheet1 it open VBA Editor and jump directly to the code I wrote for a button on my userform. I know the way to jump directly to code for a module but I couldn't find a property for userform button control (something like ThisWorkbook.VBProject.VBComponents("UserForm1").Controls("MyButton"))
Following is my code for jumping right into my Madule1 code:
Private Sub CommandButton1_Click()
Dim Wb As Workbook: Set Wb = ThisWorkbook
'---Open Module1
Application.VBE.MainWindow.Visible = True
Wb.VBProject.VBComponents("UserForm1").Activate
End Sub
I thaught these pictures would help to better understand my goal:
1-I have an ActiveX command button on sheet1
2-I have userform with a commandbutton on it
3-This commandbuttun has it's own code which I want to jump to it's code

If you want to jump to that specific sub directly, try this:
Private Sub CommandButton1_Click()
Application.VBE.ActiveVBProject.VBComponents("UserForm1").CodeModule.CodePane.Show
With Application.VBE.ActiveCodePane.CodeModule
lStartLine = .ProcStartLine("CommandButton1_Click", 0) '"CommandButton1_Click" being the name of the sub you want.
.CodePane.SetSelection lStartLine, 1, lStartLine, 1
End With
End Sub
First line should show you the code panel for the userform, and the second part selects the row down by the sub you are looking for by name.

Related

VBA Command button to open Userform returns 1004 error

In a excel project, I have a sheet with data, a userform that draws data from that sheet, and a sheet to paste userform data.
I created a button in the data sheet that should open the user form. For reference the name of my userform is UserForm1. I get a 424 error Object requiredwhen I click on the command button. I would appreciate any help or clarification, seems I am getting my classes/instances mixed up?
Code below:
Private Sub blocksSorter_Click()
Load UserForm
UserForm.Show
End Sub
The form itself is within the same VBA project and is the only form in the project. My UserForm1 code is looks generally like this:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
code
End Sub
Private Sub UserForm_Initialize()
code
End Sub
The form will only show if I do this, and change my UserCode subs to UserForm1 from UserForm.
Private Sub blocksSorter_Click()
Load UserForm1
UserForm1.Show
End Sub
Even though my form name is UserForm1, I had the caption as something else. With the help of braX, I have the following:
Private Sub blocksSorter_Click()
Load UserForm1
UserForm1.Show
End Sub
My form properties are now as follows:
Name: UserForm1
Caption: UserForm1
My UserForm code remains the same.
Start a new VBA project.
Add a form.
Name the form frmOne
Double click on the form and add MsgBox "Hello" to that event.
Add another form.
Name that form frmTwo
Double click on the 2nd form and add frmOne.Show to that event.
Run the second form, and click on it, it should open frmOne. Then click on the frmOne and the msgbox should come up.
Notice that each form has a subroutine called UserForm_Click() but they are different. Does that help clear it up? I'm not sure how else to explain it to you.

VBA does not active

I suggest that my workbook contains a VBA code below. But It does not run when I opened my workbook too.
Sub Appl_StatusBar()
Application.StatusBar = "SupportABC"
End Sub
If you put your code in the Workbook Open Event it will do what you need. To do this click the top dropdown where it says "(General)" and hit "Workbook". In the right dropdown select "Open" and save your code there". See below
Private Sub Workbook_Open()
Application.StatusBar = "SupportABC"
End Sub

VBA Userform not populated when opened first time, but works the second time

I'm working on Excel and making use of a userform. The idea is to highlight a row, then click a button which opens my useform. Column 10 of the active row consists of comments. I have coded the button so that when it is clicked, the textbox is automatically populated with the contents from column 10. The user can then add to these current comments and then press submit to make the changes.
The problem is that when the button is pressed the first time, the useform will not come populated with the comments. If I exit and do the procuedre again, then it works. What have I done wrong? The code for the button is below (textbox is called 'comment'):
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Set ws1 = Worksheets("Now")
UserForm1.Show
'Add current comments to box
UserForm1.Comment.Value = ws1.Cells(ActiveCell.row, 10).Value
End Sub
Try rearranging your code to be like this:
Private Sub CommandButton1_Click()
Dim ws1 As Worksheet
Set ws1 = Worksheets("Now")
'Add current comments to box
UserForm1.comment.Value = ws1.Cells(ActiveCell.Row, 10).Value
UserForm1.Show
End Sub
This should load the data into the text box before it is shown to you.

Start userform multipage into a specific page

I have a Userform named SheetBox
SheetBox contains a 3-page multipage window
"page1" for selecting sheets to import "page2" contains a pseudo
progress bar "page3" for selecting sheets to protect
What I need now is a method to open a specific page upon opening the Userform when a button on a worksheet is clicked
ex:
ImportBttn opens page1 of userform
ProtctBttn opens page3 of userform
I'm doing this to reduce the number of userform I needed to create, instead of creating 3 separate userforms. This also helps reduce the filesize.
This works too
Sub ImportBttn_Click()
Load SheetBox: SheetBox.MultiPage1.Value = 0: SheetBox.Show
End Sub
Sub ProtctBttn_Click()
Load SheetBox: SheetBox.MultiPage1.Value = 2: SheetBox.Show
End Sub
this loads sheetbox first, changes the multipage page and shows it afterwards
but thanks for the caller method, will be useful when I need to know what button gets pressed
In the UserForms Initialise Event, use Application.Caller to detect which button on the worksheet was pressed, and then set the multipage
Private Sub UserForm_Initialize()
Select Case Application.Caller
Case "ImportBttn"
`1st tab
Me.MultiPage1.Value = 0
Case "ProtctBttn"
`3rd tab
Me.MultiPage1.Value = 2
End Select
End Sub
My project using several multiPages. Since the user-defined names for the tabs are dynamic, I set the default Tab with ...
Sub iconDailyEntry_Click()
Call init_GUI(frmGUI.mulGUI, "pgDaily")
End Sub
Sub init_GUI(mPage As MSForms.MultiPage, pgName As String)
mPage.Value = mPage(pgName).Index
mPage.Parent.Show
End Function
Rinse & repeat for each button's click event. Cheers.

Excel VBA 2010 - Command buttons stop working with multiple sheets selected

My problem are command buttons that fail to respond when I've selected multiple sheets in a workbook. The workbook I'm testing here is meant only to study and troubleshoot this problem, which I originally found in a much more complicated workbook.
My workbook has a total of 5 worksheets. There are four ActiveX command buttons on sheet1. These four buttons launches code to either select several worksheets together or just sheet1.
After selecting multiple sheet together, only the last button clicked actually can be clicked again, the rest of the buttons on the sheet1 don't respond anymore, like they're disabled or something. Once I manually deselect the sheets so that just sheet1 is selected, the controls start working normally.
Weird.. I think it must be some sort of bug in Excel. I also can replicate the problem if I manually select multiple worksheets rather than let the code do it.
Some of my findings so far...
It doesn't seem to matter how many sheets I select so long as its two or more.
VBA code selection or manual SHIFT-CLICK doesn't matter.
The last button activate still runs once the other buttons gets locked up.
I only get this with Excel 2010, Excel 2007 didn't have this problem.
I've replicated the problem in a isolated workbook, so I don't think this is corruption issue.
The 4 command buttons execute the functions shown below. Each button marks adjacent cells if the code runs. I put a 1 second delay to verify clicking a button twice in a row was working.
CMD 1: Select sheet1 only
CMD 2: Select sheet1 only
CMD 3: Select sheet1 and sheet2
CMD 4: Select sheet1 through sheet4 via sub routine in module1
Here is my code attached to sheet1....
Option Explicit
Private Sub CommandButton1_Click()
Call MarkCmdsAsInactive
Me.Select
Call WaitSeconds(1)
Range("E6").Value = "CMD 1 Works"
End Sub
Private Sub CommandButton2_Click()
Call MarkCmdsAsInactive
Me.Select
Call WaitSeconds(1)
Range("E10").Value = "CMD 2 Works"
End Sub
Private Sub CommandButton3_Click()
Call MarkCmdsAsInactive
Sheets(Array("Sheet1", "Sheet2")).Select
Call WaitSeconds(1)
Range("E14").Value = "CMD 3 Works"
End Sub
Private Sub CommandButton4_Click()
Call MarkCmdsAsInactive
Call SelectSomeSheets
Call WaitSeconds(1)
Range("E18").Value = "CMD 4 Works"
End Sub
Private Sub MarkCmdsAsInactive()
Range("E6").Value = "Inactive"
Range("E10").Value = "Inactive"
Range("E14").Value = "Inactive"
Range("E18").Value = "Inactive"
End Sub
Private Sub WaitSeconds(waitInSeconds As Variant)
Dim newHour As Variant
Dim newMinute As Variant
Dim newSecond As Variant
Dim waitTime As Variant
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + waitInSeconds
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
End Sub
In module1 I have...
Option Explicit
Sub SelectSomeSheets()
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
End Sub
Update 2012-10-09
Here is a simple way to replicate this bug in Excel 2010...
Place 4 command buttons on sheet1.
Copy the code below to sheet1.
Put breakpoints on each of the "End Sub" statements.
Try clicking the buttons with just sheet1 selected. All buttons launch routines.
SHIFT-CLICK to select a group of sheets.
Try buttons again with the sheet group selected. Only the last used button works.
Private Sub CommandButton1_Click()
End Sub
Private Sub CommandButton2_Click()
End Sub
Private Sub CommandButton3_Click()
End Sub
Private Sub CommandButton4_Click()
End Sub
I guess it has to do with the "scope" of the ActiveX buttons (i.e. scope is sheet1 and not sheet1+sheet2+...). It seems that the selection of multiple sheets deactivates command buttons in sheet 1, even if sheet 1 remains the "active" sheet. because the ActiveX components are private to sheet1.
As a workaround, I moved your Sub CommandButtonX_Click to Module 1, removed the Private keyword, created a custom ribbon MyTools with the 4 Sub's as selectable elements. This way I moved the visibility of the sub's from sheet level to application level and all works.
Of course I had also to change Me.Select into Sheets("Sheet1").Select (allthough I hate hard codings like this ....)
Hope that helps ...

Resources