Splitting text into columns at space every time data changes using VBA

Andrew1234

New Member
Joined
Feb 1, 2023
Messages
29
Office Version
  1. 2013
Platform
  1. Windows
Hi, I have been heavily researching on how to split information in one cell and have it put into columns using vba.
Any codes I found online either needed me to run the macro every time or they didn't work. I would like it to run every time data is added on this particular sheet.
I have a very basic understanding of vba and simply do not know how to write the coding.

Here are some images showing what I want to achieve.

excel.PNG

excel1.PNG


from these images you can see that I need to split information from column A across columns B - H.
I would like the information to be split straight away as soon as the information comes into column A. I hope this can be done.

Just to make you aware, I also have a simple code for this worksheet that simply move on to the next cell automatically after the information is scanned in using the barcode scanner.

Here is an image of the code used for moving to the next cell.
excelvba.PNG



If someone could help me with this issue it would be much appreciated.
Many thanks for your help in advance!
 
Just for fun, I know you have Excel 2013, but this ms365 solution would auto-update when new data is pasted in column A:

Book1
ABCDEFGH
1Information from scanBarcodeIn/OutQuantityFirst NameLast NameDateTime
2123 check-in 1 jimmy jones 123 456123check-in1jimmyjones123456
3456 check-out 1 jack jones 123 789456check-out1jackjones123789
Sheet2
Cell Formulas
RangeFormula
B1:H3B1=LET(x,{"Barcode","In/Out","Quantity","First Name","Last Name","Date","Time"},IFERROR(REDUCE(x,DROP(TOCOL(A:A,3),1),LAMBDA(a,b,VSTACK(a,TEXTSPLIT(b," ")))),x))
Dynamic array formulas.

Hi thank you for your help!

I have pasted your formula into cell B1 and unfortunately it is showing this error seen below.
Have I done this correct?

Thanks
JvdV.PNG
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Thank you for your prompt reply, but unfortunately when I use this code an error appears saying that an ambiguous name detected: Worksheet_Change.
You added my Change event procedure to the one you already had... you can only have one Change event procedure, so either remark out or delete the one you already had.
 
Upvote 0
You can't have two subs with the same name, either delete one or change the name of one
Ok Thank you!

i will try the above formulas again and change "Worksheet_Change" to "Text_Split"

Hopefully this will work!
 
Upvote 0
@JvdV - Here is another XL365 solution that is somewhat shorter than your formula. Here I am setting a large range (can be as large as you want)...
Excel Formula:
=VSTACK({"Barcode","In/Out","Quantity","First Name","Last Name","Date","Time"},TEXTSPLIT(TEXTJOIN("*",,A2:A99)," ","*"))
@Rick Rothstein yeah I used REDUCE() to overcome the limits of TEXTJOIN()'s 32767 characters (cell's limits) which theoretically can pose an issue for OP. Also, TOCOL(A:A,3) would make for more dynamic use I believe, filtering out empty cells.
 
Upvote 0
You added my Change event procedure to the one you already had... you can only have one Change event procedure, so either remark out or delete the one you already had.
Hi,
unfortunately I must have the first worksheet change code, As if I didn't have it, excel wouldn't know to move onto the next cell and all of the the data I wirelessly scan into the sheet would be in one cell. And therefore very difficult to get the information split.

what i will be using this for is a store inventory system. so i will select this work sheet and the next available cell in column A. Then go out into the store and scan items. as i will not be right beside the computer i would need excel to be able to automatically go down to the next cell and also split the information into the various columns. i will then create more work sheets which glean the information from this sheet.

Hopefully there is a solution and a way to work around this issue.

Again, many thanks for your help
 
Upvote 0
@JvdV - Here is another XL365 solution that is somewhat shorter than your formula. Here I am setting a large range (can be as large as you want)...
Excel Formula:
=VSTACK({"Barcode","In/Out","Quantity","First Name","Last Name","Date","Time"},TEXTSPLIT(TEXTJOIN("*",,A2:A99)," ","*"))
Hi,
Unfortunately I cannot get the these formulas to work.

All i have done is copy and paste your formulas into cell B1 and it shows an error.

Is this what i am meant to do?

thanks
 
Upvote 0
Hi,
Unfortunately I cannot get the these formulas to work.

All i have done is copy and paste your formulas into cell B1 and it shows an error.

Is this what i am meant to do?

thanks

You are using Excel 2013 ... as a consequence, you cannot use these functions ...
 
Upvote 0
You are using Excel 2013 ... as a consequence, you cannot use these functions ...
Ok thank you for letting me know as i was tying to make this work....

I have just tried your first suggestion of recording a macro. And yes it does work, but i have to click run macro every time. is there a way that i can set thuis to automatically run every time a new line of data is pasted into column A?

Also bare in mind that i need this code in to force excel to move down one cell after that data is scanned in...

Private Sub Worksheet_change(ByVal Target As Range)
Dim ChangedCell As Range
Set ChangedCell = Range(Target.Address)

'Select the next cell
ChangedCell.Offset(1, 0).Select
End Sub

This seems to be a problem as codes i have been advidsed to use above have the same name.

Is there a way that i can keep my cell auto jump code and also have and also one for splitting the text?

Many thanks
 
Upvote 0
Maybe try it this way, it will drop down a cell each time something is scanned - ready for the next scan:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim var As Variant
   
    If Target.Column <> 1 Then Exit Sub
    var = Split(Target.Value)
    Target.Offset(, 1).Resize(, UBound(var) + 1) = var
    Target.Offset(1).Select
End Sub

Sometimes it is about combining the functionality of two worksheet_change codes
 
Upvote 0
You can test following
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
' In Sheet Module
Dim ChangedCell As Range
Set ChangedCell = Range(Target.Address)
Application.Run ("TextinCols")
'Select the next cell
ChangedCell.Offset(1, 0).Select
End Sub

Sub TextinCols()
' In Standard Module - Rick's macro
 ActiveCell.TextToColumns ActiveCell.Offset(, 1), xlDelimited, , True, False, False, False, True, False
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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