Open UserForm, hide some command buttons in 1st page of multipages - vba

I have a command button in excel that will open the userform
Sub openfile ()
UserForms1.Show
End Sub
Now, how do I hide the command button on first page of the form then unhide it when the next page is selected?

Place the following into your UserForm_Initialize....
Me.CommandButton1.Visible = False. Alternatively you can set the visible value of the button to false in properties window!
Then add this to the code for your UserForm
Private Sub MultiPage1_Change()
If MultiPage1.Value = 1 Then 'page values start with 0 as the first page
Me.CommandButton1.Visible = True
Else
Me.CommandButton1.Visible = False
End If
End Sub

Related

Macro Freezes when ListBox is clicked

I'm using Office Professional Plus 2019. I have a ListBox that sometimes freezes the macro. The UserForm (AffDateSelector) has a ListBox (DateSelectionList) and a Button (Continue). The UserForm is called from the driving Sub with AffDateSelector.show. The code for the form is below:
Option Explicit
Private Sub Continue_Click()
Dim lngCurrItem As Long
'Assign the selected value to the public variable strClassDate.
For lngCurrItem = 0 To DateSelectionList.ListCount - 1
If DateSelectionList.Selected(lngCurrItem) = True Then
strClassDate = DateSelectionList.List(lngCurrItem)
End If
Next lngCurrItem
'Unload the form and return to the calling Sub.
Unload Me
End Sub
Private Sub DateSelectionList_Click()
End Sub
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_initialize()
'Load ListBoxAccounts with the public variable strDateArray
With DateSelectionList
.List = strDateArray
End With
'Default the first row in DateSelectionList to be selected.
DateSelectionList.Selected(0) = True
End Sub
I've been using F8 to step through the code. When the form is shown, the UserForm_initialize() sub is executed and the data display properly. If the user does nothing else except click the Continue button, the Continue_Click() sub gets executed, the variable strClassDate is populated, control is returned to the calling sub and all is well.
The problem comes when the user selects an item other than the one defaulted in the list. Once the user clicks on the ListForm, the sub DateSelectionList_Click() is executed, but there's nothing in that sub, so the macro freezes. If I put in "dummy code" into DateSelectionlist_Click() (e.g. strClassDate = strClassDate) that line is executed and then the macro freezes when the End Sub statement is executed.
How can I allow the user to click on a non-defaulted item in the list, keep the form displayed, and wait for the Continue button to be pressed?
The code was fine. I had a lot of windows open and the pop-up didn't appear I was running the code from the VB editor but the pop-up never appeared. When I pressed Alt + Tab it didn't appear in that list either.

how to hide tab control on opening the form?

I currently have a tab control with 4 tabs. Each tab is tied to a checkbox, in which the tab will open when the checkbox clicks.
However, I am struggling with how to hide the entire tab control or tabs upon opening the form. Ergo, I only want the tabs to start appearing when boxes are ticked. Whereas right now, the tabs all appear when the forms open.
my current code is this:
Private Sub Type_C_Click()
If Me.Type_C = vbTrue Then
Me.Section_III__Cybersecurity.Visible = True
Else
Me.Section_III__Cybersecurity.Visible = False
End If
End Sub
Private Sub Type_D_Click()
If Me.Type_D = vbTrue Then
Me.Section_IV__Data_Security.Visible = True
Else
Me.Section_IV__Data_Security.Visible = False
End If
End Sub
Private Sub Type_I_Click()
If Me.Type_I = vbTrue Then
Me.Section_V__ICT_Outage.Visible = True
Else
Me.Section_V__ICT_Outage.Visible = False
End If
End Sub
Private Sub Type_G_Click()
If Me.Type_G = vbTrue Then
Me.Section_VI__Loss_of_Government_Issue_Equipment.Visible = True
Else
Me.Section_VI__Loss_of_Government_Issue_Equipment.Visible = False
End If
End Sub
I've tried to use form current and hide the visibility of the tab control, however, subsequently none of the tabs will appear when the boxes are ticked. Similarly, I've tried to hide individual tabs in form current, however i get error 2156 where one of the tabs is the focus and I can't seem to bypass it.
Any help would be greatly appreciated!
Make sure checkbox TripleState property is set to no. Set each Tab page Visible property to no in form design.
Before setting page to not visible, set focus to some other control on form that is not located on any of the pages because page that has focus cannot be set not visible. Conversely, after setting a page visible, put focus on a control located on that page.

When I double click on a cell to open a form, a checkbox on the form behind my cursor is checked/unchecked. How can I prevent this from happening?

I have a form that appears on a double click event of a specific cell.
The form contains a list box with a bunch of checkboxes in it and in my _Activate() sub, the checkboxes are set to true or false based on values on the active sheet.
The trouble is that when the form opens up behind the cursor, the second click of the double click that opens the form is also checking/unchecking a checkbox in the form.
I've tried sticking "DoEvents" in the activate sub before the code sets the checkbox values but it hasn't made a difference - The checkbox behind my cursor where the form opens will be checked/unchecked.
I don't expect that the code will help much but it is essentially as below:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target = Range("aParticularRangeName") Then
frmSelectStuff.Show
End If
End Sub
Public Sub UserForm_Activate()
Dim iRegions As Integer
Dim sRecheck As Variant
Dim sRecheckList() As String
sRecheckList = Split(ActiveCell.Value, "; ")
For Each sRecheck In sRecheckList
For iRegions = 0 To lbRegionsTemp.ListCount - 1
If sRecheck = lbRegionsTemp.List(iRegions) Then lbRegionsTemp.Selected(iRegions) = True
Next
Next
End Sub
What about using Cancel = True?
If Target = Range("aParticularRangeName") Then
Cancel = True
frmSelectStuff.Show
End If

Pausing VBA Code

How can you pause a code from running and then allow it to continue again.
Using a ToggleButton in a Userform (which initiates your code and Modal = True)
You can successfully pause the code. With this you can then work in an additional selection to change the operation or function of the code via an addiotn button.
You will need to use public/global variables.
Comments Welcome.
Userform:
Public Sub ToggleButton1_AfterUpdate()
'Program is Paused / Selected to Pause
If ProgBar.ToggleButton1.Value = True Then
'Changing the Text of the Toggle button once the program is
'selected to Pause
'If program paused then button will display continue
ProgBar.ToggleButton1.Caption = "Continue"
'For Sending to Loop Code
ProgramStatus = "0"
Call LoopCode.PrgStat(ProgramStatus)
End If
'Program is running / Selected to run
If ProgBar.ToggleButton1.Value = False Then
'Changing the Text of the Toggle button once the program is selected to continue
'If program running then button will display pause
ProgBar.ToggleButton1.Caption = "Pause"
'For Sending to Loop Code
ProgramStatus = "1"
Call LoopCode.PrgStat(ProgramStatus)
End If
End Sub
In your Module
Public Status As String
Public Sub PrgStat(ByVal ProgStatus As String)
Status = ProgStatus
End Sub
Sub SomeSub()
Do While
' Some Loop Code Running
If Status = "1" Then
'Toggle Not Pressed
End If
If Status = "0" Then
'Toggle Button Pressed
Do While Status = "0"
'Program will stop here until the togglebutton on the
'userform is pressed again which changes Status = 1
'This is where you can make another selection on the
'userform
DoEvents
Loop
End If
Loop
End Sub

Press other button2 when button 1 is pressed EXCEL VBA

I have 3 toggle buttons as shown in Excel VBA. First I press continuous then it will be pressed and start running macro. During that if I need to press Emergency button I need to do :
step1: Switch off Continuous button
step2 : Switch on Emergency Button
( manual is irrelevent here)
Currently when I try to do this, Emergency Stop cannot be pressed until macro of continuous ends.
I use this most of the time when I trying to say, scrape data from the net.
Option Explicit
Dim boolStop As Boolean
'~~> Continuous button
Private Sub CommandButton1_Click()
CommandButton1.Enabled = False
boolStop = False
For i = 1 To 1000000000
For j = 1 To 1000000000
Debug.Print i
DoEvents
If boolStop = True Then
Msgbox "Operation Paused by the User"
CommandButton1.Enabled = True
Exit Sub
End If
Next j
Next i
End Sub
'~~> Emergency button
Private Sub CommandButton3_Click()
boolStop = True
End Sub