[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Excel Button to copy rows and format

Posted on 2012-03-28
17
Medium Priority
?
448 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 42

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 42

Accepted Solution

by:
dlmille earned 2000 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
Independent Software Vendors: 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!

 

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 42

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 42

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
 
LVL 42

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 42

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 42

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
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…

873 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