Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA for idenitfying text and copying it to a new line

Can anyone help with the following?

I have a spreadsheet of approximately 15000 records where the information
has been captured incorrectly.
This needs to be Split into its own rows based on an "LO" reference number.

The field it looks at can have several types of input.
|reference number is 123456LO|
|REFERENCE NUMBER IS 123456LO AND 123457LO|
|REFERENCE NUMBER IS 123456LO23456LO AND 112233LO|

Code:
Sub test_v1() 
    Dim lRow    As Long 
    Dim i       As Long, x 
     
    lRow = Range("F" & Rows.Count).End(xlUp).Row 
     
    For i = lRow To 2 Step -1 
        If InStr(1, Cells(i, 6).Value, "LO")  0 Then 
            x = Split(Cells(i, 6), "LO") 
            Rows(i + 1 & ":" & i + UBound(x) - 1).Insert 
            Range(Cells(i, 1), Cells(i + UBound(x) - 1, 5)).FillDown 
            Cells(i, 6) = Right(x(0), 6) & "LO" 
            For j = 1 To UBound(x) - 1 
                Cells(i + j, 6) = Trim(Right(x(j), 6)) & "LO" 
            Next 
        End If 
    Next 
End Sub
The above works fine until it encounters the following.

Line 1 | 123456LO 123457LO - 123458LO |
Line 2 | TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 999999LO TEXT|

The output always go wrong with the above in cell F1 and F2

Can anyone help with this?

Thanks In Advance!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default VBA for idenitfying text and copying it to a new line

I think I'd just go through that array and look for numbers at the end:

Option Explicit
Sub test_v1()

Dim lRow As Long
Dim iRow As Long
Dim mySplit As Variant
Dim UsedLOs As Variant
Dim LOCtr As Long
Dim sCtr

lRow = Range("F" & Rows.Count).End(xlUp).Row

For iRow = lRow To 2 Step -1
If InStr(1, Cells(iRow, 6).Value, "LO") 0 Then
mySplit = Split(Cells(iRow, 6), "LO")
UsedLOs = mySplit
LOCtr = -1
For sCtr = LBound(mySplit) To UBound(mySplit)
If IsNumeric(Right(mySplit(sCtr), 6)) Then
LOCtr = LOCtr + 1
UsedLOs(LOCtr) = Right(mySplit(sCtr), 6) & "LO"
End If
Next sCtr

If LOCtr -1 Then
ReDim Preserve UsedLOs(LBound(UsedLOs) To LOCtr)
If LOCtr 0 Then
Rows(iRow + 1).Resize(LOCtr).Insert
Rows(iRow + 1).Resize(LOCtr, 5).Value _
= Rows(iRow).Resize(1, 5).Value
End If
Cells(iRow, 6).Resize(LOCtr + 1, 1).Value _
= Application.Transpose(UsedLOs)
End If
End If
Next
End Sub

williamC wrote:

Can anyone help with the following?

I have a spreadsheet of approximately 15000 records where the information
has been captured incorrectly.
This needs to be Split into its own rows based on an "LO" reference number.

The field it looks at can have several types of input.
|reference number is 123456LO|
|REFERENCE NUMBER IS 123456LO AND 123457LO|
|REFERENCE NUMBER IS 123456LO23456LO AND 112233LO|

Code:
 
 Sub test_v1()
     Dim lRow    As Long
     Dim i       As Long, x
 
     lRow = Range("F" & Rows.Count).End(xlUp).Row
 
     For i = lRow To 2 Step -1
         If InStr(1, Cells(i, 6).Value, "LO")  0 Then
             x = Split(Cells(i, 6), "LO")
             Rows(i + 1 & ":" & i + UBound(x) - 1).Insert
             Range(Cells(i, 1), Cells(i + UBound(x) - 1, 5)).FillDown
             Cells(i, 6) = Right(x(0), 6) & "LO"
             For j = 1 To UBound(x) - 1
                 Cells(i + j, 6) = Trim(Right(x(j), 6)) & "LO"
             Next
         End If
     Next
 End Sub

The above works fine until it encounters the following.

Line 1 | 123456LO 123457LO - 123458LO |
Line 2 | TEXT TEXT TEXT TEXT TEXT TEXT TEXT TEXT 999999LO TEXT|

The output always go wrong with the above in cell F1 and F2

Can anyone help with this?

Thanks In Advance!


--

Dave Peterson
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
Challenge - Excel Line Feed Character CHR(10) - How to Delete and keep the text formatting without going ro single line in a cell ? No Name Excel Worksheet Functions 7 October 7th 09 11:10 AM
Copying text with more than one line Noel Excel Worksheet Functions 1 July 30th 09 05:12 PM
Copying text from comments to cells with line feed [email protected] Excel Programming 1 July 7th 06 04:58 PM
A 2 line text showing up in the Cell in Excel prints in 1 line Danny Excel Discussion (Misc queries) 6 July 12th 05 08:47 PM
import huge text file line-by-line? rachel Excel Programming 2 November 6th 04 04:43 PM


All times are GMT +1. The time now is 02:26 AM.

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

About Us

"It's about Microsoft Excel"