VBA: Split TextBox Value 8 Char Length into Each Active Cell

excelnubz

New Member
Joined
Feb 14, 2022
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I'd like to split the textbox value in groups of 8 characters into each empty active unlocked cell (column A and Column B) after the user clicks on the submit button in the UserForm.
So, basically parse the first 8 characters into the first active cell, move to the next cell then parse the next 8 characters into that active cell, and so forth.

Here's what I have so far...

VBA Code:
Private Sub CommandButton1_Click()

    Dim myScan As String, cell As Range
    
    myScan = TextBox1.Value
    
    If myScan = "" Then
        MsgBox ("No Input")
    Else
        ActiveCell.Value = UserForm1.TextBox1.Value
        
        For Each cell In Range("A2:B31")
            ActiveCell.Value = Split(UserForm1.TextBox1.Value, "", 8)
        Next cell
        
    End If
    
    Unload Me
    
End Sub

I feel like I'm not using the Split function right in my code (I'm trying to say split textbox value, no delimiter, 8 characters in length)
and I'm missing some kind of "i" statement.

Here's a visual of what I'm looking for:
BeforeFormSubmit-min.jpg

Before Form Submit ^

AfterFormSubmit-min.jpg

After Form Submit ^
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
VBA Code:
Dim i as Long

i = 0
For Each cell In Range("A2:B31")
    ActiveCell.Value = Mid(UserForm1.TextBox1.Value, 8*i+1, 8)
    i = i + 1
Next cell
 
Upvote 0
I don't know what your active cell is but see if this heads yo in the right direction. It puts 8 characters in A1, A2, A3, until myScan has no characters left

VBA Code:
Do Until Len(myScan) = 0
  i = i + 1
  Range("A" & i).Value = Left(myScan, 8)
  myScan = Mid(myScan, 9)
Loop
 
Upvote 0
VBA Code:
Dim i as Long

i = 0
For Each cell In Range("A2:B31")
    ActiveCell.Value = Mid(UserForm1.TextBox1.Value, 8*i+1, 8)
    i = i + 1
Next cell
So it compiles fine but when I run the code, nothing seems to record in the cells (they stay blank), but maybe it's my code, here it is:
VBA Code:
Dim myScan As String, cell As Range
    Dim i As Long
    
    myScan = TextBox1.Value
    i = 0
    
    If myScan = "" Then
        MsgBox ("No Input")
    Else
        For Each cell In Range("A2:B31")
            ActiveCell.Value = Mid(UserForm1.TextBox1.Value, 8 * i + 1, 8)
            i = i + 1
        Next cell
    End If
    
    Unload Me
 
Upvote 0
I don't know what your active cell is but see if this heads yo in the right direction. It puts 8 characters in A1, A2, A3, until myScan has no characters left

VBA Code:
Do Until Len(myScan) = 0
  i = i + 1
  Range("A" & i).Value = Left(myScan, 8)
  myScan = Mid(myScan, 9)
Loop
So, this one seems to repeat the 8th character infinitely down column A and B, but maybe its the way I've applied it to my code:
VBA Code:
Dim myScan As String, cell As Range
    Dim i As Long
    
    myScan = TextBox1.Value
    i = 0
    
    If myScan = "" Then
        MsgBox ("No Input")
    Else
        Do Until Len(myScan) = 0
            i = i + 1
            Range("A2:B31" & i).Value = Left(myScan, 8)
            myScan = Mid(myScan, 9)
        Loop
    End If
    
    Unload Me
Here's a snapshot of the results:
scnshot.jpg
 
Upvote 0
but maybe its the way I've applied it to my code:
Yes, the problem is the way you have applied it.

Try this within your code.
I have assumed that the 60 cells in A2:B31 is enough to hold all the characters.

VBA Code:
i = 0
j = 1
With Range("A2:B31")
  Do Until Len(myScan) = 0
    i = i + 1
    If i > .Rows.Count Then
      i = 1
      j = j + 1
    End If
    .Cells(i, j).Value = Left(myScan, 8)
    myScan = Mid(myScan, 9)
  Loop
End With
 
Upvote 0
Solution
Yes, the problem is the way you have applied it.

Try this within your code.
I have assumed that the 60 cells in A2:B31 is enough to hold all the characters.

VBA Code:
i = 0
j = 1
With Range("A2:B31")
  Do Until Len(myScan) = 0
    i = i + 1
    If i > .Rows.Count Then
      i = 1
      j = j + 1
    End If
    .Cells(i, j).Value = Left(myScan, 8)
    myScan = Mid(myScan, 9)
  Loop
End With
This worked thank you!
 
Upvote 0
You're welcome. Thanks for the confirmation. :)
 
Upvote 0
So it compiles fine but when I run the code, nothing seems to record in the cells (they stay blank), but maybe it's my code, here it is:
VBA Code:
Dim myScan As String, cell As Range
    Dim i As Long
   
    myScan = TextBox1.Value
    i = 0
   
    If myScan = "" Then
        MsgBox ("No Input")
    Else
        For Each cell In Range("A2:B31")
            ActiveCell.Value = Mid(UserForm1.TextBox1.Value, 8 * i + 1, 8)
            i = i + 1
        Next cell
    End If
   
    Unload Me
My bad my code should be cell not ActiveCell
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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