VBA: Show listbox on click - vba

I searched for a way to show a listbox on certain event, here a click, but wasn't able to find it
If MsgBox("Souhaitez vous reprendre un bordereau déjà édité?", vbYesNo, "Edition Bordereau") = vbYes Then
PreCheckPlot
Else
rest of commands
And the sub where I want to show the listbox
Sub PreCheckPlot()
ListBox2.Visible = True
End Sub
This doesn't work, and ListBox2.Show doesn't either, it throws an error.
Is it possible to show a listbox on a click, and if yes, how would I write it?
Thank you in advance.

You need to refer to the Sheet as well.
So if your ListBox2 is in Sheet1 then you need to use:
Sheet1.ListBox2.Visible = True

Does it go into PreCheckPlot when you step through?
What is the error?
If you create a userform, put a listbox on it and a button which when pressed shows your message your code then works fine.
I wonder if you are trying to set ListBox2.Visible from outside the form (where it will not know what the form is)
This assumes it is an ActiveX listbox - or is it a forms listbox?

Related

Get value from command button VBA

I have created A userform with few command buttons.
I can't seem to figure out how do I get the information from them.
I want to open this userform from another one and then I want the user to choose an option from one of the buttons which will change a specific cell in the table itself
the userform I created
I did not write anything on this userform therefor there is no code from it to show only the design.
how do get the information from the buttons to be written in A specific cell on a specific worksheet?
double click on one of the buttons, in the code menu a new sub should appear. this looks something like:
Sub CommandButton1_onClick()
End sub
Alongside this event method, it also has a few properties. The one that is usefull here is the CommandButton1.Value, this represents the state of the button (either true or false iirc).
So build for each button (I strongly advice giving them custom names to prevent getting lost in the trouble) as sub like this:
Sub CommandButton1_onClick()
if CommandButton1.Value then
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = "Some value"
else
ThisWorkBook.WorkSheets("WorksheetName").Range("YourRange").Value = ""
end if
End sub

Creation of Excel Form using VBA and save it

I am trying to create a excel form with text and combo boxes using VBA where the data I enter in the form should get saved in a different worksheet. There should be a save button on the form for users to click on it so that the data gets saved. Is that possible. please help
It's certainly possible, can you be more specific about what part you're having a problem with?
The general timeline of events would be this:
Create UserForm and add textboxes, comboboxes and save button.
a. To learn how to populate comboboxes, check out these resources: How to add items to a combobox in a form in excel VBA?
Give each of your textboxes, comboboxes and save button a name in properties, so that they can be referenced in the code.
Add code to the Save Button event. To do this, go to your UserForm and double click on the Save Button, this will bring you to the code that will execute when that button is clicked, you'll be adding something like this:
Sheets("Example").Range("A1").Value = textbox.Value
To answer your comment:
You shouldn't have any problem adding arrays to Comboboxes like this:
Private Sub UserForm_Initialize()
ComboBox1.List = Array("One", "Two", "Three")
ComboBox2.List = Array("Four", "Five", "Six")
ComboBox3.List = Array("Seven", "Eight", "Nine")
End Sub
If you're having issues, make sure your code, in this case "ComboBox1... etc." is referring to Comboboxes that exist.

Ellipsis Textbox for VBA Userform File Select

I am trying to create a path selection user interface for an extensive VBA program I've been working on, but I can't seem to get the ellipsis textbox that I'd like. This is a very common feature, especially in option tables. This is an example of what I'd like to get, straight from the VBA Options panel:
I would LOVE to find a way to get the same functionality in a Userform. The only solution that I've found thus far is to use a combo box with the ellipsis arrow option enabled. However, there doesn't seem to be an apparent way to use the activation of the combo box arrow to run a dialog box, nor does there seem to be a way to make it look UNLIKE a combo box. Last resort I use a button below the text box, but I'd really prefer a less-bulky way of doing this.
Any solution would be greatly appreciated.
The only solution that I've found thus far is to use a combo box with
the ellipsis arrow option enabled. However, there doesn't seem to be
an apparent way to use the activation of the combo box arrow to run a
dialog box, nor does there seem to be a way to make it look UNLIKE a
combo box
Your suggestion does work, and it is surely less complex and more elegant than having two controls work together, Button + Textbox.
A Combo can achieve perfectly the desired feature, in the following way.
1) In design mode, set the button style to Ellipsis
DropButtonStyle = fmDropButtonStyleEllipsis
And eventually, make the ellipsis show up only when the combo is entered, by setting the design-time property:
ShowDropButtonWhen = ShowDropButtonWhenFocus
2) If needed, you can set other design-time properties to have some look and feel. The defaults look pretty good however.
3) Add the following handler to the parent userform. The snippet simulates the launching of a dialog and getting a new value or cancelling. It does not show any dropdown window. (but you still have control over that: if you want to show it according to some condition, you still can call the method ComboBox1.DropDown)
Private Sub ComboBox1_DropButtonClick()
' The following two lines avoid to call the routine twice, at entry and at exit
Static i As Integer
i = (i + 1) Mod 2: If i = 0 Then Exit Sub
With ComboBox1
s = InputBox("enter some text", , .Value) '<~~ simulates any dialog
If s <> "" Then .Value = s
SendKeys ("{Enter}") '<~~ to close immediately the dropdown window
End With
End Sub
Try it ;)
Not only do ComboBoxes have Drop Buttons, so do TextBoxes (as do Excel's RefEdit controls). Even though you can't access the Textbox's Drop Button at design time, you can do so at runtime. Using a textbox avoids having to deal with the dropped down list of a combobox.
Given a textbox named TextBox1, the following code will provide the desired ellipsis drop button:
Private Sub UserForm_Initialize()
With Me.TextBox1
.DropButtonStyle = fmDropButtonStyleEllipsis
.ShowDropButtonWhen = fmShowDropButtonWhenAlways
End With
End Sub
Then use the DropButtonClick event of the textbox to perform whatever action you want:
Private Sub TextBox1_DropButtonClick()
'' Code here to do what you need
End Sub
I have an extensive example at Alternative to Excel’s Flaky RefEdit Control that uses a textbox with a "Reduce" drop button to replicate the functionality of Excel's unreliable RefEdit controls.

How to get selected text in VBA

I have a macro that changes the selected text, and I have it assigned to a button.
It works perfectly when i run it directly from visual basic, but when I click the button, the button gets the focus and my text is no longer selected so the macro change the selected element to (button).
How can I select the text and run the macro by clicking on the button and still have the text selected?
The way to do this is to set the set the TakeFocusOnClick property of the CommandButton to False. Here are is the code I use.
Private Sub CommandButton1_Click()
Dim Sel As Selection
Set Sel = Application.Selection
If Sel.Type <> wdSelectionIP Then
MsgBox Sel.Text
End If
End Sub
Is the button embedded in the document? You may need to put it on a form that loads on top of the Word window or in a menu/toolbar, so that clicking it does not affect the selection in the document itself.
Edit:
I think you can use Application.Selection.Previous to get at what you need. You could use this to restore the selection after the click event, or to act upon that section of the document, or both.
I assume that this is available in previous versions of Word, but have only confirmed its presence in 2007.
You need to change TakeFocusOnClick to "False" in the Button Preferences.

How to determine next tabstop in VBA UserForm?

I have a UserForm with some textbox entry fields on it that are enabled/disabled by a checkbox. When a checkbox is clicked to check it, I'd like to move the focus into the now-enabled textbox.
The textbox is the next control after the checkbox in the tab order, so it seems like using the tab order to find the appropriate textbox would be a good idea.
But... how can I find the next control in the tab order after a given control? Is there a method to do that, or do I have to enumerate all the controls and figure it out for myself?
I appreciate that this comes under the heading of "enumerating all the controls" but it's pretty simple and I attach the code for completeness:
Private Sub CheckBox1_Click()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.TabIndex = Me.ActiveControl.TabIndex + 1 Then
ctl.SetFocus
Exit For
End If
Next
End Sub
As a different way of looking at this.
Can you not rather use the textbox you want focussed, and set that name in the checkbox.tag
then in you vba code use
DoCmd.GoToControl CheckBox.Tag
Where the CheckBox.Tag is the Textbox.Name?
EDIT:
OK, I found this
SendKeys "{Enter}", True
at VBA code for moving to next control? It must be eeeasy
I had trouble with
SendKeys "{Enter}", True
With a little experimentation I found this works
SendKeys "{TAB}", True
One caveat...if you're in the VBE stepping through the code, and watching it on the form, SendKeys is executed is executed in the code. Confused the heck out of me at first why my code started to look odd, e.g. extra spacing and extra lines!
With all due respect to the kind advice offered thus far, this issue shouldn't be addressed through programming when there's a way to address it within the userform itself.
Click on the userform itself, then right click and select "tab order". You can then move each element within the userform to whatever position you want without having to resort to complicated and unstable programming tricks.