Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
2007 Form Control/ActiveX Control font difference Nikko963 Excel Discussion (Misc queries) 0 April 15th 09 04:21 PM
Using a Form to control variable fields. Memphus01 Excel Programming 8 March 18th 09 05:33 AM
variable form control Roman[_4_] Excel Programming 2 June 15th 05 08:20 AM
Variable Reference to a Form Control Sharlene England Excel Programming 2 November 5th 04 10:56 PM
Tool Tip Text for Form control/ Active-X control Freddie[_2_] Excel Programming 0 October 19th 04 04:14 AM


All times are GMT +1. The time now is 01:33 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"