+ Reply to Thread
Results 1 to 2 of 2

Sorting with Blank Cells

Hybrid View

  1. #1
    Registered User
    Join Date
    10-25-2007
    Posts
    1

    Sorting with Blank Cells

    I have a list of people, their birthdates and their addresses. There's a column for each (see example below). Clearly, each person only has one birthdate, but several of them have several addresses (vacation homes, etc.). I only typed in their names once (i.e., there's a blank value in the people and birthdate sections in the rows of extra addresses). If I sort the list alphabetically by people's name, the extra addresses end up lumped together by themselves. Is there a way to link the rows that only contain extra addresses to their corresponding person entry so that they don't get lumped together?


    Person | Birthdate | Address
    Tom XXX YYY
    Steve XXX YYY
    Maria XXX YYY
    XXX YYY
    XXX YYY
    Betty XXX YYY

    If I sort by person, the "extraneous" lines get detached from the corresponding person.

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887
    Hi excelentuser,

    I found this code at http://www.mvps.org/dmcritchie/excel/fillempt.htm that might do what you're looking for. I combined three pieces of code:
    1. The code that fills empty cells with the text in the cell above it
    2. The code to sort the selection by the name column (column A)
    3. The code to remove those duplicate text entries created in step 1.

    Prior to running the code you must select your range of data, e.g. A1:C47. Put this code in the worksheet's code module by right-clicking on the sheet tab and selecting View Code. You can then run the code by pressing ALT+F8 and selecting 'Sheet1.FillEmpty' from the list and clicking Run.
    Sub Fill_Empty()
     '--David McRitchie,  2003-07-24,  fillempt.htm
     '--Macro version of -- Excel -- Data Entry -- Fill Blank Cells
     'http://www.contextures.com/xlDataEntry02.html
     'http://www.pcworld.com/shared/printable_articles/0,1440,9346,00.html
        Dim oRng As Range
        Set oRng = Selection
        Selection.Font.Bold = True
        Selection.SpecialCells(xlCellTypeBlanks).Select
        Selection.Font.Bold = False
        Selection.FormulaR1C1 = "=R[-1]C"
        oRng.Copy
        oRng.PasteSpecial Paste:=xlValues, Operation:=xlNone, _
           SkipBlanks:=False, Transpose:=False
           
        Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlNo, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    
        Call clear_dupcells_below
    End Sub
    
    Sub clear_dupcells_below()
      'D.McRitchie,  2006-02-01 www.mvps.org/dmcritchie/excel/fillempt.htm
      Dim rng As Range, iRows As Long, iColumns As Long
      Dim ic As Long, ir As Long
      Set rng = Intersect(Selection, ActiveSheet.UsedRange)
      Dim WithWhat As Variant
      iRows = rng.Rows.Count
      iColumns = rng.Columns.Count
      If iRows < 2 Then Exit Sub
      Application.ScreenUpdating = False
      Application.Calculation = xlCalculationManual
      For ic = iColumns To 1 Step -1
         For ir = iRows To 2 Step -1
          If rng.Item(ir, ic).Value = _
             rng.Item(ir - 1, ic).Value Then
             rng.Item(ir, ic).Formula = ""
          End If
         Next ir
      Next ic
      Application.Calculation = xlCalculationAutomatic
      Application.ScreenUpdating = False
    End Sub
    Let us know how that works for you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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