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
 
the line takes information from the "input photo" and pastes it on the "output photo"
so
WS1.Range("B9") input photo contains "name1a name1b" and pastes it to output photo in cell "B2"
but I would like it to paste name1a in cell "B2" and name1b in cell "C2"

hope that helps
 

Attachments

  • input.JPG
    input.JPG
    50.3 KB · Views: 4
  • output.JPG
    output.JPG
    40.9 KB · Views: 5
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
OK, I was a bit confused regarding the sheets. I thought it was on WS2, but it looks like it is on WS1.
So, if that is correct, I think this should work:
VBA 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"))

'Write names to cells B2 and C2 on WS1
WS1.Range("B2") = Left(WS1.Range("B9"), InStr(WS1.Range("B9"), " ") - 1)
WS1.Range("C2") = Mid(WS1.Range("B9"), InStr(WS1.Range("B9"), " ") + 1)

End Sub
 
Upvote 0
I ran the code and it split the name as requested however it wrote it on WS1 and the joined name on WS2
what I was hoping for is replacing the full name on WS2 with the split name so looking at the photos from before I would end up with this... photo attached

thanks again
 

Attachments

  • would like.JPG
    would like.JPG
    65.2 KB · Views: 4
Upvote 0
OK, I was unable to look at your images before. Now that I can, I think I see what you need.
It is a rather trivial change (I take it that you did not write the original code yourself, or you probably would have figured out what needs to be changed).

The code at the bottom of my previous post should be changed to:
VBA Code:
'Write names to cells B2 and C2 on WS2
WS2.Range("B2") = Left(WS1.Range("B9"), InStr(WS1.Range("B9"), " ") - 1)
WS2.Range("C2") = Mid(WS1.Range("B9"), InStr(WS1.Range("B9"), " ") + 1)
 
Upvote 0

Forum statistics

Threads
1,214,947
Messages
6,122,413
Members
449,082
Latest member
tish101

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