Referring to control value on form using variable name for control
Sorry, I misread that first sentence!
If you want to loop through the controls looking for textboxes, you can do
something like:
Option Explicit
'some event in the userform module??
Sub Commandbutton1_click()
Dim Ctrl As Control
Dim res As Variant 'could be an error
Dim LookUpRng As Range
Dim myStr As String
Dim myVal As Double
Set LookUpRng = Worksheets("Sheet2").Range("A:e")
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
If LCase(Left(Ctrl.Name, 6)) = LCase("brkqua") Then
'MsgBox "Found it!" 'just for testing!
myStr = Ctrl.Value
res = Application.VLookup(myStr, LookUpRng, 2, False)
If IsError(res) Then
'no match, what should be done
MsgBox "No match"
Else
If IsNumeric(res) = False Then
'arithmetic would fail, what should be done
MsgBox "Fix the table!"
Else
myVal = res * 12.34 'whatever
MsgBox Format(myVal, "$#,##0.00")
End If
End If
End If
End If
Next Ctrl
End Sub
if you have a specific textbox you want to use, you can use:
MsgBox Me.brkquaTest1.Value
or
MsgBox Me.Controls("brkquatest1").Value
If you know that you have 14 of those textboxes, you can use that second syntax
and loop through them with something like:
Dim iCtr as long
for ictr = 1 to 14
msgbox me.controls("brkquatest" & ictr).value
next ictr
On 10/01/2010 10:00, CompleteNewb wrote:
Thanks, Norie and Dave. Dave, these are user forms, not controls put into
worksheets, so none of that stuff I found about referring to controls and their
values applied, as it was all about controls inserted into worksheets, and
apparently it's a horse of a different color when dealing with actual forms.
Norie, I did figure out a workaround to address a control's value, but I was
never able to refer to controls using a variablename. For instance, if I have a
list f controlnames in a worksheet, and I set a variable named "CtlName" to one
of the values in the worksheet (let's say cell A3 as "cboChooseState", which is
the name of a combobox on the form, and I set CtlName=Range(A3).value, I cannot
then refer to CtlName.value, or Controls(CtlName).value; it sees I can't refer
tom a control if the name of the resides in a variable. That's the issue I was
having, and I've gotten around it, but never figured out a way to resolve it.
"norie" wrote in message
...
If this is a userform then take a look at the Controls collection.
Here's a simple loop:
Dim ctl As MSForms.Control
For Each ctl In Me.Controls
' do stuff with control
Next ctl
You can check the name and type of the control within the loop and
then so whatever you want to do.
--
Dave Peterson
|