VBA to move a cell value up if blank

DMO123

Board Regular
Joined
Aug 16, 2018
Messages
99
HI, im looking for a VBA code that will help me with an issue. i have data in column N but if in column O the cell is blank i need it to move the value below up so the cell in column O is no longer blank.

essentially this if IF column N has a value and column O is blank copy the value below up from column O.

can someone help, been searching online and cant find much. i can get it working for a formula but i need to to run via a button and i needs to run on 10000 cells.

thanks!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You can try this untested procedure.

Code:
Sub t()
Dim c As Range
    With ActiveSheet
        For Each c In .Range("N2", .Cells(Rows.Count, "N").End(xlUp))
            If c.Value <> ""  And c.Offset(, 1).Value = "" Then
                c.Offset(, 1) = c.Offset(, 1).End(xlDown).Value
            End If
        Next
    End With
End Sub
 
Upvote 0
Thanks - it dose not seem to work - it changes all values to the same for some reason.
 
Upvote 0
Could you post some data sample? showing the "before & after" data.
And it isn't clear, do you want to move or copy (the value below up from column O)?
Move & copy are 2 different things.
 
Last edited:
Upvote 0
Sure:

Before:

1 N O
2 Test 1
3 --------Listed
4 Test 2
5 --------Listed
6 Test 3
7 --------Listed
8 Test 4
9 --------Listed
10 Test 5 Listed

After:
1 N O
2 Test 1 Listed
3
4 Test 2 Listed
5
6 Test 3 Listed
7
8 Test 4 Listed
9
10 Test 5 Listed

so as you can see the values in O have moved up one row but as per line 10 not all will be blank
 
Last edited:
Upvote 0
Ok, try this:

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1083797b()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1083797-vba-move-cell-value-up-if-blank.html[/COLOR][/I]
[COLOR=Royalblue]Dim[/COLOR] rng [COLOR=Royalblue]As[/COLOR] Range, va, vb, i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], n [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
     
    n = Range([COLOR=brown]"N:O"[/COLOR]).Find([COLOR=brown]"*"[/COLOR], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    vb = Range([COLOR=brown]"N1:N"[/COLOR] & n)
    [COLOR=Royalblue]Set[/COLOR] rng = Range([COLOR=brown]"O1:O"[/COLOR] & n)
    va = rng
   
    [COLOR=Royalblue]For[/COLOR] i = LBound(vb, [COLOR=crimson]1[/COLOR]) [COLOR=Royalblue]To[/COLOR] UBound(vb, [COLOR=crimson]1[/COLOR]) - [COLOR=crimson]1[/COLOR]
       
        [COLOR=Royalblue]If[/COLOR] vb(i, [COLOR=crimson]1[/COLOR]) <> [COLOR=brown]""[/COLOR] [COLOR=Royalblue]And[/COLOR] va(i, [COLOR=crimson]1[/COLOR]) = [COLOR=brown]""[/COLOR] [COLOR=Royalblue]Then[/COLOR]
            va(i, [COLOR=crimson]1[/COLOR]) = va(i + [COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR])
            va(i + [COLOR=crimson]1[/COLOR], [COLOR=crimson]1[/COLOR]) = [COLOR=brown]""[/COLOR]
        [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]
       
    [COLOR=Royalblue]Next[/COLOR]
 
rng = va
[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
[/FONT]
 
Upvote 0
Thank you - i have tested this and nothing seems to happen it gets to

Code:
[COLOR=Royalblue][FONT=&quot]For[/FONT][/COLOR][COLOR=#333333][FONT=&quot] i = LBound(vb, [/FONT][/COLOR][COLOR=crimson][FONT=&quot]1[/FONT][/COLOR][COLOR=#333333][FONT=&quot]) [/FONT][/COLOR][COLOR=Royalblue][FONT=&quot]To[/FONT][/COLOR][COLOR=#333333][FONT=&quot] UBound(vb, [/FONT][/COLOR][COLOR=crimson][FONT=&quot]1[/FONT][/COLOR][COLOR=#333333][FONT=&quot]) - [/FONT][/COLOR][COLOR=crimson][FONT=&quot]1[/FONT][/COLOR]

and goes straight to End.
 
Upvote 0
deleted, wrong table
 
Last edited:
Upvote 0
Don't know why that happen. It worked using your sample.
Try using your sample first.

Using your data sample


Book1
NO
1Test 1
2Listed
3Test 2
4Listed
5Test 3
6Listed
7Test 4
8Listed
9Test 5Listed
Sheet4



The result:


Book1
NO
1Test 1Listed
2
3Test 2Listed
4
5Test 3Listed
6
7Test 4Listed
8
9Test 5Listed
Sheet4
 
Upvote 0
My apologies it is working but only when i remove excess data, the example i gave above was how i wanted the data to be displayed but the actual data has multiple rows in like so:



CauseDet
Not RelatedListed
Listed
Listed
Listed
Not Assessable
Unlisted
Unlisted
Unlisted
Not Related
Unlisted
Unlisted
Unlisted
Not Related
Listed
Listed
Listed
Not Assessable
Unlisted
Not Assessable
Unlisted
Not Related
Listed
Listed
Listed

<tbody>
</tbody>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,428
Messages
6,119,420
Members
448,895
Latest member
omarahmed1

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