Better VBA Code to Insert Blank Row and Copy a Cell

tsroque

Board Regular
Joined
Jan 19, 2007
Messages
127
Office Version
  1. 365
Hey Guys -

This VBA code works, but just wondering if there is a better way to write it.

PURPOSE/GOAL: User can add additional time to a day on a timesheet. Example: Employee works two shifts. Timesheet has In/Out/In/Out columns (4) so when they clock in more than 2x, we have to insert an additional row. The users are not Excel experts so I need to make inserting a row as user-friendly as possible.

Code:
Sub AddRow()
'Inserts and Copies a Row Below a Selected Cell with Same Formatting
    With ActiveCell
        .EntireRow.Copy
        .EntireRow.Insert
    End With
    Application.CutCopyMode = False
    
' Clear Cells in Range D:G
    ActiveCell.Offset(1, 2).Select
        ActiveCell = Null
    ActiveCell.Offset(0, 1).Select
        ActiveCell = Null
    ActiveCell.Offset(0, 1).Select
        ActiveCell = Null
    ActiveCell.Offset(0, 1).Select
        ActiveCell = Null
        
' Clear Cells in Range P:Q
    ActiveCell.Offset(0, 9).Select
        ActiveCell = Null
    ActiveCell.Offset(0, 1).Select
        ActiveCell = Null
        
' Clear Cell, Copy Above Cell and Paste VALUE to Cell Below
    ActiveCell.Offset(0, -15).Select
        ActiveCell = Null
    ActiveCell.Offset(-1, 0).Select
        ActiveCell.Copy
    ActiveCell.Offset(1, 0).Select
    Selection.PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
End Sub

Thank you!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Do you really want to clear all the values in column "D:G" and columns ("P:Q")

If so you can do it this way:

Code:
Range("D:G,P:Q").ClearContents

And maybe you can explain a little more what you want to do.

Your script is doing a lot more then just inserting a row.
And it's not a really a go ideal to use active cell. When the script runs you can mess up if your active cell is not where you want it.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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