+ Reply to Thread
Results 1 to 3 of 3

Deleting second line in a cell

Hybrid View

  1. #1
    Registered User
    Join Date
    04-13-2013
    Location
    Calcutta
    MS-Off Ver
    Excel 2013
    Posts
    87

    Deleting second line in a cell

    Hello

    I have a dataset where the address and ZIP columns contain 2 lines of data, please see the attached file.

    However, the first line of the address and zip is correct and the second line isn't

    How can I keep only the first line of the cell and delete the second one? There are about 49K records in the final file


    Many thanks
    Attached Files Attached Files
    Last edited by probuddha; 05-12-2016 at 01:12 PM.

  2. #2
    Forum Contributor
    Join Date
    10-19-2012
    Location
    Omaha, Nebraska USA
    MS-Off Ver
    Excel 2010
    Posts
    249

    Re: Deleting second line in a cell

    Hi,

    Here is some VBA code that should get rid of the 2nd line.

    Sub Delete2ndLine()
    
    ' Define Variables
    Dim LastCol, LastRow, i, j, k As Long
    
    ' Get LastCol and LastRow with Data
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    ' Go through data and delete 2nd line, if there is one
    For i = 1 To LastRow
       For j = 1 To LastCol
          k = InStr(Cells(i, j), Chr(10)) -1
          If (k > 0) Then Cells(i, j) = Left(Cells(i, j), k)
       Next j
    Next i
    
    ' Give Completed Message
    MsgBox "Complete."
    
    End Sub
    Hope that helps,

    Dan
    Last edited by djbomaha; 05-12-2016 at 02:59 PM. Reason: Adjusted code to remove return as well

  3. #3
    Forum Expert newdoverman's Avatar
    Join Date
    02-07-2013
    Location
    Port Dover, Ontario, Canada
    MS-Off Ver
    2010
    Posts
    10,330

    Re: Deleting second line in a cell

    Enter this in C2 and fill down. When done, you can select column C, copy and paste values to get rid of the formulae then delete column B
    Formula: copy to clipboard
    =LEFT(B2,FIND(CHAR(10),B2))


    A
    B
    C
    1
    address
    zip
    2
    845 Sir Thomas Ct Ste 12
    7 Carousel Cir
    17109-4843
    17033
    17109-4843
    3
    33 N Duke St
    130 E Woods Dr
    17602-2842
    17543-8651
    17602-2842
    4
    3000 Two Logan Sq
    417 Lindy Ln
    19103-2799
    19004-1334
    19103-2799
    5
    1200 Wilson Dr
    19380-4262
    19382
    19380-4262
    6
    124 N 4th St
    219 Arch St
    17801-2211
    17801-2220
    17801-2211
    <---------If you like someone's answer, click the star to the left of one of their posts to give them a reputation point for that answer.
    Ron W

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro for Deleting every n^th line
    By Al3xTa in forum Excel Programming / VBA / Macros
    Replies: 24
    Last Post: 02-02-2015, 07:25 PM
  2. [SOLVED] Line not deleting like it shouild
    By mahat in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 08-12-2014, 06:48 PM
  3. deleting a line based on data in a previous line
    By El Conquistador in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-30-2013, 09:49 AM
  4. deleting new line character
    By heathboy1986 in forum Excel General
    Replies: 25
    Last Post: 04-29-2011, 02:03 AM
  5. Deleting extra line at beginning of cell
    By sysco in forum Excel General
    Replies: 4
    Last Post: 02-21-2007, 05:17 PM
  6. Deleting this white line?
    By Ozz in forum Excel General
    Replies: 6
    Last Post: 07-17-2006, 06:40 AM
  7. Macro Help - Deleting two characters per line!
    By mccrimmon in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-28-2006, 02:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1