Split Fuction Headache - Seperate Numbers & Text

MrManBoy

New Member
Joined
May 28, 2014
Messages
37
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hi All,

Issue: I am having issues with the following code to reach the desired outcome. I have had only some success with it in what I want it to do; Maybe I have misunderstood something :eeek:

Code:
Dim LR As Long, i As Long, X As Variant
LR = Range("H" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("H" & i)
        X = Split(.Value)
        .Offset(, 1).Resize(, UBound(X) + 1).Value = X
    End With
Next i

Example Data: The font in bold is what I am trying to achieve with the above code.

[TABLE="class: outer_border, width: 500, align: left"]
<tbody>[TR]
[TD]Column H[/TD]
[TD]Column I[/TD]
[TD]Column J[/TD]
[/TR]
[TR]
[TD]12345.HR Manager[/TD]
[TD]12345
[/TD]
[TD]HR Manager
[/TD]
[/TR]
[TR]
[TD]12345.HR Manager, Boss & CEO[/TD]
[TD]12345[/TD]
[TD]HR Manager, Boss & CEO
[/TD]
[/TR]
[TR]
[TD]246810.CFO & CIO[/TD]
[TD]246810
[/TD]
[TD]CFO & CIO
[/TD]
[/TR]
</tbody>[/TABLE]







Thanks in advance ;)
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
The Split function has a second argument which tells VBA where you want the split(s) to occur. If you leave it out (as is the case in the code you posted) the default is a space character. Try:

X = Split(.Value, ".")
 
Upvote 0
Solution
Pretty sure Joe figured it out for you, but just in case that doesn't work. I'll help you out. I'm not familiar with "With" loops so here is my version
Code:
LR = Range("H" & Rows.Count).End(xlUp).Row
i = 1
Do Until i > LR
    x = SPLIT(Range("H" & i).Value, ".")
    Range("I" & i).Value = x(0)
    Range("J" & i).Value = x(1)
    i = i + 1
Loop
 
Upvote 0
This is how I would write it...
Code:
Sub SplitAtFirstDot()
  Dim Cell As Range
   For Each Cell In Range("H1", Cells(Rows.Count, "H").End(xlUp))
     Cell.Offset(, 1).Resize(, 2) = Split(Cell.Value, ".", [B][COLOR=#ff0000]2[/COLOR][/B])
   Next
 End Sub
Note the red highlight optional argument in the Split function... it forces the array to only have two elements (protects against the text portion of the cell containing dots of its own).
 
Upvote 0
Thank you all for providing assistance :)

Codes work as to what I've been trying to achieve, you guys are bloody **** great, have no idea what this means *violins starts playing* haha anyways thanks fellas!

Hoo Roo!
 
Upvote 0

Forum statistics

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