Provide a means to reformat a table such that only the same value
is on each row by pushing down values on row that are higher than
the lowest non-blank value on a row.
  | A | B | C |
1 |
2005-03-01 |
2005-03-02 |
2005-03-03 |
2 | apples | apples |
avocados |
3 | bananas |
bananas | oranges |
4 | oranges | |
peaches |
| |
  | A | B | C |
1 |
2005-03-01 |
2005-03-02 |
2005-03-03 |
2 | apples | apples |
|
3 | | |
avocados |
4 | bananas |
bananas | |
5 | oranges | |
oranges |
6 | | |
peaches |
|
Don't know why this has become such a popular macro, but in order for the macro
to function the following things have been included.
Option Explicit
Sub pushdown_high()
'David McRitchie, 2005-02-09 rev. 2006-05-23
Dim r As Long, c As Long, val As String
Dim xLong as Long
Dim too_high As String, LastCol As Long
LastCol = Cells.SpecialCells(xlLastCell).Column
too_high = application.REPT(CHR(255),20) 'instead of "ZZZZZZZZZZ"
'--Sort each column starting at row 2 (column has headers)
For c = 1 To LastCol
Columns(c).Sort Key1:=Cells(2, c), Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Next c
For r = 2 To 5000
val = too_high
'-- Find lowest value in a row
For c = 1 To LastCol
If Trim(Cells(r, c).Value) <> "" And _
UCase(Cells(r, c).Value) < UCase(val) Then
val = Cells(r, c).Value
End If
Next c
'-- if not the lowest value in row push down values in column
For c = 1 To LastCol
If Trim(Cells(r, c).Value) <> "" And _
UCase(Cells(r, c).Value) > UCase(val) Then
Cells(r, c).Insert Shift:=xlDown
End If
Next c
If val = too_high Then GoTo all_done
Next r
all_done:
xlong = ActiveSheet.UsedRange.Rows.Count _
+ ActiveSheet.UsedRange.Columns.Count 'Tip73
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Sub pushdown_restart()
' Select all data, delete empty cells shifting up
Cells.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.Delete Shift:=xlUp
End Sub
'test data
' Bh72 R.sol A.tum EbN1 N.euro G.metali
'PF00013 PF00013 PF00013 PF00013 PF00013 PF00013
'PF00015 PF00015 PF00015 PF00015 PF00015 PF00014
'PF00023 PF00023 PF00023 PF00023 PF00016 PF00015
'PF00027 PF00027 PF00024 PF00027 PF00023 PF00023
'PF00028 PF00032 PF00027 PF00032 PF00027 PF00027
'PF00032 PF00033 PF00032 PF00033 PF00032 PF00032
'PF00033 PF00034 PF00033 PF00034 PF00033 PF00033
'PF00034 PF00035 PF00034 PF00035 PF00034 PF00034
'PF00035 PF00036 PF00035 PF00036 PF00035 PF00035