Get the value of a Boolean variable by passing its name as text

pmich

Active Member
Joined
Jun 25, 2013
Messages
294
I would like to Get the value of a Boolean variable by passing its name as text.
The following line works for me in another file
Code:
lablnam = Me.Controls(StrArrOptBtnLblNam(ctra, 1)).Caption
Is there an equivalent as the above or something else to get value of variables using the names of the variables as strings?
This is my code:
Code:
Private Sub UserForm_Click()
 'get the value of a boolean variable by passing its name as text
 
 Dim Address(4) As String
 Dim ChosenName As Boolean
 Dim ChosenAddress As Boolean
 Dim ChosenState As Boolean
 Dim ChosenPinCode As Boolean
 Dim ChosenFieldList As String
 Dim i As Integer
 Dim SelFldNam() As String
 Address(0) = "ChosenName"
 Address(1) = "ChosenAddress"
 Address(2) = "ChosenState"
 Address(3) = "ChosenPinCode"
 'Now, the User choses the option button for the ChosenName in the Userform.
 'So, the result is...
 ChosenName = True
 ChosenFieldList = "ChosenName, ChosenAddress, ChosenState, ChosenPinCode"
 SelFldNam = Split(ChosenFieldList, ",") 'Text Array populated
 For i = LBound(SelFldNam) To UBound(SelFldNam)
  msgbox "SelFldNam=" & i & ". " & SelFldNam(i)
  'Address(0) = "ChosenName"
  'SelFldNam(i) = "ChosenName"
  If SelFldNam(i) = True Then 'checking whether ChosenName is true
   msgbox "Address(" & i & ")=" & SelFldNam(i) & "= true"
   'some actions as this is true
   
   'Please note:
   'The following line works for me in another file
   'lablnam = Me.Controls(StrArrOptBtnLblNam(ctra, 1)).Caption
   'Is there an equivalent as the above to call variables using strings?
   
  Else
   msgbox "Address(" & i & ")=" & SelFldNam(i) & "= false"
   'no actions as this is false
  End If
 Next i
End Sub
Any suggestions appreciated.
Also, please let me know why the MsgBox is in lower in my code.
Sorry, for the trouble. Thanks in advance.
 
Last edited:

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I would suggest that you use an array of booleans populated in the same order as your ChosenFieldList string. You can then simply use the i variable as an index into that array in your loop.

Regarding msgbox, I suspect you have at some point used that as a function/variable name in lower case.
 
Upvote 0
Mr. RoryA
Thanks for your immediate response.
I tried as per your suggestion.
This is my code:
Code:
Private Sub UserForm_Click()
'https://www.mrexcel.com/forum/excel-questions/1019120-get-value-boolean-variable-passing-its-name-text.html
'RoryA
'I would suggest that you use an array of booleans
'populated in the same order as your ChosenFieldList string.
'You can then simply use the i variable as an index into that array in your loop.
'Regarding msgbox, I suspect you have at some point used that as a function/variable name in lower case.
 Dim Address(4) As String
 Dim ArrBools(4) As Boolean
 ArrBools(0) = ChosenName
 ArrBools(1) = ChosenAddress
 ArrBools(2) = ChosenState
 ArrBools(3) = ChosenPinCode
 Dim ChosenFieldList As String
 Dim i As Integer
 Dim SelFldNam() As String
 Address(0) = "ChosenName"
 Address(1) = "ChosenAddress"
 Address(2) = "ChosenState"
 Address(3) = "ChosenPinCode"
 'Now, the User choses the option button for the ChosenName in the Userform.
 'So, the result is...
 ChosenName = True
 ChosenFieldList = "ChosenName, ChosenAddress, ChosenState, ChosenPinCode"
 SelFldNam = Split(ChosenFieldList, ",") 'Text Array populated
 For i = LBound(SelFldNam) To UBound(SelFldNam)
  msgbox "SelFldNam=" & i & ". " & SelFldNam(i)
  If ArrBools(i) = True Then 'checking whether ChosenName is true
   msgbox "Address(" & i & ")=" & Address(i) & "= true"
   'some actions as this is true
  Else
   msgbox "Address(" & i & ")=" & Address(i) & "= false"
   'no actions as this is false
  End If
 Next i
End Sub
Kindly let me know how to correct this.
Thanks.
 
Last edited:
Upvote 0
This line:

Code:
ChosenName = True

should be:

Code:
arrBools(0) = True

and replace this:

Code:
 Dim Address(4) As String
 Dim ArrBools(4) As Boolean
 ArrBools(0) = ChosenName
 ArrBools(1) = ChosenAddress
 ArrBools(2) = ChosenState
 ArrBools(3) = ChosenPinCode

with this:

Code:
 Dim Address(3) As String
 Dim ArrBools(3) As Boolean

Finally, you could use an Enum to make the code a little clearer:

Code:
Private Enum OptionList
    ChosenName = 0
    ChosenAddress = 1
    ChosenState = 2
    ChosenPinCode = 3
End Enum
Private Sub UserForm_Click()
'https://www.mrexcel.com/forum/excel-questions/1019120-get-value-boolean-variable-passing-its-name-text.html
'RoryA
'I would suggest that you use an array of booleans
'populated in the same order as your ChosenFieldList string.
'You can then simply use the i variable as an index into that array in your loop.
'Regarding msgbox, I suspect you have at some point used that as a function/variable name in lower case.
 Dim Address(3) As String
 Dim ArrBools(3) As Boolean
 Dim ChosenFieldList As String
 Dim i As Integer
 Dim SelFldNam() As String
 Address(OptionList.ChosenName) = "ChosenName"
 Address(OptionList.ChosenAddress) = "ChosenAddress"
 Address(OptionList.ChosenState) = "ChosenState"
 Address(OptionList.ChosenPinCode) = "ChosenPinCode"
 'Now, the User choses the option button for the ChosenName in the Userform.
 'So, the result is...
 ArrBools(OptionList.ChosenName) = True
 ChosenFieldList = "ChosenName, ChosenAddress, ChosenState, ChosenPinCode"
 SelFldNam = Split(ChosenFieldList, ",") 'Text Array populated
 For i = LBound(SelFldNam) To UBound(SelFldNam)
  MsgBox "SelFldNam=" & i & ". " & SelFldNam(i)
  If ArrBools(i) = True Then 'checking whether ChosenName is true
   MsgBox "Address(" & i & ")=" & Address(OptionList.ChosenName) & "= true"
   'some actions as this is true
  Else
   MsgBox "Address(" & i & ")=" & Address(OptionList.ChosenName) & "= false"
   'no actions as this is false
  End If
 Next i
End Sub
 
Last edited:
Upvote 0
Mr. RoryA
Thanks for your detailed reply.
Your code is working fine.
I have used the following code in one of my vba files.
Code:
Private Type AsoDataType
   AssoNum As Integer
   Rgn As Integer
   Lun As Integer
   Grdn As Integer
End Type
But, 'Enum OptionList' is new to me. Thanks. Hereafter I will use this.
Since, the following line...
Code:
MsgBox "Address(" & i & ")=" & Address(OptionList.ChosenName) & "= true"
...displayed 'ChosenName' for all other fields,
I used select case.
Probably there may be an easy way.
Code:
For i = LBound(SelFldNam) To UBound(SelFldNam)
  msgbox "SelFldNam=" & i & ". " & SelFldNam(i)
  If ArrBools(i) = True Then 'checking whether ChosenName is true
  Select Case i
   Case 0
    msgbox "Address(" & i & ")=" & Address(OptionList.ChosenName) & "= true"
   Case 1
    msgbox "Address(" & i & ")=" & Address(OptionList.ChosenAddress) & "= true"
   Case 2
    msgbox "Address(" & i & ")=" & Address(OptionList.ChosenState) & "= true"
   Case 3
    msgbox "Address(" & i & ")=" & Address(OptionList.ChosenPinCode) & "= true"
   End Select
   'some actions as this is true
  Else
   msgbox Address(i) & "= false"
   'no actions as this is false
  End If
 Next i
Thank you so much.
 
Upvote 0
You could just use i instead of that Select Case. I only used the Enum because you specifically referred to index 0 in your original code.
 
Upvote 0
Mr. RoryA
You are right.
Though I used it in the ELSE part of my code, I missed to see your point.
Thanks for your valuable suggestion.
I have modified my code as per your guidance.
Code:
If ArrBools(i) = True Then 'checking whether ChosenName is true
    msgbox "Address(" & i & ")=" & Address(i) & "= true"
   'some actions as this is true
  Else
   msgbox Address(i) & "= false"
   'no actions as this is false
  End If
You have saved my time.
Thanks a lot.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top