Solved

Using VBA to insert a row, copy format and formulas from above, and redefine a formula in a different cell

Posted on 2016-09-11
5
43 Views
Last Modified: 2016-09-12
In capture one you can see a sample of a sheet I've created.  
Capture1.PNG
When the user clicks on the command button "Add Tier 1 Account" I'd like what's visible in the second capture to appear.   Additionally, I'd like the total in I5 to include the new line of data.  
Capture2.PNG
I've pieced together some VBA code from other posts to come up with something close but I'm having a hard time getting all the way there.  My VBA skills are very weak, just started messing around with this to make life easier for work.  

Thank you all.  If you'd like, I can share my entire project but I was trying to keep this focused on my current hurdle.  I'm building a custom (obviously) commission calculation program from my sales team and I want to make it simple to use (hence the cmd buttons) and duplicate when we hire new sales staff.  

Here's the code I have in Module 1:

Public Sub cmdinsertRowBelow_Click()

  Dim lRow As Long
  Dim lRsp As Long
  On Error Resume Next
    lRow = Selection.Row()
    lRsp = MsgBox("Insert new row below " & lRow & "?", _
            vbQuestion + vbYesNo)
    If lRsp <> vbYes Then Exit Sub

ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromRightOrAbove
ActiveCell.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End Sub
0
Comment
Question by:Eric Stevens
5 Comments
 
LVL 48

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 125 total points
ID: 41793780
HI,

pls try
 Cells(ActiveCell.Row, 2).Resize(1, 6).Copy
 Cells(ActiveCell.Row, 2).Resize(1, 6).Insert Shift:=xlDown
 Cells(ActiveCell.Row, 2).Resize(1, 5).Offset(1).ClearContents

Open in new window

Regards
0
 
LVL 17

Accepted Solution

by:
Roy_Cox earned 250 total points
ID: 41793782
VBA is totally unnecessary if you use Tables

Overview of Excel Tables

Formulas and formatting are copied when a new row is added.

You can even set this up in Excel Options for earlier versions.
0
 
LVL 31

Assisted Solution

by:Rob Henson
Rob Henson earned 125 total points
ID: 41793860
For the formula in I5, if you set it to include row 7 in the first instance, when a row is inserted and row 7 moves down to row 8, the formula will adjust automatically.

I second the comment regarding tables. Formatting and formulas will be copied as rows are inserted. Likewise, with the use of a table the formula in I5 will adjust. The initial formula will look like:

=SUM(Table1[[#All],[Commision]])

Thanks
Rob H
0
 

Author Closing Comment

by:Eric Stevens
ID: 41795416
Gents, thank you all for your input.  Switching over to tables and tying in macros with a form control button will accomplish everything I need with far less of a mess.  Very helpful and I can't wait to learn more...a lot more haha.  

Cheers!
0
 
LVL 17

Expert Comment

by:Roy_Cox
ID: 41795431
Pleased to help
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now