VBA User Form question regarding multiple inputs

raihnman

Board Regular
Joined
Nov 5, 2002
Messages
99
I have a VBA Forms question.

Is it possible to use comma separated inputs in a single textbox on a form?

The user will be inputing a series of numbers. It could be just one number or it could be 20 numbers. Does anyone know how to assign variables to a request like this?

Thanks,

Raihnman
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It looks as though I can enter numbers in the textbox and separate them with commas. Maybe I could take the string, remove the commas and save the data in between.

The question is, how do I do that and assign each number between the commas to a variable?

Thanks,

Raihnman
 
Upvote 0
Just to clarify what I would like to do:

If the user inputs this: 0,2,4,12,14,18
I would like to do this:
V1=0
V2=2
V3=4
V4=12
V5=14
V6=18
 
Upvote 0
Here's what I came up with:

Code:
Private Sub Separate_Values()
Dim MyValue As String
Dim NewValue() As String
Dim Num_Values As Long
Dim i As Long
    MyValue = TextBox1.Value
    Do Until IsNumeric(Application.Search(",", MyValue)) = False
        If IsNumeric(Application.Search(",", MyValue)) Then
            Num_Values = Num_Values + 1
            MyValue = Right(MyValue, Len(MyValue) - Application.Search(",", MyValue))
        End If
    Loop
    MyValue = TextBox1.Value
    ReDim NewValue(Num_Values)
    For i = 0 To UBound(NewValue)
        If IsNumeric(Application.Search(",", MyValue)) Then
            NewValue(i) = Left(MyValue, Application.Search(",", MyValue) - 1)
            MyValue = Right(MyValue, Len(MyValue) - Application.Search(",", MyValue))
        Else
            NewValue(i) = MyValue
        End If
        MsgBox NewValue(i)
    Next i
End Sub
 
Upvote 0
Just an alternative

Here is a method that passes the results back as an array that can then be stepped through to get your values. Just an alternative. :confused:

Code:
Private Sub CommandButton1_Click()

Dim aryCSV As Variant

Application.ScreenUpdating = False

aryCSV = SplitText(Text:=CStr(TextBox1.Text), Separator:=",", StartPosition:=1)

Columns(1).Clear

On Error Resume Next
For i = 0 To UBound(aryCSV)
    Cells(i + 1, 1) = CDbl(aryCSV(i))
Next
On Error GoTo 0

Erase aryCSV

Application.ScreenUpdating = True

Unload Me

End Sub

'====================================================================================

Private Function SplitText(ByRef Text As String, ByRef Separator As String, ByRef StartPosition As Integer) As Variant

Dim intChr As Integer
Dim intLen As Integer
Dim intSep As Integer

Dim arySlt()
ReDim arySlt(0)

intChr = StartPosition
intLen = Len(Text)

Do

    If arySlt(0) = "" Then Else ReDim Preserve arySlt(UBound(arySlt) + 1)

    intSep = InStr(intChr, Text, Separator)

    If intSep <> 0 Then

        arySlt(UBound(arySlt)) = Mid(Text, intChr, intSep - intChr)
        Text = Mid(Text, intSep + 1, intLen - intSep)

    Else

        arySlt(UBound(arySlt)) = Text
        Exit Do

    End If

Loop

SplitText = arySlt
Erase arySlt

End Function
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,178
Members
448,871
Latest member
hengshankouniuniu

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