Excel Copy Cell Above or Below | VBA Macro #14

Share on facebook
Share on twitter
Share on linkedin
Have you run into this problem? You are completing an export of records with a variety of items some of them having multiple lines of values. After completing your export, you realize there are blank rows that should have the values of the cells above or below. Which is extremely frustrating if the unique identifier is missing values that it should be associated with. A situation like this can leave you wondering “Now what?” This is why Leverage & Lean created the Copy Cell Above or Below macro that can assist you with populating all of these empty cells. Once you run this Excel macro an InputBox will appear to determine if you want the cell values from above or below the blank cells. Indicate your selection with a 1 or 2 then watch at the blank cells transform into their appropriate values. (This macro is customizable allowing you to default either the Above or Below cell avoiding the InputBox altogether)

See it in Action!

Watch this video to see this macro in action.

The Code

Here is the code behind this macro.
'Leverage & Lean "Less Clicks, More Results"
Sub CopyCellAboveBelow()
' Means variable is in use
Dim AboveorBelow As String '
Dim BelowCounter As Integer '
Dim Column As Integer '
Dim LastColumn As Integer '
Dim LastRow As Integer '
Dim Row As Integer '
 
On Error GoTo LeverageLean

'Determine if you want to copy the cell from Above or Below
AboveorBelow = InputBox("Do you want to copy the cell from Above or Below?" & vbNewLine & _
         "1. Copy Cell Above" & vbNewLine & _
         "2. Copy Cell Below")

'AboveorBelow = 1 'Default to copy cell from Above
'AboveorBelow = 2 'Default to copy cell from Below

If AboveorBelow = "" Then
Exit Sub
ElseIf AboveorBelow <> 1 And AboveorBelow <> 2 Then
MsgBox ("Please select a valid response 1 or 2.")
Exit Sub
End If
 
Row = 1
Column = 1
LastRow = Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row on worksheet
LastColumn = Cells(Row, Columns.Count).End(xlToLeft).Column 'Identify Last Column on worksheet

Do Until Row > LastRow
Do Until Column > LastColumn
If Cells(Row, Column).Value = "" Then 'If Cell is Blank determine what cell to copy
If AboveorBelow = 1 Then 'Copy Cell Above
If Cells(Row - 1, Column).Value <> "" Then
Cells(Row, Column).Value = Cells(Row - 1, Column).Value
End If
ElseIf AboveorBelow = 2 Then 'Copy Cell Below
BelowCounter = 1
Do Until Cells(Row, Column).Value <> ""
If Cells(Row + BelowCounter, Column).Value <> "" Then
Cells(Row, Column).Value = Cells(Row + BelowCounter, Column).Value
End If
BelowCounter = BelowCounter + 1
Loop
End If
End If
Column = Column + 1
Loop
Column = 1
Row = Row + 1
Loop

Range("A1").Select

MsgBox ("Copy Cell from Above or Below is complete!  Stay Awesome!")
Exit Sub

LeverageLean:
MsgBox ("Something went wrong.  Don't hesitate to email me: brentschneider@leveragelean.com")

End Sub
'Stay Awesome

Macro Monday

Here is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!

Customization

These lines of code can be customized to personalize this macro.

14, 18 and 19 Here is where you can make the InputBox a comment and instead default running the Above or Below portion of this code.

58 If you don’t want to see a finished MsgBox you can make it a comment or delete it here.

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Leverage & Lean

Is focused on improving computer processes by Leveraging programming and applying Lean principles. It is our goal to make you more efficient and effective on your computer producing “Less Clicks and More Results!”