Move cells up based on cell above

cfp14

New Member
Joined
Jul 10, 2019
Messages
9
Hi All,

I need some major help haha. I'm a total newbie to VBA, started out of necessity. I need to write a function that when data from sheet 1 is inserted to sheet 2 it looks at that new data and if the cell above it is empty it moves the cell up until no empty cells remain. So essentially top justifying the cells, if that makes any sense. I don't want to just delete the empty rows as I have a set amount of 50 and need it to stay at 50. I'm not sure what the best way to go about this is but I'll give you what I have. For all I know (not a whole lot) this is all wrong. Any help is greatly appreciated!

Code:
Sub Move_Cell_Up()
    Dim rng As Range
    Dim cell As Range
    Set rng = ActiveSheet.Range("D7:D56,E7:E56")
    For Each cell In rng
        If ActiveCell.Offset(1, 0) = " " Then
            Selection.Offset(1, 0).Select
        Else
            Selection.Offset(0, 0).Select
        End If
    Next cell
End Sub
 
How about something like this... Please test on a backup copy of your data. You should be able to leave your formulas in place...

Code:
Sub SkipBlanks()


    Dim rng As Range
    Dim arr(1 To 50, 1 To 2)
    Dim i As Integer, x As Integer
    
    Set rng = Range("D7:E56")
    For x = LBound(rng.Formula) To UBound(rng.Formula)
        If Not rng.Formula(x, 1) = "" Then
            i = i + 1
            arr(i, 1) = rng.Formula(x, 1)
            arr(i, 2) = rng.Formula(x, 2)
        End If
    Next
    Range("D7:E56").ClearContents
    Range("D7").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    
End Sub
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Are you able to give me the formulas which you have in D7 and E7, which I assume are then copied down to row 56??
 
Upvote 0
OK, given some assumptions on my part.....
You have sheet 'Options' which includes 50 checkboxes which are used to select line options.
You have a column linked to the checkboxes. Assumed column F

So similar to below.


Excel 2010
CDEF
6ItemForm Reference #Action ItemLinked CheckBox
71FR-Row7StuffE7TRUE
82FR-Row8StuffE8TRUE
93FR-Row9StuffE9FALSE
104FR-Row10StuffE10TRUE
115FR-Row11StuffE11FALSE
126FR-Row12StuffE12TRUE
137FR-Row13StuffE13FALSE
148FR-Row14StuffE14FALSE
159FR-Row15StuffE15FALSE
1610FR-Row16StuffE16TRUE
1711FR-Row17StuffE17FALSE
1812FR-Row18StuffE18FALSE
1913FR-Row19StuffE19FALSE
2014FR-Row20StuffE20FALSE
2115FR-Row21StuffE21FALSE
Options


You then want formulas in a "To Do List' sheet that bring in the selected items, top justified rather than spaced out as per the Options list.
Maybe like the array formula in D7 below. Entered using Ctrl + Shift +Enter then dragged across and down as required?


Excel 2010
CDE
6ItemForm Reference #Action Item
71FR-Row7StuffE7
82FR-Row8StuffE8
93FR-Row10StuffE10
104FR-Row12StuffE12
115FR-Row16StuffE16
126
137
148
159
1610
1711
1812
To Do List
Cell Formulas
RangeFormula
D7{=IFERROR(INDEX(Options!D$7:D$56,MATCH(SMALL(IFERROR($C$7:$C$56/Options!$F$7:$F$56,"X"),$C7),IFERROR($C$7:$C$56/Options!$F$7:$F$56,"X"),0),1),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.


No need for vba. Hopefully, the principle is good but you will likely need to edit for applicable ranges.
 
Upvote 0
How about something like this... Please test on a backup copy of your data. You should be able to leave your formulas in place...

Code:
Sub SkipBlanks()


    Dim rng As Range
    Dim arr(1 To 50, 1 To 2)
    Dim i As Integer, x As Integer
    
    Set rng = Range("D7:E56")
    For x = LBound(rng.Formula) To UBound(rng.Formula)
        If Not rng.Formula(x, 1) = "" Then
            i = i + 1
            arr(i, 1) = rng.Formula(x, 1)
            arr(i, 2) = rng.Formula(x, 2)
        End If
    Next
    Range("D7:E56").ClearContents
    Range("D7").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    
End Sub
That works exactly like I wanted it to thank you! At the moment its only working for 3 lines though, trying to figure that part out.
 
Upvote 0
OK, given some assumptions on my part.....
You have sheet 'Options' which includes 50 checkboxes which are used to select line options.
You have a column linked to the checkboxes. Assumed column F

So similar to below.

Excel 2010
CDEF
6ItemForm Reference #Action ItemLinked CheckBox
71FR-Row7StuffE7TRUE
82FR-Row8StuffE8TRUE
93FR-Row9StuffE9FALSE
104FR-Row10StuffE10TRUE
115FR-Row11StuffE11FALSE
126FR-Row12StuffE12TRUE
137FR-Row13StuffE13FALSE
148FR-Row14StuffE14FALSE
159FR-Row15StuffE15FALSE
1610FR-Row16StuffE16TRUE
1711FR-Row17StuffE17FALSE
1812FR-Row18StuffE18FALSE
1913FR-Row19StuffE19FALSE
2014FR-Row20StuffE20FALSE
2115FR-Row21StuffE21FALSE

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Options



You then want formulas in a "To Do List' sheet that bring in the selected items, top justified rather than spaced out as per the Options list.
Maybe like the array formula in D7 below. Entered using Ctrl + Shift +Enter then dragged across and down as required?

Excel 2010
CDE
6ItemForm Reference #Action Item
71FR-Row7StuffE7
82FR-Row8StuffE8
93FR-Row10StuffE10
104FR-Row12StuffE12
115FR-Row16StuffE16
126
137
148
159
1610
1711
1812

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
To Do List

Array Formulas
CellFormula
D7{=IFERROR(INDEX(Options!D$7:D$56,MATCH(SMALL(IFERROR($C$7:$C$56/Options!$F$7:$F$56,"X"),$C7),IFERROR($C$7:$C$56/Options!$F$7:$F$56,"X"),0),1),"")}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



No need for vba. Hopefully, the principle is good but you will likely need to edit for applicable ranges.
The formula I'm using is this =IF(PSSR!E7=TRUE,PSSR!F7," ") . It gets the true or false from another sheet. Ill give this formula a try, thank you!
 
Upvote 0
That works exactly like I wanted it to thank you! At the moment its only working for 3 lines though, trying to figure that part out.

Can you describe the issue a little more. It should look at your entire range of D7:E56, and depending whether or not any cell in Column D is blank, condense and move the range up, starting at row 7.
 
Upvote 0
Perhaps the cells in column D are not truly blank but contain some sort of non-printing character...
 
Upvote 0
Perhaps the cells in column D are not truly blank but contain some sort of non-printing character...
It condences the first 3 lines but only those three. Everything else moves up but the spaces are still there. And I don't see why there would be. I tried clearing formatting and everything else and no luck. The formulas are all the same too (excluding sequential cell numbering)
 
Upvote 0
Perhaps a sample of your data would help us to figure out what is going on. When you say the formulas are all the same, is that good or bad. If you want the formulas to change their reference, that is not going to happen with this method.
 
Upvote 0

Excel 2010
EFG
6Linked CheckBoxForm Reference #Action Item
7TRUEFR-Row7StuffE7
8TRUEFR-Row8StuffE8
9FALSEFR-Row9StuffE9
10TRUEFR-Row10StuffE10
11FALSEFR-Row11StuffE11
12TRUEFR-Row12StuffE12
13FALSEFR-Row13StuffE13
14FALSEFR-Row14StuffE14
15FALSEFR-Row15StuffE15
16TRUEFR-Row16StuffE16
17FALSEFR-Row17StuffE17
18FALSEFR-Row18StuffE18
PSSR




Excel 2010
CDE
6ItemForm Reference #Action Item
71FR-Row7StuffE7
82FR-Row8StuffE8
93FR-Row10StuffE10
104FR-Row12StuffE12
115FR-Row16StuffE16
126
137
148
To Do List
Cell Formulas
RangeFormula
D7{=IFERROR(INDEX(PSSR!F$7:F$56,MATCH(SMALL(IFERROR($C$7:$C$56/PSSR!$E$7:$E$56,"X"),$C7),IFERROR($C$7:$C$56/PSSR!$E$7:$E$56,"X"),0),1),"")}
Press CTRL+SHIFT+ENTER to enter array formulas.


????????????//
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top