Mapping a VBA variable to a Textbox value

Oseitutuakrasi

New Member
Joined
Jun 7, 2023
Messages
38
Office Version
  1. 365
Platform
  1. Windows
Dear All,
In a procedure, I have dimensioned a variable and initialized it:

Dim u as integer
u = 8

Msgbox u gives me 8. OK.

Now in a Textbox if I enter a character (say u), I want that character to have the value of the "u" that I have initialized (that is 8)
That is:
Sub Textbox1_Change()
Dim u as integer
u = 8
Msgbox u (Result 8)
' I enter u in the textbox, then
Msgbox Textbox1.Text ' I get "u" instead of 8
End Sub
Is it possible to make the 'u' in the textbox take on value of the 'u' a have initialized.
I have tried functions like Evaluate(Textbox1.Text), Val(Textbox1.Text); CInt etc, etc

If this is possible at all, any help please?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This question pops up occasionally, but no, it's not possible. The VBA model does not allow a program to reference its own code. Depending on what you want to use it for, there are a couple of options that might work though.

VBA Code:
Sub TestOne()

' Option 1
Dim u(10)

    u(1) = 8
    
    MsgBox u(1)
    
    v = InputBox("Enter value: ")   ' Enter a 1 here
    MsgBox u(v)
    
' Option 2
Dim dic As Object
    Set dic = CreateObject("Scripting.Dictionary")
    
    dic("u") = 8
    MsgBox dic("u")
    
    v = InputBox("Enter value: ")   ' Enter a u here
    MsgBox dic(v)
    

End Sub

Both of these options use a compound data structure, and just allow you to reference an individual entry within that structure. So depending on how you design your program, you can allow the user to see some of the variables.

Hope this helps.
 
Upvote 0
Why do you need variable u?
If you just want to change "u" to "8" whenever you type "u" in the textbox, maybe something like this:
VBA Code:
Private Sub TextBox1_Change()
Dim tx As String
With TextBox1
tx = .Text
Select Case Len(tx)
    Case Is = 1
        If tx = "u" Then .Text = 8
    Case Is > 1
        If Right(tx, 1) = "u" Then
           .Text = Left(tx, Len(tx) - 1) & 8
        End If
    End Select
End With
End Sub

or if you need to use variable u, then:
VBA Code:
Private Sub TextBox1_Change()
Dim tx As String
Dim u As String

u = 8
With TextBox1
tx = .Text
Select Case Len(tx)
    Case Is = 1
        If tx = "u" Then .Text = u
    Case Is > 1
        If Right(tx, 1) = "u" Then
           .Text = Left(tx, Len(tx) - 1) & u
        End If
    End Select
End With
End Sub
 
Upvote 0
Solution
Why do you need variable u?
If you just want to change "u" to "8" whenever you type "u" in the textbox, maybe something like this:
VBA Code:
Private Sub TextBox1_Change()
Dim tx As String
With TextBox1
tx = .Text
Select Case Len(tx)
    Case Is = 1
        If tx = "u" Then .Text = 8
    Case Is > 1
        If Right(tx, 1) = "u" Then
           .Text = Left(tx, Len(tx) - 1) & 8
        End If
    End Select
End With
End Sub

or if you need to use variable u, then:
VBA Code:
Private Sub TextBox1_Change()
Dim tx As String
Dim u As String

u = 8
With TextBox1
tx = .Text
Select Case Len(tx)
    Case Is = 1
        If tx = "u" Then .Text = u
    Case Is > 1
        If Right(tx, 1) = "u" Then
           .Text = Left(tx, Len(tx) - 1) & u
        End If
    End Select
End With
End Sub
Thank you, Eric, for your extensive response
Yea I had the feeling my requirement was not possible. I will have a good look at the codes you have provided.

Enjoy the rest of your day.
 
Upvote 0

Forum statistics

Threads
1,224,815
Messages
6,181,136
Members
453,021
Latest member
Justyna P

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