VBA - Parsing out results from inputbox

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I would like to use an input box that allows for users to enter multiple inputs separated by commas and then writes the result to cells on a sheet as text.

For example, if a user enters "364, 212, Tommy, 008" into the InputBox, the result would be:
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]364[/TD]
[/TR]
[TR]
[TD]212[/TD]
[/TR]
[TR]
[TD]Tommy[/TD]
[/TR]
[TR]
[TD]008[/TD]
[/TR]
</tbody>[/TABLE]

An entry of "Smith" (no commas involved) would result in:
[TABLE="class: grid, width: 100"]
<tbody>[TR]
[TD]Smith[/TD]
[/TR]
</tbody>[/TABLE]

I don't know how many inputs will be included in each entry or else I would just write the full entry to a cell and parse it on the sheet.

Thanks,

Josh in IN
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this on a COPY of your data NOT the ORIGINAL.
***********************************************************
Sub jardenp()
Dim myString
Dim LenMyString As Integer
Dim CurrentWord, CurrentChar As String
myString = InputBox("enter the values separated by commas")
Range("a2").Select
LenMyString = Len(myString)
CurrentWord = ""
For n = 1 To LenMyString
CurrentChar = Mid(myString, n, 1)
If CurrentChar = "," Then
ActiveCell.Value = CurrentWord
ActiveCell.Offset(1, 0).Select
CurrentWord = ""
Else
CurrentWord = CurrentWord & CurrentChar
End If
Next
ActiveCell.Value = CurrentWord
End Sub
******************************************************************************************
 
Upvote 0
Wow. Count me impressed! That works great.

I don't mean to be picky, but is there a way to paste the values as text? Specifically, so that if any number with leading zeroes is entered, then the leading zeroes would remain?

Thanks!
 
Upvote 0
Updated code:
**************************************************************************
Sub jardenp()
Dim myString
Dim LenMyString As Integer
Dim CurrentWord, CurrentChar As String
myString = InputBox("enter the values separated by commas")
Range("a2").Select
LenMyString = Len(myString)
CurrentWord = ""
For n = 1 To LenMyString
CurrentChar = Mid(myString, n, 1)
If CurrentChar = "," Then
ActiveCell.Value = "'" & CurrentWord
ActiveCell.Offset(1, 0).Select
CurrentWord = ""
Else
CurrentWord = CurrentWord & CurrentChar
End If
Next
ActiveCell.Value = "'" & CurrentWord
End Sub
**********************************************************************
 
Upvote 0
You could just use the Split function.

So say:

sz_npt="Smith, 463, My Dog"
a_npt=split(sz_npt, ", ")

That will turn it into an array:
a_npt(1)="Smith"
a_npt(2)="463"
a_npt(3)="My Dog"

I use it all the time.

-Me
 
Upvote 0
Here is another macro that will also work...
Code:
[table="width: 500"]
[tr]
	[td]Sub SplitInputBoxEntryDown()
  Dim Parts() As String
  On Error GoTo NoText
  Parts = Split(Replace(InputBox("Enter the values separated by commas"), ", ", ","), ",")
  Columns("A").Clear
  With Range("A2").Resize(1 + UBound(Parts))
    .NumberFormat = "@"
    .Cells = Application.Transpose(Parts)
  End With
NoText:
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Thank you, akmatz for the update (works) and thanks jsmath22 and Rick for the other suggestions. I really appreciate it.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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