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
47 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 49

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 32

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

920 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

13 Experts available now in Live!

Get 1:1 Help Now