Solved

Excel Button to copy rows and format

Posted on 2012-03-28
17
375 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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