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
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 51

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 20

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 33

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 20

Expert Comment

by:Roy_Cox
ID: 41795431
Pleased to help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

707 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