Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default separate two strings

A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.



  #2   Report Post  
Posted to microsoft.public.excel.programming
JMB JMB is offline
external usenet poster
 
Posts: 2,062
Default separate two strings

Perhaps this will help some:

Sub test()
Dim lngTemp As Long
Dim sAuthor As String
Dim sTitle As String

strLiterature = Range("A1").Value
lngTemp = InStr(1, strLiterature, ":", vbTextCompare)
If lngTemp 0 Then
sAuthor = Trim$(Left$(strLiterature, lngTemp - 1))
sTitle = Trim$(Mid$(strLiterature, lngTemp + 1, Len(strLiterature)))
End If

End Sub

"Max Bialystock" wrote:

A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default separate two strings

one way, assuming data is in column A on sheet1 and you want the data in columns
C & D
Sub split_names()
Dim ws As Worksheet
Dim sName As Variant
Dim i As Long
Dim lastrow As Long
Dim sAuthor As Long, sTitle As Long
sAuthor = 3 ' column C
sTitle = 4 ' column D
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
sName = Split(ws.Range("A" & i), ":")
ws.Cells(i, sAuthor).Value = sName(0)
ws.Cells(i, sTitle).Value = sName(1)
Next

End Sub

--


Gary


"Max Bialystock" wrote in message
...
A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default separate two strings

and if you wanted to trim the spaces
Option Explicit

Sub split_names()
Dim ws As Worksheet
Dim sName As Variant
Dim i As Long
Dim lastrow As Long
Dim sAuthor As Long, sTitle As Long
sAuthor = 3 ' column C
sTitle = 4 ' column D
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
sName = Split(ws.Range("A" & i), ":")
ws.Cells(i, sAuthor).Value = Trim(sName(0))
ws.Cells(i, sTitle).Value = Trim(sName(1))
Next

End Sub

--


Gary


"Max Bialystock" wrote in message
...
A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default separate two strings

Gary,

Thanks for your help.

Max

"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
and if you wanted to trim the spaces
Option Explicit

Sub split_names()
Dim ws As Worksheet
Dim sName As Variant
Dim i As Long
Dim lastrow As Long
Dim sAuthor As Long, sTitle As Long
sAuthor = 3 ' column C
sTitle = 4 ' column D
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
sName = Split(ws.Range("A" & i), ":")
ws.Cells(i, sAuthor).Value = Trim(sName(0))
ws.Cells(i, sTitle).Value = Trim(sName(1))
Next

End Sub

--


Gary


"Max Bialystock" wrote in message
...
A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default separate two strings

What about my help?

"Max Bialystock" wrote:

Gary,

Thanks for your help.

Max

"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
and if you wanted to trim the spaces
Option Explicit

Sub split_names()
Dim ws As Worksheet
Dim sName As Variant
Dim i As Long
Dim lastrow As Long
Dim sAuthor As Long, sTitle As Long
sAuthor = 3 ' column C
sTitle = 4 ' column D
Set ws = Worksheets("sheet1")
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To lastrow
sName = Split(ws.Range("A" & i), ":")
ws.Cells(i, sAuthor).Value = Trim(sName(0))
ws.Cells(i, sTitle).Value = Trim(sName(1))
Next

End Sub

--


Gary


"Max Bialystock" wrote in message
...
A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.







  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default separate two strings

A cell contains an author's name and a book title separated by a colon.

Charles Dickens : Great Expectations

I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.

In this case there is always a space before and after the colon.


CellValue = "Charles Dickens : Great Expectations"
sAuthor = Split(CellValue, " : ")(0)
sTitle = Split(CellValue, " : ")(1)

Rick
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 42
Default separate two strings

initial = cells("A1")
mid_index = Instr(intitial, " : ")
name = left(intial,mid_index-1)
title = mid(initial,mid_index+3)

Max Bialystock" wrote:

A cell contains an author's name and a book title separated by a colon.



Charles Dickens : Great Expectations



I need to get "Charles Dickens" into sAuthor

and "Great Expectations" into sTitle.



In this case there is always a space before and after the colon.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
separate strings cz1 Excel Discussion (Misc queries) 2 October 29th 09 10:07 PM
how do i separate 2 strings of a cell in different cells? Bona Excel Worksheet Functions 1 July 26th 07 11:32 AM
Comparing character strings in separate cells Odin[_2_] Excel Programming 6 October 13th 06 08:36 AM
Break up strings into separate cells al007 Excel Programming 4 November 23rd 05 11:14 PM
Match data of two identical strings in two separate spreadhseets Mahendra Excel Discussion (Misc queries) 0 September 14th 05 11:40 PM


All times are GMT +1. The time now is 05:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"