简体   繁体   中英

Excel vba copy row and paste one row down based on a number in cell

Hi guys need little help on VBA macro I have data on sheet, in row 36 from A36 to Q36 that i need to copy one row down based on a number from cell S36. So if number in S36 is 5 that means that i need to copy row 36 five times from that row. Number in cell S36 goes from 0 if 0 do not copy.

This is method based upon ranges. You might want to use times_to_copy+1 , I could not tell from your question. I have not tested if this works correctly with formulas, etc. The code assumes the active sheet is the one where you want the code to apply.

Sub copy_cat()

Dim main_range As Range
Dim times_to_copy As Integer
Dim copy_to_range As Range

If (times_to_copy > 0) Then
    Set main_range = ActiveSheet.Range("A36:Q36")
    times_to_copy = ActiveSheet.Range("S36").Value
    Set copy_to_range = main_range.Resize(times_to_copy, main_range.Columns.Count)
    ' or Set copy_to_range = main_range.Resize(times_to_copy+1, main_range.Columns.Count) dunno
    copy_to_range = main_range.Value
End If

End Sub

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