Thread: Hierarchy Macro
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JW[_2_] JW[_2_] is offline
external usenet poster
 
Posts: 638
Default Hierarchy Macro

One Way. This will identify the last used row and last used column in
your spreadsheet and cycle through til complete. If you want it to
cycle through different columns/rows, just change the values assigned
to the variables as needed.
Sub likeThis()
Dim sRow As Long, lRow As Long
Dim sCol As Integer, lCol As Integer
Dim found As Boolean
sRow = 1
sCol = 1
lRow = Cells.Find(what:="*", After:=[A1], _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
lCol = Cells.Find(what:="*", After:=[A1], _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
For i = sRow To lRow
found = False
For j = sCol To lCol
If found = False Then
If IsEmpty(Cells(i, j)) Then
Cells(i, j).Delete Shift:=xlToLeft
j = j - 1
Else
found = True
End If
Else
If IsEmpty(Cells(i, j)) Then _
Cells(i, j) = Cells(i, j).Offset(0, -1)
End If
Next j
Next i
End Sub

Jason Hall wrote:
I have the following Excel table:

x = blank cells

A B C D E F G H
1 x x x x x x M R
2 x x x x x M R T
3 x x x x M R T S
4 x x x M R T S Y
5 x x M R T S Y N
6 x M R T S Y N Q
7 M R T S Y N Q W
8 x x x x M R T S

I want to have Excel see the blanks (if there is one) and place the M in
column A and any cells up to column H to be back filled with the existing
data. Please see what I want the above table to look like after macro:

A B C D E F G H
1 M R R R R R R R
2 M R T T T T T T
3 M R T S S S S S
4 M R T S Y Y Y Y
5 M R T S Y N N N
6 M R T S Y N Q Q
7 M R T S Y N Q W
8 M R T S S S S S