Split single cell content into 2 cells

Miss L

New Member
Joined
Sep 13, 2023
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Hi all,

I m currently working on some data import from text file to excel using excel VBA programming and I m totally new.
Currently I able to convert the text into excel but somehow there's some data not splitting into separate cells.
Please see below example:
1694659061926.png

The above is an example of excel I obtained after converted from text. But noticed one data in P column A and 1000 does not split. Of course this is just a part of the excel content and an example.
What I need is to search the entire excel sheet to look for those cell with alphabet and numeric and then split the numeric into the next column.
Please see below for the expected result:
1694659132348.png

Any idea how I can do this?
Thanks
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Change references as required.
This checks from A2 on down.
Code:
Sub Maybe()
Dim c As Range
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If Len(c) > 1 And Left(c, 1) Like "[A-Z]" Then
            With c
                .Offset(, 1).Insert Shift:=xlToRight
                .Offset(, 1).Value = Mid(c, 2)
            End With
        c.Value = Left(c, 1)
        End If
    Next c
End Sub
 
Upvote 0
Change references as required.
This checks from A2 on down.
Code:
Sub Maybe()
Dim c As Range
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
        If Len(c) > 1 And Left(c, 1) Like "[A-Z]" Then
            With c
                .Offset(, 1).Insert Shift:=xlToRight
                .Offset(, 1).Value = Mid(c, 2)
            End With
        c.Value = Left(c, 1)
        End If
    Next c
End Sub
Hi, Thanks for the code. I tried modified a bit based on my content.

VBA Code:
Dim c As Range
    For Each c In Range("F2:J" & Cells(Rows.Count, 1).End(xlUp).row)
        If Len(c) > 1 And Left(c, 1) Like "m" Then
            With c
                .Offset(, 1).Insert Shift:=xlToRight
                .Offset(, 1).Value = Mid(c, 4)
            End With
        c.Value = Left(c, 3)
        End If
    Next c

This is working for me. But I have another question. If I wanna search based on alphabet more than one character 'max' instead of 'm'
How can I modify accordingly?
Thanks.
 
Last edited by a moderator:
Upvote 0
Re: "If I wanna search based on alphabet more than one character 'max' instead of 'm'"
Don't know what you mean by that. Can you explain?
 
Upvote 0
Re: "If I wanna search based on alphabet more than one character 'max' instead of 'm'"
Don't know what you mean by that. Can you explain?
Hi,
Please see below:
1694741339190.png

I would like to split the max500 to 2 cells

Thanks.
 
Upvote 0
In Post #1 it shows Column P only.
In Post #5 it does not show any Column.
In Post#3 you changed it to Range("F:J")
Can you let us know which Columns need checking so we don't keep on going around and around because we don't have the proper range.

Don't add to the clutter by quoting whole posts please.
 
Upvote 0
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

What I need is to search the entire excel sheet to look for those cell with alphabet and numeric and then split the numeric into the next column.
Give this a try with a copy of your your data.

VBA Code:
Sub SplitTextNumbers()
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long, j As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Pattern = "(^[A-Za-z]+)([0-9]+$)"
  Application.ScreenUpdating = False
  With ActiveSheet.UsedRange
    a = .Value
    For j = UBound(a, 2) To 1 Step -1
      For i = UBound(a, 1) To 1 Step -1
        If RX.Test(a(i, j)) Then
          Set M = RX.Execute(a(i, j))
          .Cells(i, j + 1).Insert Shift:=xlToRight
          .Cells(i, j).Resize(, 2).Value = Array(M.Item(0).SubMatches(0), M.Item(0).SubMatches(1))
        End If
      Next i
    Next j
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
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