VBA Macro used in the Microsoft Office Excel Application

Copy Cell Above or Below

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.
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Search for Excel Macros

Recent Macros

VBA Macro used in the Microsoft Office Outlook Application

Outlook AutoSave Attachments

Do you frequently receive email attachments that need to be saved to your computer?  Wouldn’t it be nice if these attachments were automatically saved without having to interact with them? Cool thing is there are multiple ways to do this with VBA! This Outlook macro gives you a variety of options to automatically save email attachments. It can find attachments in the emails you select or by looping through your entire inbox. You can even set up this macro to check for attachments when emails are received.

VBA Macro used in the Microsoft Office Word Application

Word Copy Multiple Documents into One

Have you ever had different Word Documents coming from various sources and need to combine them into one?  Say for example a different team member is assigned to work on the Beginning, Middle, and End of a paper.  Once each part is complete you have to open each Word Document, select all the content, then copy and navigate back to your original document to paste. Fortunately, there is a better way with VBA! With the Copy Multiple Documents into One macro you can quickly combine all these Word Documents with one click! This Word macro allows you to delegate more work because you can efficiently combine all the efforts of your team.

VBA Macro used in the Microsoft Office Excel Application

Excel Save Copy of Workbook

If you are consistently creating a copy of an Excel Workbook you are spending time completing tedious repetitive tasks. Navigating to the Workbook, saving a copy, renaming the new Workbook, and potential creating a new Folder for storage. The good news is there is a better way using VBA! This Excel Macro saves a copy of the Active Workbook. This macro saves you time by completing all the busy work. It is difficult to remain consistent with how you are storing new copies. This Excel macro standardizes storage by creating new folders making it easy to navigate through historical records.

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 Excel, Outlook, 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

Looking for more?

Subscribe as an Insider to receive additional rights.

Contact Us

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

Copyright © 2019 Leverage & Lean – Uptown Style WordPress theme by GoDaddy