Extract first 65 characters to new cell

mordrid

Board Regular
Joined
Jul 22, 2005
Messages
244
Hi, I have been searching the message board but can not appear to find a suitable answer so decided to post asking for some help.

I have a spread sheet which I am having to reformat inorder that it can be used as a direct import into a relational database. My dilema is that some of the cells currently contain text from 10 to 240 characters and the new format has to only have 65 characters in the cell meaning that any cell with more than 65 characters needs splitting into another row i.e. say c3 has 120 characters then c3 will need to split 65 in c3 and 55 in d3.

Can anyone please help by providing me a bit of code or formula that can look at a cell and then split the data across multiple cells (either in helper cells or the next row) I am sure I can manipulate it if I can just work out the split function!!!!

I have 7000 lines plus of data to do this against so any help would be great.

Thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi,

Cant you use

=LEFT(A1,65) in column 1
=mid(a1,66,65) in column 2
=mid(A1,131,65) in column 3 etc

Kev
 
Upvote 0
One way could be (not ideal though):

D3 = Left(C3,65)
E3 = Mid(C3,66,65)
F3 = Mid(C3,131,65) etc.
 
Upvote 0
Hi

Try:
Code:
Sub SlideandDice ()
Application.ScreenUpdating = False
Dim i As Long
Dim Length As Integer
Length = 65
For i = 2 To Range("C" & Rows.Count).End(xlUp).Row
    If Len(Range("C" & i)) > Length Then
        Range("D" & i).Value = Right(Range("C" & i), Len(Range("C" & i)) - Length)
        Range("C" & i).Value = Left(Range("C" & i), Length)
    End If
Next
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi Guys, thanks so much for the quick responses, that works for me, its always the simple answer that catches me out!!!!

Thanks again really appreciate it
 
Upvote 0
Not well thought out, and I don't know if splitting words is a concern, but maybe...
Excel Workbook
ABCD
1Mary had a little lamb, who's fleece was white as snow. And everywhere that Mary went, that lamb was sure to go.1130
2Little Jack Horner Sat in the corner, Eating a Christmas pie; He put in his thumb, And pulled out a plum, And said 'What a good boy am I!'138Mary had a little lamb, who's fleece was white as snow. And60
30everywhere that Mary went, that lamb was sure to go.52
40Little Jack Horner Sat in the corner, Eating a Christmas pie;61
50He put in his thumb, And pulled out a plum, And said 'What a60
6good boy am I!'15
70
80
90
100
Sheet3
Excel 2003
Cell Formulas
RangeFormula
B1=LEN(A1)
B2=LEN(A2)
B3=LEN(A3)
B4=LEN(A4)
B5=LEN(A5)
D1=LEN(C1)
D2=LEN(C2)
D3=LEN(C3)
D4=LEN(C4)
D5=LEN(C5)
D6=LEN(C6)
D7=LEN(C7)
D8=LEN(C8)
D9=LEN(C9)
D10=LEN(C10)


Rich (BB code):
Sub exa4()
Dim Cell As Range
Dim sText As String
Dim sTmp As String
Dim bolValid As Boolean
    
    With Sheet3
        For Each Cell In Range(.Cells(1, "A"), .Cells(.Rows.Count, "A").End(xlUp))
            If Not Len(Cell.Text) > 65 Then
                .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Value = Cell.Value
            Else
                sText = Cell.Text
                
                Do While Len(sText) > 0
                    If Len(sText)<= 65 Then
                        .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Value = Trim(sText)
                        Exit Do
                    End If
                    
                    sTmp = Left(sText, 65)
                    Do
                        sTmp = Left(sTmp, Len(sTmp) - 1)
                        If Right(sTmp, 1) = Chr(32) Then
                            bolValid = True
                            Exit Do
                        End If
                    Loop While Len(sTmp) > 0 And Not Right(sTmp, 1) = Chr(32)
                    If bolValid Then
                        .Cells(.Rows.Count, "C").End(xlUp).Offset(1).Value = Trim(sTmp)
                        bolValid = False
                        sText = Mid(sText, Len(sTmp))
                        sTmp = vbNullString
                    Else
                        MsgBox "ACK!  I goobered something"
                    End If
                Loop
            End If
        Next
    End With
                        
End Sub
 
Upvote 0
Hi GTO, thanks for your suggestions I will take a look and have a play later. As for splitting words is not an issue as the important thing is 65 Chs max as otherwise the db will throw it out. Once in the db it all ends up back together when reporting etc is done so no worries there.

Its great to see the different ideas to solve the same problem, this is certainly giving me good ideas of how to move forward

Thanks again
 
Upvote 0
Hi,

GTO's code is good but i need to make some changes... Can someone help me how to add a separator to the result on column C

the output should be like this.. see sample:

sample-1.jpg


Thanks
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,849
Members
452,948
Latest member
UsmanAli786

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