In Excel VBA - Copy one DataSet of 8 rows and Paste is same excel N number or times.

prashant230182

New Member
Joined
Sep 16, 2022
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
In Excel VBA - Copy one DataSet of 8 rows and Paste is same excel N number or times. I have data set of 8 rows and 5 Columns
Requirement
Copy data set of 8 rows once and paste N number of times with respect to project requirement.
I am using below code and able to paste is once, but not able to past same data set N number of times based on my requirement

ActiveSheet.UsedRange.Offset(1, 0).Copy
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste

I tired Looping too
For Row = 1 To 2
ActiveSheet.UsedRange.Offset(1, 0).Copy
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
Next Row

but not working with above logic data set paste 3 times but still with N number times its failing
maincl1cl2cl3cl4
1a1H1J1M1
2a2H2J2M2
3a3H3J3M3
4a4H4J4M4
5a5H5J5M5
6a6H6J6M6

I want below data set to be paste N number of time in loop based on my req*
1a1H1J1M1
2a2H2J2M2
3a3H3J3M3
4a4H4J4M4
5a5H5J5M5
6a6H6J6M6
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi prashant230182,

Welcome to MrExcel!!

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long, lngPasteRow As Long, lngCounter As Long
    Dim wsSrc As Worksheet
    Dim strSrcCols As String
  
    Application.ScreenUpdating = False
  
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing the data. Change to suit.
    strSrcCols = "A:E" '<-Columns containing the data. Change to suit.
  
    lngLastRow = wsSrc.Range(strSrcCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  
    For lngCounter = 1 To 8
        lngPasteRow = wsSrc.Range(strSrcCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        wsSrc.Range(Split(strSrcCols, ":")(0) & "2:" & Split(strSrcCols, ":")(1) & lngLastRow).Copy Destination:=wsSrc.Range(Split(strSrcCols, ":")(0) & lngPasteRow)
    Next lngCounter
  
    Application.ScreenUpdating = True
  
End Sub

Regards,

Robert
 
Upvote 0
Hi prashant230182,

Welcome to MrExcel!!

Try this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long, lngPasteRow As Long, lngCounter As Long
    Dim wsSrc As Worksheet
    Dim strSrcCols As String
 
    Application.ScreenUpdating = False
 
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing the data. Change to suit.
    strSrcCols = "A:E" '<-Columns containing the data. Change to suit.
 
    lngLastRow = wsSrc.Range(strSrcCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
 
    For lngCounter = 1 To 8
        lngPasteRow = wsSrc.Range(strSrcCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        wsSrc.Range(Split(strSrcCols, ":")(0) & "2:" & Split(strSrcCols, ":")(1) & lngLastRow).Copy Destination:=wsSrc.Range(Split(strSrcCols, ":")(0) & lngPasteRow)
    Next lngCounter
 
    Application.ScreenUpdating = True
 
End Sub

Regards,

Robert
Really Appreciated on quick turnaround on provide simple and quick to understand logic.

I was able to complete my task and it worked for all conditions.
Only thing, I did strSrcCols = "A:E" '<-Columns containing the data. Change to suit. <----- I provide Hardcode value on number of columns
can i provide condition on dynamically selecting the columns, as present Columns number were fixed it worked, I want to understand for Future and make it more dynamics.

But really Thanks I was able to complete the Excel automation task of one day to 1 hrs.
 
Upvote 0
But really Thanks I was able to complete the Excel automation task of one day to 1 hrs.

WOW - that's great!!

Only thing, I did strSrcCols = "A:E" '<-Columns containing the data. Change to suit. <----- I provide Hardcode value on number of columns
can i provide condition on dynamically selecting the columns, as present Columns number were fixed it worked, I want to understand for Future and make it more dynamics.

Try this where the user selects the columns to be copied:

VBA Code:
Option Explicit
Sub Macro1()

    Dim lngLastRow As Long, lngPasteRow As Long, lngCounter As Long
    Dim wsSrc As Worksheet
    Dim strSrcCols As String
    Dim rngSrcCols As Range
    
    On Error Resume Next
        Set rngSrcCols = Application.InputBox(Prompt:="Select the Entire Columns to be Copied:", Type:=8)
    On Error GoTo 0
    If rngSrcCols Is Nothing Then
        Exit Sub
    Else
        strSrcCols = CStr(rngSrcCols.Address)
    End If

    Set wsSrc = ThisWorkbook.Sheets("Sheet5") '<-Sheet name containing the data. Change to suit.
    
    lngLastRow = wsSrc.Range(strSrcCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    For lngCounter = 1 To 8
        lngPasteRow = wsSrc.Range(strSrcCols).Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
        wsSrc.Range(Split(strSrcCols, ":")(0) & "2:" & Split(strSrcCols, ":")(1) & lngLastRow).Copy Destination:=wsSrc.Range(Split(strSrcCols, ":")(0) & lngPasteRow)
    Next lngCounter
    
    Application.ScreenUpdating = True
    
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top