Can you reference the control name within a sub in vba? - vba

So I'm attempting to filter a large section of data in a spreadsheet with checkboxes. So far around 80 individual checkboxes. I'm wondering if there is anyway to refer to the checkbox (or any other control) name within the sub, as a specific reference, i.e. thiscontrol.name. At present the first checkbox reads:
Private Sub_F1_Click()
StringVariableForLaterUse ="F1"
If F1.Value = True Then
'Display Data Relevant to F1
End If
End
I'm wondering if I can use
StringVariableForLaterUse = ThisControl.Name
and
If ThisControl.Value = True Then
I'd then be able to replicate this a further 78 times. Currently pure laziness factor, however I want my Subs to be self sufficient as possible. Any thoughts folks?

You can never use a string like an object... But, for an ActiveXcheck box, use the next workaround code.
Public chk1 As Shape
Private Sub CheckBox1_Click()
Set chk1 = ActiveSheet.Shapes(CheckBox1.Name)
MsgBox chk1.OLEFormat.Object.Object.value
End Sub
You can use now chk1 in another Sub inside the sheet module.
You can also refer to it from a module Sub, referencing the sheet, too:
Sub testSheetChk()
Debug.Print Worksheets("Sheet Name").chk1.OLEFormat.Object.Object.value
End Sub
But, this will work only after you firstly run (once) the click event, in order to alocate a value to the object variable.

Related

Why is the Formula property necessary?

I have been wondering what the purpose of the Formula property is in VBA since we can get the exact same output in Excel using the same code without the the said property.
For example :
Sub myformula()
Range("f3").Formula = "=sum(b6+h9)"
End Sub
produces the same results as
Sub myformula()
Range("f3") = "=sum(b6+h9)"
End Sub
Is there a specific scenario in which using this property becomes important and just typing out the formula as a value might not work?

Excel VBA - Remember Last Userform Textbox Cursor Focus

I have 10 text boxes on an Excel VBA Userform, and when I switch away from the window (say, to do other work in a different program) and come back, I want the focus to be on the textbox I was last using.
I think this should be an obvious question to an experienced person, but I can't find a well-asked question about this topic anywhere. If anyone can direct me to a proper answer, give me a searchable topic, or a good piece of code, that would be golden. I'm a sponge, I'll take anything.
Thank you!
-Chris
Cursor focus is not an event in VBA, thus it is a bit tough to do it. BUT you can use the _Change event and remember the last changed Textbox, which is quite close to you want.
You need a public variable in a module, for the name of the last changed TextBox. Thus, the next time the Form is activated, you may use a simple select case and use .SetFocus to the corresponding name of the form. The code below works with two TextBoxes.
In the form:
Option Explicit
Private Sub TextBox1_Change()
strLastTb = "TextBox1"
End Sub
Private Sub TextBox2_Change()
strLastTb = "TextBox2"
End Sub
Private Sub UserForm_Activate()
Select Case strLastTb
Case "TextBox1"
TextBox1.SetFocus
Case "TextBox2"
TextBox2.SetFocus
End Select
End Sub
In a module:
Option Explicit
Public strLastTb As String
In general, you may create a function that concatenates TextBox&Digit, so you will not be required to write 100s lines if you have 50 TextBoxes. And it would be looking better.

Can you a call a Sub that has an argument with a button?

I want to call the following sub with a button from my excel sheet but it doesn't appear in the list when you try to assign a macro. I also call it from a bigger macro, in which case I don't want to show the "Finished" message box. Hence I have the DontShowMsgBox boolean argument.
Sub InteriorDumbCopyExport(DontShowMsgBox as Boolean)
'do stuff...
If DontShowMsgBox Then
MsgBox "Finished."
End If
End Sub
It's when I add the argument in that I can no longer see it in the list. Is there an easy way around this?
I want to call the sub directly as I have to call it from a shape so can't use an ActiveX button. I haven't found anything in my searching so far but feel there must be a solution, otherwise what is the difference between a function with an argument and a sub with an argument?
You can indeed do that. In the Assign Macros dialog, simply type the name of the macro and the argument, all enclosed in single quotes. For example:
'InteriorDumbCopyExport True'
Note however that this can cause problems if your workbook is saved as an xlsb file (I don't know why).
In this particular case though, I'd just make the argument optional and default to whichever value you want the shape to use. Then you just use the macro name in the Assign Macros dialog without adding a parameter.
Simply call your button sub with another sub:
Sub test() 'call with your button
Call InteriorDumbCopyExport(DontShowMsgBox as Boolean)
End Sub
Another way would be to know the source of your DontShowMsgBox. As a public variable, you could set it in front of executing the macro and then directly use it within having no arguments for the call itself.
In any module:
Dim DontShowMsgBox As Boolean
And if you only need it for debugging then make it a Const which will only be true if you want it to.
For a FORM control that's normal behaviour as you cannot assign a Sub depending on parameters as the button wouldn't know which parameter to hand over (for a similar reason you can't link functions to buttons), whereas an ActiveX button would create its own Sub (e.g. Private Sub CommandButton1_Click()) within the code space of the worksheet it appears.
So use a Sub without parameters for the button to call, within the sub determine the actual condition of DontShowMsgBox and call another Sub doing the job into which youdeliver this parameter.

Is it possible set my Excel Userform Combo box to begin narrowing the fields while data is entered?

VBA Newbie here.
I have searched high and low for this answer, and even come across other questions very similar to mine, but cannot get an answer. So I am hoping that this is my lucky day.
I have a Userform in excel that has Four combo boxes. Each combo box has a drop down with several choices. In two of these boxes, there are many business names and a lot of these names are similar. I was wanting to have the feature where are the data was being typed into the box, it would begin to narrow the options. EXAMPLE: if I type "heating and air" it begins to only show items in the list that include that word or phrase.
Is this a properties change in the box, or a code written, or something else?
Please help- I am stumped and no one seems to have the answer.
Very grateful-
Excel Newbie
Yes this is very possible. All you have to do is create a sub that populates that combo box, set it up so that when adding it checks the value of the box for example if it was the basic typing example. basic format. This assumes the possible values are stored in an array. This would add any item that has the string entered in it. (in any position)
For I = 0 to Number of Values
If instr(Value(I), ComboBox.Text) > 0 then
add item
endif
next
I played around a bit and came up with something to get you started. This basically functions as an "auto search". It is not autocomplete because it will search entire terms, not just terms which begin with whatever you've typed in. Basically I assume you have a range of cells (in this example cells A2:A121) that have the date for your drop down in it.
Setup
Add a new generic module (I named mine GlobalVars and add the following code:
Option Explicit
Public IgnoreChange As Boolean
Public RangeOfData As Variant
The Code
Open the code to your UserForm.
Add the following code:
Private Sub UserForm_Initialize()
RangeOfData = Application.WorksheetFunction.Transpose(Sheet1.Range("A2:A121").Value)
IgnoreChange = False
End Sub
Be sure to update A2:A121 and Sheet1 (I am using code name, but Worksheets("Sheet1") would work just as well) to point to the data which contains your combobox choices.
Now, the meat of the job is handled in the Combobox_Change event
Private Sub ComboBox1_Change()
If Me.ComboBox1.Text = vbNullString Then
Me.ComboBox1.Clear
SendKeys ("{Enter}")
End If
If Me.ComboBox1.TextLength > 2 Then
Dim i As Long, j As Long
If IgnoreChange = False Then
Me.ComboBox1.Clear
SendKeys ("{Enter}")
DoEvents 'Bug with NumLock
For i = LBound(RangeOfData) To UBound(RangeOfData)
If UCase(RangeOfData(i)) Like "*" & UCase(Me.ComboBox1.Text) & "*" Then
Me.ComboBox1.AddItem RangeOfData(i)
End If
Next i
Me.ComboBox1.DropDown
End If
End If
IgnoreChange = False
End Sub
Be sure to change ComboBox1 to the name of your combobox control.
Basically, what this does is it handles user input when it reaches two characters or longer. The code will search through your input data range and then return results that match the string as the user is entering it. The results is like a suggestions box.
I should note that with this method, the combobox is NOT pre-populated with data, so users must begin typing something into the combobox.
Additionally, I added the following code to handle the backspace key:
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 8 Then
IgnoreChange = True
End If
End Sub
Again be sure to change ComboBox1 as appropriate.
For my example, I loaded all 120 crayola crayon colors into a spreadsheet (that is what is in Sheet1 from A2:A121).
Here is example output for when I start typing, first I input blu:
As you can see I am getting all values that contain blu, including those that don't start with blu such as Cadet Blue or Midnight blue.
As another example, I will search for flower
So as you can see, instead of being a combobox with 120 static options, it is updated based on what the user types and tied to a list of values in your cells.
I did notice that SendKeys sometimes toggled my NumLock, this is a known issue. The point of that line is to collapse the drop down after the user deletes all of the text or continues texting so as to "refresh" the auto-generated list.

Provide a range selection tool/utility to the user in Excel VBA

I am trying to develop a user form in Excel 2007 VBA and want to provide a user with a range selection icon, something like this:
However, I have not been able to find any built-in form design tool or any online tool which provides this or at least gives me an idea. If anyone has any idea about this, I will greatly appreciate their help.
This control is called RefEdit control.
To use it, you have to first add it to the toolbox window by right-clicking in the toolbox window and selecting Additional Controls.... Then you select RefEdit.Ctrl and close the dialog.
Now you can select it in the toolbox and place it on your form.
Another alternative to using the RefEdit.Ctrl is to hook into some undocumented features of the TextBox control and use the Application.InputBox function.
There are two properties of the TextBox control that do not appear in the Properties dialog, that allow you to add a button on the right. They are DropButtonStyle and ShowDropButtonWhen. When the button is clicked it will fire the DropButtonClick event for the control where you can show the input box.
Start by placing a TextBox control on the form. Then add the following to the UserForm_Initialize procedure:
Private Sub UserForm_Initialize()
txtRefersTo.DropButtonStyle = frmDropButtonStyleReduce
txtRefersTo.ShowDropButtonWhen = frmShowDropButtonWhenAlways
End Sub
Then add an event handler to the DropButtonClick event as follows to capture the range using the Application.InputBox dialog:
Private Sub txtRefersTo_DropButtonClick()
Me.Hide
txtRefersTo.Text = Application.InputBox("Select the range", "Range Picker", txtRefersTo.Text, Type:=8)
Me.Show vbModal
End Sub
The main advantage to this approach is that it allows you to place a control within a frame or on a separate tab without experiencing the issues associated with the RefEdit.Ctrl. The disadvantage is that it requires a separate dialog to interact with Excel.
Although this question is already almost a decade old, it still came up as my first Google search result so I'm going to post an answer as another approach to consider. The InputBox with type set to cell reference might be sufficient for many people's needs. The InputBox type does the drudge work of validating the user's response. See this article for how to use the InputBox types: https://www.thespreadsheetguru.com/blog/vba-to-select-range-with-inputbox
I liked #krey answer which was basically a very simplified version of the link shared in #stifin answer --> https://www.thespreadsheetguru.com/blog/vba-to-select-range-with-inputbox
But both used "odd" names for the buttons and I think guides should use default names/values for easy understand and manipulation by end user. Additionally, I added a CommandButton to "run" the userform and a "select" statement for initial testing. I also wanted to provide an initial value as I try to pre-populate my userforms, but allow end user to override if needed.
Here is the final code snipppets utilizing a UserForm w/ one TextBox named TextBox1 (default) and one CommandButton named CommandButton1 (also default).
Option Explicit
Public rng As Range
Public Sub UserForm_Initialize()
Me.TextBox1.DropButtonStyle = fmDropButtonStyleReduce
Me.TextBox1.ShowDropButtonWhen = fmShowDropButtonWhenAlways
Set rng = Range("A1:B4")
TextBox1.Value = rng.Address(False, False)
End Sub
Public Sub CommandButton1_Click()
rng.Select
Unload Me
End Sub
Public Sub TextBox1_DropButtonClick()
Me.Hide
Set rng = Application.InputBox("Select the range", "Range Picker", TextBox1.Text, Type:=8)
TextBox1.Value = rng.Address(False, False)
Me.Show
End Sub