[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 450
  • Last Modified:

Excel Button to copy rows and format

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
Rayne
Asked:
Rayne
  • 10
  • 7
1 Solution
 
dlmilleCommented:
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
 
RayneAuthor Commented:
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
 
dlmilleCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
RayneAuthor Commented:
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
 
dlmilleCommented:
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
 
RayneAuthor Commented:
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
 
dlmilleCommented:
Cool ;)
0
 
RayneAuthor Commented:
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
 
dlmilleCommented:
Are you only going to have invoice data on the sheet?  If so, I can programmatically find it all.

Let me know.

Dave
0
 
RayneAuthor Commented:
maybe use a constant variable that could be changed to designate the column numbers? or something
0
 
RayneAuthor Commented:
this would primarily be a excel form which users will be filling on a regular basis
0
 
RayneAuthor Commented:
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
 
dlmilleCommented:
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
 
RayneAuthor Commented:
Super Fast Response and  resolution!!
0
 
RayneAuthor Commented:
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
 
dlmilleCommented:
;)
0
 
RayneAuthor Commented:
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

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now