Solved

Excel Button to copy rows and format

Posted on 2012-03-28
17
389 Views
Last Modified: 2012-05-25
Hi,
How do I go about adding a button in excel that will do this function? Let’s say user want to enter a new row for a new invoice info, THEN: just by pressing the button [Add new invoice], an empty row is copied with all the formulas of the previous row with a formatted line maintained in-between rows as the picture.
Thanks in advance. Help is greatly appreciated.

-R
buttonS.xlsx
0
Comment
Question by:Rayne
  • 10
  • 7
17 Comments
 
LVL 41

Expert Comment

by:dlmille
ID: 37780503
Something like this?

Sub addNewInvoice()
Dim wkb As Workbook
Dim wks As Worksheet
Dim lastRow As Long
Dim rng As Range
Dim r As Range
Dim lastSelect As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    Set lastSelect = Selection
    
    lastRow = wks.Range("B" & wks.Rows.Count).End(xlUp).Row
    
    wks.Range("B" & lastRow - 1, "F" & lastRow).Copy
    
    wks.Range("B" & lastRow + 1).PasteSpecial
    
    Application.CutCopyMode = False
    
    lastSelect.Select
End Sub

Open in new window


see attached.

version 2 works with only one invoice row, rather than your current starting point...


Dave
buttonS-r1.xlsm
buttonS-r2.xlsm
0
 

Author Comment

by:Rayne
ID: 37780521
Hi Dave,

Yes thats near but there is that it should only copy the exact formulas of the previous rows But Not the actual cell contents of them. So as the users enter new info into the new empty row, the calculated columns work as the previous row in terms of formulas...does that make it clear?

Thanks again for helping
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 37780524
Easily done.  I'm assuming that data has to be populated - at least the invoice number - before a new row can be generated, correct?

Option Explicit

Sub addNewInvoiceV2()
Dim wkb As Workbook
Dim wks As Worksheet
Dim lastRow As Long
Dim rng As Range
Dim r As Range
Dim lastSelect As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    Set lastSelect = Selection
    
    lastRow = wks.Range("B" & wks.Rows.Count).End(xlUp).Row
    
    wks.Range("B" & lastRow, "F" & lastRow).Copy
    
    wks.Range("B" & lastRow + 1).Resize(2, 5).PasteSpecial
    Application.CutCopyMode = False
    
    With wks.Range("B" & lastRow + 1).Resize(, 5)
        .ClearContents
        .Interior.Color = 10147522
    End With
    
    wks.Range("B" & lastRow + 2).Resize(, 5).SpecialCells(xlCellTypeConstants).ClearContents
    
    lastSelect.Select
End Sub

Open in new window


See attached.

Dave
buttonS-r3.xlsm
0
 

Author Comment

by:Rayne
ID: 37780541
Hi Dave,

Your recent code - when i click on the button, everything vanishes.
Further,  an entire row will have a mix of user entered and calculated fields
Data does not need to be populated before a new row is added. Rather, a new row is first added and then the users enter info into it.

Thank you
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37780546
I'm not sure I understand what you mean.

What I posted has one invoice line.  When you click the button, a spacer line (green) and then a new invoice line is created.  The new invoice line has only the formulas that existed on the original invoice line.

What are you seeing differently than I?

Dave
0
 

Author Comment

by:Rayne
ID: 37780557
Hi Dave,

your attached code is working as it should and its a very good idea to ensure that the rows get populated first and then the add button works, great
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37780560
Cool ;)
0
 

Author Comment

by:Rayne
ID: 37780578
Dave,

in actual, the sheet has much more number of columns than the sample file. So for example if the number of columns now is 9 (say)

can you tell me where in the your code I need to make the changes so that the range changes for that number of columns

I tied only changed this line and ran your code but got error:
wks.Range("B" & lastRow, "F" & lastRow).Copy

Changed to:
wks.Range("B" & lastRow, "J" & lastRow).Copy

So i think i need to adjust the code at several places? can you guide.

Thank you
0
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.

 
LVL 41

Expert Comment

by:dlmille
ID: 37780581
Are you only going to have invoice data on the sheet?  If so, I can programmatically find it all.

Let me know.

Dave
0
 

Author Comment

by:Rayne
ID: 37780589
maybe use a constant variable that could be changed to designate the column numbers? or something
0
 

Author Comment

by:Rayne
ID: 37780590
this would primarily be a excel form which users will be filling on a regular basis
0
 

Author Comment

by:Rayne
ID: 37780607
Hi Dave,

I figured it, also change the resizing piece in the code along with column name. That works. Thank you for your timely response. Great to have worked with you again

My Pleasure

-R
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37780612
Ok - this app assumes the invoice form is positioned on the sheet... somewhere, but nothing else is on the sheet (e.g., don't know left or right column, but there's an initial dataset to start with).  It finds the left and right columns, then generates as previously requested:

Option Explicit

Sub addNewInvoiceV2()
Dim wkb As Workbook
Dim wks As Worksheet
Dim leftCol As Long
Dim rightCol As Long
Dim lastRow As Long
Dim rng As Range
Dim r As Range
Dim lastSelect As Range
Dim rCopyRow As Range

    Set wkb = ThisWorkbook
    Set wks = wkb.ActiveSheet
    
    Set lastSelect = Selection
    
    leftCol = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlNext).Column
    rightCol = wks.Cells.Find(what:="*", LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, searchdirection:=xlPrevious).Column
    lastRow = wks.Cells(wks.Rows.Count, leftCol).End(xlUp).Row
    
    Set rCopyRow = wks.Range(wks.Cells(lastRow, leftCol), wks.Cells(lastRow, rightCol))
    
    rCopyRow.Copy
    
    rCopyRow.Offset(1, 0).Resize(2, rCopyRow.Columns.Count).PasteSpecial

    Application.CutCopyMode = False
    
    With rCopyRow.Offset(1, 0).Resize(, rCopyRow.Columns.Count)
        .ClearContents
        .Interior.Color = 10147522
    End With
    
    rCopyRow.Offset(2, 0).Resize(, rCopyRow.Columns.Count).SpecialCells(xlCellTypeConstants).ClearContents
    
    lastSelect.Select
End Sub

Open in new window


See attached.

Dave
buttonS-r3.xlsm
0
 

Author Closing Comment

by:Rayne
ID: 37780615
Super Fast Response and  resolution!!
0
 

Author Comment

by:Rayne
ID: 37780621
You rock Dave, that is super perfect, I wish if the system would let me give you 1000 points. Thats a life saver definitely ;)
0
 
LVL 41

Expert Comment

by:dlmille
ID: 37780623
;)
0
 

Author Comment

by:Rayne
ID: 38012300
Hello Dave,

I have posted a question relevant to this solution;
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Q_27732460.html

Thank you
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

This article will show you how to use shortcut menus in the Access run-time environment.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

896 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

11 Experts available now in Live!

Get 1:1 Help Now