Copy & Paste when range meets criteria

bloodmilksky

Board Regular
Joined
Feb 3, 2016
Messages
202
Hi Guys, I hope you are all well.

I was just wondering if anyone would know how I can amend the below code so that it will only copy rows where the value in column D exceeds a Value of 0

any help would be greatly appreciated

Many thanks

jamie

Code:
    Sheets("Menu").Range("B7:D68").Copy
    Sheets("LensOrder").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
    Sheets("Menu").Range("C3").Select
    Selection.ClearContents
    Range("QTYS").Select
    Selection.ClearContents
    Range("C3").Selec
 
Last edited:

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
If I understand you correctly you need the values from B thru D on Menu to be copied to LensOrder from A thru C starting on the last blank row of LensOrder.

Instead of a quick copy of the entire range my solution copies individual rows from Menu to LensOrder, if and only if the value in D is numeric, and is greater than zero. If not the solution just skips the row in Menu; it does not skip a row in LensOrder. If you would like it to skip a row in LensOrder when D=0 then change where it says
[pasteRow = pasteRow] to [pasteRow = pasteRow +1]

We have grown from two lines to a monster. You might want another VBA expert to look at this problem for a simpler solution. However, if my assumption was correct this code will work for you. If you like this, then please LIKE this.

Code:
Sub newCode()
    Dim rowNbr As Long
    Dim colNbr As Integer
    
    Dim Menu As Worksheet
    Set Menu = Sheets("Menu")
    
    Dim LensOrder As Worksheet
    Set LensOrder = Sheets("LensOrder")
    
    Dim startCol As Integer
    Dim startRow As Long
        startRow = 7
        startCol = 2
        
    Dim lastCol As Long
    Dim lastRow As Long
        lastRow = 68
        lastCol = 4
    
    Dim pasteCol As Integer
    Dim pasteRow As Long
        pasteRow = LensOrder.Cells(Rows.Count, 1).End(xlUp).Row + 1
        
    For rowNbr = startRow To lastRow
        pasteCol = 1
        If IsNumeric(Menu.Cells(rowNbr, lastCol).Value) Then
            If Menu.Cells(rowNbr, lastCol).Value > 0 Then
                For colNbr = startCol To lastCol
                    LensOrder.Cells(pasteRow, pasteCol).Value = Menu.Cells(rowNbr, colNbr).Value
                    pasteCol = pasteCol + 1
                Next colNbr
                pasteRow = pasteRow + 1
            Else
                pasteRow = pasteRow
            End If
        End If
    Next rowNbr
    
    Sheets("Menu").Range("C3").Select
    Selection.ClearContents
    Range("QTYS").Select
    Selection.ClearContents
    Range("C3").Selec
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,596
Messages
6,120,438
Members
448,966
Latest member
DannyC96

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