Copy or not copy depends on cell value

KlausW

Active Member
Joined
Sep 9, 2020
Messages
386
Office Version
  1. 2016
Platform
  1. Windows
Hi

I am using this VBA code to copy from one range to another. It works just fine. Can anyone help so it only copying if the number in column P is less than 58000.

Example:
In cell P2 stand 58000, so K2 should not be copied. In cell P10 stand 57000 is written, so K10 must be copied.

Any help will be appreciated

Best regards

Klaus W

VBA Code:
Sub Macro3()

    Sheets("Worksheet").Range("k2:k400").Copy _
    Destination:=Sheets("Worksheet").Range("b2")
    Application.CutCopyMode = False
    Sheets("Worksheet").Range("b2:b400").RemoveDuplicates _
    Columns:=1, Header:=xlNo
    
   End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Try this:
VBA Code:
Sub Macro3()

    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    
    Set ws = Sheets("Worksheet")
    Set rng = ws.Range("k2:k400")
    
    Application.ScreenUpdating = False
    
    For Each cell In rng
        If cell <> "" And cell.Value < 58000 Then
            ws.Cells(ws.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = cell.Value
        End If
    Next cell
    
    ws.Range("b2:b400").RemoveDuplicates Columns:=1, Header:=xlNo
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub Macro3()

    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
   
    Set ws = Sheets("Worksheet")
    Set rng = ws.Range("k2:k400")
   
    Application.ScreenUpdating = False
   
    For Each cell In rng
        If cell <> "" And cell.Value < 58000 Then
            ws.Cells(ws.Rows.Count, "B").End(xlUp).Offset(1, 0).Value = cell.Value
        End If
    Next cell
   
    ws.Range("b2:b400").RemoveDuplicates Columns:=1, Header:=xlNo
   
    Application.ScreenUpdating = True
   
End Sub
It doesn't work, vba code should only copy the cells if the number in column P is less than 58000. KW
 
Upvote 0
Try this instead.

VBA Code:
Sub Test()
  With Sheets("Worksheet")
    With .Range("K1:P400")
      .AutoFilter Field:=6, Criteria1:="<58000"
      If .Columns(1).SpecialCells(xlVisible).Count > 1 Then .Offset(1).Resize(.Rows.Count - 1, 1).Copy Destination:=.Parent.Range("B2")
    End With
    .AutoFilterMode = False
    .Range("B2:B400").RemoveDuplicates Columns:=1, Header:=xlNo
  End With
End Sub
 
Upvote 0
Solution
Try this instead.

VBA Code:
Sub Test()
  With Sheets("Worksheet")
    With .Range("K1:P400")
      .AutoFilter Field:=6, Criteria1:="<58000"
      If .Columns(1).SpecialCells(xlVisible).Count > 1 Then .Offset(1).Resize(.Rows.Count - 1, 1).Copy Destination:=.Parent.Range("B2")
    End With
    .AutoFilterMode = False
    .Range("B2:B400").RemoveDuplicates Columns:=1, Header:=xlNo
  End With
End Sub
Hi Peter_SSs it's just as it should be, thank you very much. Have a nice day, regards from Denmark Klaus W
 
Upvote 0
It doesn't work, vba code should only copy the cells if the number in column P is less than 58000. KW
Sorry, I misread and thought the value of 58000 was in column K.
 
Upvote 0

Forum statistics

Threads
1,215,284
Messages
6,124,059
Members
449,139
Latest member
sramesh1024

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