Need to split the text in a cell

Marky_B

New Member
Joined
Feb 4, 2020
Messages
39
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi I currently have this code

Sub PostToRegister()
'Copies Invoice data to the Invoice Register
Dim WS1 As Worksheet
Dim WS2 As Worksheet

Set WS1 = Worksheets("Invoice")
Set WS2 = Worksheets("Summary")

'Figures out which row is the next row
nextrow = WS2.Cells(Rows.Count, 2).End(xlUp).Row + 1

'Write the Information to the Register
WS2.Cells(nextrow, 2).Resize(1, 6).Value = Array(WS1.Range("B9"), WS1.Range("E8"), WS1.Range("E9"), Range("InvBC"), Range("InvASC"), Range("InvTotal"))

End Sub

I now have the need to split the text in Cell "B9" when its extracted and paste it as 2 separate cells. It contains a forename and a surname like this "Jo Bloggs" or this "Jo-jo Bloggs" the delimitator is a " "(space)

thank you
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Can you record a macro of you using the Text to Columns tool and try to replicate that as the next line in the Macro.
 
Upvote 0
the data is dynamic so that wouldn't work. I though about doing that though.
Could you please help on the code which takes the cell text and splits it into 2?
thank you
 
Upvote 0
Try this:
VBA Code:
    Dim fname As String, lname As String
    fname = Left(WS2.Range("B9"), InStr(WS2.Range("B9"), " ") - 1)
    lname = Mid(WS2.Range("B9"), InStr(WS2.Range("B9"), " ") + 1)
and then you can put fname and lname values in any cells you want.
 
Upvote 0
yes however, your code doesn't work within mine??
sorry.
 
Upvote 0
i know this is the line that i need to change but dont know how

WS2.Cells(nextrow, 2).Resize(1, 6).Value = Array(WS1.Range("B9"), WS1.Range("E8"), WS1.Range("E9"), Range("InvBC"), Range("InvASC"), Range("InvTotal"))
WS1.Range("B9") this is the location the forename and surname is located, just don't know how to pull and split the text into 2 cells

thank you
 
Upvote 0
What is that line currently doing?
What range is it writing to, and what exactly is it writing to that range?
 
Upvote 0

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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