Respective Digits in Respective numbered Textbox

NimishK

Well-known Member
Joined
Sep 4, 2015
Messages
688
Hello

Can someone help me for the following

I have 5 textbox ie from Textbox1.Text to Textbox5.Text and one More Textbox as TextNumbers.Text

I want respective Digit from below array to display in respective Numbered Textbox
VBA Code:
Dim dgtNos() As Variant
dgtNos() = Array{1, 2, 3, 4, 5}
'Further got stuck in deriving the logic


for eg if TextNumbers.Text displayed with following options
1 1 5 4 2 or 11542 or 1,1,5,4,2 or 1, 1, 5, 4, 2

then I would like to display the above different options as below

Textbox1.Text = 1 1
Textbox4.Text = 4
Textbox2.Text = 2
Textbox3.Text = ""
Textbox5.Text = 5

Your help will be appreciated and remembered always.

Thanks
NimishK
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Something like.
Code:
Private Sub TextNumbers_Change()
    Dim x, i&
    For i = 1 To 5
        Me("textbox" & i) = ""
    Next
    If Me.TextNumbers = "" Then Exit Sub
    x = Split(StrConv(Replace(Replace(Me.TextNumbers, " ", ""), ",", ""), 64), Chr(0))
    If UBound(x) <> 5 Then Exit Sub
    x(0) = Join(Array(x(0), x(1)))
    x = Application.Index(x, [{1,5,6,4,3}])
    For i = 1 To 5
        Me("textbox" & i) = x(i)
    Next
End Sub
 
Upvote 0
Thanks Fuji

Few queries on your code and some observations after implementing your code

if not mistaken you have Dim x As Variant and i as Long.

You type any number with 5 digits only TextNumbers.Text
EG1
TextNumbers.Text = 66543

in Textbox1.text it types 6 6
Textbox2. Text it types 3
Textbox3. Text it types ""
Textbox4. Text it types 4
Textbox5. Text it types 5


Secondly 6 6 or single digit 6 should not be displayed as digits will be from 1 to 5 and TextNumbers.Text will have digits only from 1 to 5
So in above case it should be
Textbox1.text = ""
Textbox2. Text = ""
Textbox3. Text = "3"
Textbox4. Text = "4"
Textbox5. Text = "5"


EG2
TextNumbers.Text = 12345
in Textbox1.text it types 1 2
Textbox2. Text it types 5
Textbox3. Text it types ""
Textbox4. Text it types 4
Textbox5. Text it types 3

I don't understand Why 1 and 2 are displayed in TextBox1.Text

Actually Textbox1.Text = "1"
Textbox2. Text = "2"
Textbox3. Text = "3"
Textbox4. Text = "4"
Textbox5. Text = "5"

As per #1 : if digits are repeated of same numbers it should display its number in Respective Textbox.Text EG 1 1 in Textbox1
I think because of eg 11542 as Digit 3 is not displayed here therefor Textbox3.Text = "" and this is observed in all the Cases of Textbox3.Text

NimishK
 
Last edited:
Upvote 0
How about such code:
VBA Code:
Private Sub TextNumbers_Change()
Dim i&, j&, newtext$
    For i = 1 To 5
      newtext = ""
      For j = 1 To Len(TextNumbers.Text) - Len(Replace(TextNumbers.Text, i, ""))
         newtext = newtext & i & " "
      Next j
      Me("TextBox" & i) = Trim(newtext)
    Next i
End Sub
 
Upvote 0
Solution
Do you mean?
1)
11234 or 1 1 2 3 4 or 1,1,2,3,4
tb1 = ""
tb2 = ""
tb3 = 2
tb4 = 3
tb5 = 4

2)
12225 or 1 2 2 2 5 or 1,2,2,2,5
tb1 = 1
tb2 = ""
tb3 = ""
tb4 = ""
tb5 = 5

2) 12121
tb1 = 1
tb2 = 2
tb3 = 1
tb4 = 2
tb5 = 1
if so,
Code:
Private Sub TextNumbers_Change()
    Dim x, i&
    For i = 1 To 5
        Me("textbox" & i) = ""
    Next
    If Me.TextNumbers = "" Then Exit Sub
    x = SplitDigits(Me.TextNumbers)
    If UBound(x) <> 5 Then Exit Sub
    For i = 1 To 5
        Me("textbox" & i) = x(i)
    Next
End Sub

Function SplitDigits(s As String)
    Dim x, i&, ii&, temp$
    s = Replace(Replace(s, " ", ""), ",", "")
    ReDim x(1 To Len(s))
    For i = 1 To Len(s)
        x(i) = Trim$(Mid$(s, i, 1)): temp = x(i)
        If (temp <> "") * (temp = Mid$(s, i + 1, 1)) Then
            x(i) = "": ii = 1
            Do While temp = Mid$(s, i + ii, 1)
                x(i + ii) = ""
                Mid$(s, i + ii, 1) = " "
                ii = ii + 1
                If i + ii > Len(s) Then Exit Do
            Loop
            i = i + ii - 2
        End If
    Next
    SplitDigits = x
End Function
 
Upvote 0
Kaper
Really Appreciate your coding. Its Perfect and Short and all Trials done irrespective of digits numbered from 6 to 9 and Lenght of digits too
Also input made with Ascending and Descending of digits. They Exactly appear in respective textboxes and when digits 6 7 8 9 0 typed it does not display. Wow
Repeated digits are also displayed respectively
Great. Really Impressed.

Fuji
Please take the feedback positively.
Tried your Code as per post #5
Your attempt was good and resulted as per your Examples in #5.
Pl see below when you asked me what i mean
I meant as below
1)
11234 or 1 1 2 3 4 or 1,1,2,3,4
tb1 = 1 1
tb2 = 2
tb3 = 3
tb4 = 4
tb5 = ""

2)
12225 or 1 2 2 2 5 or 1,2,2,2,5
tb1 = 1
tb2 = 2 2 2
tb3 = ""
tb4 = ""
tb5 = 5

3) 12121
tb1 = 1 1 1
tb2 = 2 2
tb3 = ""
tb4 = ""
tb5 = ""

4) 5443775026 As per Kaper's Code Below result comes correctly
tb1 = ""
tb2 = 2
tb3 = 3
tb4 = 4 4
tb5 = 5 5

Thanks
NimishK
 
Upvote 0
OK, misunderstood your requirement.
I thought all the digits even 6 to 9 and 0 need to be considered as valid.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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