Excel VBA Userform read current values when opening - excel

I am trying to make an user form for hiding/showing different columns of the worksheet.
So i've made a button which opens the userform (called 'hider') with the following code:
Private Sub CommandButton1_Click()
Hider.Show
End Sub
The userform than currently contains two checkboxes which hide the selected columns using:
Private Sub Week3_Click()
Range("N:Q").Columns.Hidden = Not Week3
End Sub
So if the checkbox is 'checked' the columns are shown and if 'unchecked' the columns are hidden, this part works, except every time the userform opens it will reset the checkboxes to their native state of 'unchecked' while the columns remain hidden (which is ok).
So my question is:
How can I sync the checkboxes in the userform to the currently active value of the columns? I was thinking about making a sync button on the userform or an action to read all the current values when opening the userform, but i couldn't get that to work.

If I understand your question correctly ("...or an action to read all the current values when opening the userform") just use the following code within the Userform code module to synchronize your checkbox when opening:
Private Sub UserForm_Initialize()
'Me.Week3.Value = IIf(ActiveSheet.Range("N:Q").Columns.Hidden = True, False, True)
Me.Week3.Value = Not ActiveSheet.Range("N:Q").Columns.Hidden ' simplified due to comment thx Mathieu Guindon
' ...
End Sub

Related

Commandbutton page selection

So I have a commandbutton on a excel sheet and when I click on it brings up the userform But the page isn't starting from "Page1" rather its the page I was last using in the userform developer window. Therefore is there a way once clicked into the commandbutton the userform load up the first page everytime?
At the moment the commandbutton contains this (Userform is named as "WizardProp":
Private Sub CommandButton1_Click()
WizardProp.Show
End Sub
and the Openform contains this:
Sub openform()
WizardProp.Show
WizardProp.tbClient.SetFocus
End Sub
The visible page number of a MultiPage object is defined by its Value (starting with 0).
Is "tbClient" the MultiPage object on your userform? Then place this code in your userform's initialization:
Private Sub WizardProp_Initialize()
WizardProp.tbClient.Value = 0
End Sub
Would Unload Me solve your problem? insert on the close of the userform. The Only issue is that it would rest the userform, data and all.

Excel vba - Userform - doesn't change output - focus worksheet

Trying to use excel vba userforms to automate many tasks in a spreadsheet. There is a Button1 on Sheet1 that has two commands.
sub Button1_click()
sheet1.range("a3").select
userform1.show
end sub
As an example there is a data entry worksheet (sheet2) that we want to switch to to input data values to a list.
To simplify in this example and to show my issue the userform has one button
sub CommandButton1_click()
userform1.hide ' hide the form
sheet2.activate
sheet2.range("b2").select
end sub
What I want to be able to do is use the user form button to switch to sheet2, select b2, and be able to enter data starting there immediately.
What I've been getting is a selection box on sheet2.range("b2") BUT I show color starting at sheet1!a3, then sheet1!b4,... I have entry occurring on sheet1!b2 etc.
Shows beginning and entry
Entry colors from sheet1 showing on visible sheet2 - data not appearing
Actual data is entered on sheet1 not sheet2 -
The problem seems to be (as you mention in the comments) that, at the end of Button1_Click, the focus returns to the sheet containing the button even though the ActiveSheet is now a different sheet.
For the moment (until someone comes up with a better solution) a "workaround" is to allow the Button1_Click event to finish running before showing the Form. That can be achieved by changing Button1_Click to something like:
Sub Button1_click()
Sheet1.Range("a3").Select
Application.OnTime Now(), "BypassBug"
End Sub
Sub BypassBug()
UserForm1.Show
End Sub

Excel ActiveX Combobox shows previous selection when losing focus

I have this code which fills a combobox on Sheet1 with the Name column of Table1 on Sheet2.
Public Sub Worksheet_Activate()
Me.ComboBox1.List = Worksheets("Sheet2").ListObjects("Table1")_
.ListColumns("Name").DataBodyRange.Value
End Sub
Works fine but it has a weird effect when I click off the combobox onto the sheet. The selected entry in the box quickly flashes to the previous entry. For example, the currently selected item is "b" and then I select "c". If I click on the worksheet the entry in the box quickly flashes to "b" before going back to "c".
I've put this code alone in a new file and I still get the same effect. Has anyone else seen this?
Edit regarding reason for Public Sub:
Forgot to include the Workbook_Open code so that Sheet1 is considered Activated when you open the Workbook. But it doesn't matter if I keep that code or not, I still see the effect.
Private Sub Workbook_Open()
Call ActiveSheet.Worksheet_Activate
End Sub
Adding a LostFocus event with code that selects a cell on your worksheet should cause the flicker not to happen when you select a cell after changing the ComboBox's value.
Like the following:
Private Sub ComboBox1_LostFocus()
ActiveSheet.Range("A1").select
End Sub

Excel Userform Combobox Properties Rowsource box issues?

I have a userform in Excel that works as a calculator.
In this userform I have two ComboBoxs (1 & 2)
In VBA editor, with ComboBox1 selected, In Properties, under Rowsourse I have: Sheet1!a4:a5
In Sheet1, A4 = Auckland and A5 = Christchurch
This is fine and when I run the userform there is a drop down arrow with the two options (Auckland or Christchurch).
However my problem is that when you open this workbook I have a VBA command to hide it from the users sight, leaving them only the userform to work with which is what is desired.
The issue is that if you have another workbook open then open this calculator workbook (which automatically hides itself). Then the combobox list is populated by Sheet1!a4:a5 on the other workbook that was already open, not the workbook that actually contains "Auckland" & "Christchurch" from which the userform is from.
I have tried making the Rowsource for the comboboxes more specific by putting the following in the rowsource box in properties: [book1.xlsm]sheet1!a4:a5 but this comes up with a "Invalid Property Value" error message.
I have also tried making a:
Private Sub Userform1_Initialize()
ComboBox1.Additem "Auckland"
ComboBox1.Additem "Christchurch"
End Sub
And also tried this:
Private Sub Userform1_Initialize()
ComboBox1.RowSource = Workbooks("book1.xlsm").Sheets("Sheet1").Range("a4:a5").Value
End Sub
However with both codes when it opens and runs now the comboboxes are empty and there is no list.
I think the easist solution would be to somehow put the full path (including workbook name) into the rowsource box under properties. But I must be missing something as its coming up with that error for me?
All help would be greatly appreciated.
Thanks
You are missing ' in your full path row source.
It should be like this:
Me.ComboBox1.RowSource = "'[book1.xlsm]Sheet1'!$A$4:$A$5"
I have similar question that can be found HERE.
Set the row source property of the combobox as: SheetName!$Col$Row:$Col$Row, e.g.: Location!$A$1:$A$3.
You may try adding this code on userform:
Private Sub UserForm_Initialize()
ComboBox1.list = Array("Auckland","Christchurch")
End Sub
Then set Combobox propert "MatchEntry" to "1".

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.

Resources