Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
126 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 53

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 500 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 22

Accepted Solution

by:
Roy Cox earned 1000 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 34

Assisted Solution

by:Rob Henson
Rob Henson earned 500 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 22

Expert Comment

by:Roy Cox
ID: 41795431
Pleased to help
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

971 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