Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
338 views
in Technique[技术] by (71.8m points)

vba - How to copy and paste always one row below

I have been working on code to copy and paste from one worksheet to another. The data that I need to copy will always be at A1:E1, however, I need to always paste one row below. I will run it everyday, so for instance if today I paste on cells A1:E1, then tomorrow I would need to paste on A2:E2 and on the next day A3:E3... I wrote the code below which works but is not as dynamic as I need it to be. I would appreciate your help

Thank you

Sub Copy_range()

Worksheets("Dividends").Range("A1:E1").copy

Worksheets("Draft").Range("A1:E1").PasteSpecial
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This will make it more modular by skipping down past all used cells then pasting the value on the next empty cell.

Sub Copy_range()

    ' edit line below to change where data will be copied from
    Worksheets("Dividends").Range("A1:E1").Copy ' copy the value

    ' select the first cell on the "Draft" sheet
    Worksheets("Draft").Select
    ActiveSheet.Range("A1").Select

    Dim count As Integer
    count = 1

    'skip all used cells
    Do While Not (ActiveCell.value = None)
        ActiveCell.Offset(1, 0).Range("A1").Select
        count = count + 1

    Loop

    ' edit line below to change alphabetical values of where data will be placed
    Worksheets("Draft").Range("A" & count & ":E" & count).PasteSpecial ' paste the value
    ' at Acount:Ecount where count is the current row i.e. A11:E11

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...