Link to home
Start Free TrialLog in
Avatar of Rayne
RayneFlag for United States of America

asked on

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
Avatar of dlmille
dlmille
Flag of United States of America image

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
Avatar of Rayne

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of dlmille
dlmille
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rayne

ASKER

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
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
Avatar of Rayne

ASKER

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
Cool ;)
Avatar of Rayne

ASKER

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
Are you only going to have invoice data on the sheet?  If so, I can programmatically find it all.

Let me know.

Dave
Avatar of Rayne

ASKER

maybe use a constant variable that could be changed to designate the column numbers? or something
Avatar of Rayne

ASKER

this would primarily be a excel form which users will be filling on a regular basis
Avatar of Rayne

ASKER

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
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
Avatar of Rayne

ASKER

Super Fast Response and  resolution!!
Avatar of Rayne

ASKER

You rock Dave, that is super perfect, I wish if the system would let me give you 1000 points. Thats a life saver definitely ;)
Avatar of Rayne

ASKER

Hello Dave,

I have posted a question relevant to this solution;
https://www.experts-exchange.com/questions/27732460/Add-New-Stuff.html

Thank you