Split data in single cell

jwp

New Member
Joined
Aug 31, 2011
Messages
33
Hi

I am so close but am struggling can someone help complete my code.

I have the following in a single cell there are 2 posibilities the data is either split by " v " or a " - " I want the data split by these 2 posible characters and put into 2 cells.

Sample Data in 1 cell

Joe Bloggs v Fred Flinstone
Jeremy Clarkson - The Stig

I want this in 2 cells like
Joe Bloggs
Fred Flinstone

or
Jeremy Clarkson
The Stig


My code so far is working to split the first name with v or - but I can only paste the first name into a cell.

Code:
x=split(cell.offset(1,0), " v ")
Sheets("Temp").Range("A1") = x
Thank you for any help provided
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
This code line...

Code:
x=split(cell.offset(1,0), " v ")
puts an array in the variable x and that array is always zero-based (because Split always creates zero-based arrays). To get to any element of an array, you simply supply the numerical index of the element you want. So, for an entry of "Joe Bloggs v Fred Flinstone", x(0) contains "Joe Bloggs" and x(1) contains "Fred Flintstone". That means you can just assign whichever one you want to the Value property of the cell you want it in. For example...

Code:
x=split(cell.offset(1,0), " v ")
Range("B1").Value = x(0)
Range("B2").Value = x(1)
 
Upvote 0
Ah I understand that should be enough for me to complete my task. Will repost on my success.

Thank you!
 
Upvote 0
Can someone help me with an IF or OR statement to use the right character of " v " or " - " to split the data depending on which character is present?

Am struggling.

Thanks
 
Upvote 0
Just pick one and replace it with the other, then do your split. Something like this...

Code:
X = Split([COLOR=red]Replace([/COLOR]Cell.Offset(1, 0)[COLOR=red], " - ", " v ")[/COLOR], " v ")
Range("B1").Value = X(0)
Range("B2").Value = X(1)

The part highlighted in red is what I added to my last response. Here I replace " - " with " v " before splitting using " v " as the delimiter.
 
Upvote 0
Range("A1").Value = Split(Split(someString,"v")(0),"-")(0)
Range("A2").Value = Split(someString & "v","v")(1) & Split(someString & "-","-")(1)
 
Upvote 0
Doh it breaks down if I only have one entry

Joe Smith vs Joe Bloggs
Sam Giggs - Tod Walters
George White

How can I account for the George White not to be split but left as a single entry?

Thanks again
 
Upvote 0
Am using Rick's VBA script it fails on a single entry.

Just tried your solution and it works on a single entry.

Thank you very much. This board just keeps producing.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,729
Members
452,939
Latest member
WCrawford

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