简体   繁体   中英

How to copy and paste values into next empty row in table on the same Worksheet

I have 2 forms on the same worksheet. The first allows users to input contracted hours / start dates to determine their annual leave entitlement. This is a formula and the figures are in cells I20, J20 and K20.

When the User clicks a command button, these figures are to be copied over to a Running Total form on the same worksheet, to cells P17, Q17 and R17. If the User then changes their contracted hours on the first form and clicks the command button again, then the new figures are to be copied over to cells P18, Q18 and R18. This step can be repeated a number of times (the last row for copying the data is P22, Q22, R22).

This Running Total form has on its very last row a formula that sums the totals that have been put on the form.

I'm new to VBA but have managed to create the following code that copies the figures into P17, Q17 and R17 - but I'm not sure how to get the next figures into the row below. I feel this should be a straight forward thing to do but I am struggling a little with this. Any help would be appreciated.

Sub CopyRange()

'Value Paste Cells

Worksheets("Calculator").Activate

Range("P15").End(xlDown).Offset(1, 0).Select

ActiveCell.Value = Range("I20")
ActiveCell.Offset(0, 1).Value = Range("J20")
ActiveCell.Offset(0, 2).Value = Range("K20")

  Range("P23").End(xlUp).Offset(1, 0).Select

End Sub

Initially I get the figures showing in the Running Total Form copied into P17, Q17 and R17 after clicking the command button. It then shows active cell to be P18. If I then change the figures on the first form and click the command button again - nothing happens.

Without seeing your sheet, it's hard to know exactly where to place and End(xlUp) or End(xlDown) , but this should get you started in a good way. Tweak as neeeded.

With Worksheets("Calculator")

    .Range("P23").End(xlUp).Offset(1, 0).Resize(1,3).value = .Range("I20:K20").Value

End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM