VBA to copy and paste rows onto another sheet based on criteria twice

LostBees

New Member
Joined
Apr 15, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have been trying to work this out for about 2 weeks reading the posts here and everywhere but I cant seem to get it to work at all. I need to create a VBA to move and copy rows twice based on a condition.

I have a large spreadsheet with columns A to W in it that is populated using formulas from another sheet
Sheet 1 = Input
Sheet 2 = The one I need to take from
Sheet 3 = Where I need it to go

In column J I have this formula:
=IFERROR(VLOOKUP(L5,Refernce Sheet!$H:$I,2,0),"")

I need to move all rows where there IS something in column J to Sheet 3 *TWICE* - It needs to paste as values to the next available row in Sheet 3 and then the same thing again in the row below it - ignore any rows that are blank in J on Sheet 2 and move on to check the next

Its very messy so there could be 3 rows with something then 5 without then 2 with and 7 without etc (goes down to about 12k rows)

It needs to paste as values and not as formulas in Sheet 3

Any help would be massively appreciated as a newbie I'm all in a buzz about this one :)
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Give this a go on a copy of your data.

VBA Code:
Private Sub SubCopyRows()
Dim rngRow As Range
Dim WsTarget As Worksheet
Dim WsSource As Worksheet

    ' Set worksheet object to the source sheet.
    Set WsSource = Worksheets("SourceData")
    
    ' Set worksheet object to the target sheet.
    Set WsTarget = Worksheets("TargetWorksheet")
    
    ' DELETE THESE TWO CODE LINES IF THE TARGET SHEET HAS DATA IN IT WHICH YOU WANT TO RETAIN.
    ' Clear all data in the target sheet.
    WsTarget.Cells.Clear
    ' Create column headers in the target sheet.
    WsSource.UsedRange.Rows(1).Copy WsTarget.Range("A1")
     
    ' Turn OFF auto recalc
    Application.Calculation = xlManual
    
    ' Loop through all rows in the source sheet and copy rows to the target
    ' sheet where the call in column J is blank.
    For Each rngRow In WsSource.UsedRange.Rows
        If rngRow.Cells(1, 10).Value <> "" And rngRow.Row <> 1 Then
            WsTarget.Cells(WsTarget.UsedRange.Rows.Count + 1, 1).Resize(1, rngRow.Columns.Count) = rngRow.Value
            WsTarget.Cells(WsTarget.UsedRange.Rows.Count + 1, 1).Resize(1, rngRow.Columns.Count) = rngRow.Value
        End If
    Next rngRow
    
    With WsTarget.UsedRange.Borders
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = vbBlack
    End With
    
    WsTarget.UsedRange.Font.Size = 14
    WsTarget.UsedRange.VerticalAlignment = xlCenter
    
    ' Turn ON auto recalc.
    Application.Calculation = xlAutomatic

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,556
Messages
6,114,284
Members
448,562
Latest member
Flashbond

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