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
54 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 18

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 18

Expert Comment

by:Roy_Cox
ID: 41795431
Pleased to help
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

770 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