loop: copy if

mohamadba91

New Member
Joined
Aug 14, 2019
Messages
6
hi
i want a code which check second colulm and if to right charcter was 01 then copy entire row to another sheet
for example if b2=9164801 then copy row 2 in sheet2 and do it again until the last row
can anybody help me on it
i would appreciate you
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Does this do what you want? Change destination sheet name to suit - assumes destination sheet already exists.
Code:
Sub CopyIf01()
Dim c As Range, NxRw As Long
Application.ScreenUpdating = False
For Each c In Range("B1:B" & Cells(Rows.Count, "B").End(xlUp).Row)
    If c.Value Like "*01" Then
       With Sheets("Sheet3")
            If IsEmpty(.Range("A1")) Then
                NxRw = 1
            Else
                NxRw = .Cells(Rows.Count, "A").End(xlUp).Row + 1
            End If
        End With
        Intersect(c.EntireRow, ActiveSheet.UsedRange).Copy Destination:=Sheets("Sheet3").Cells(NxRw, "A")
    End If
Next c
With Application
    .CutCopyMode = False
    .ScreenUpdating = True
End With
 
Upvote 0
Hi,
Check this out.

Code:
Sub CopyRows()
Dim ws1 as worksheet
Dim ws2 ad worksheet
Dim i&
Dim counter&

Set ws1=worksheets(1)
Set ws2=worksheets(2)
i=2
With ws1
Do while .cells(i, "B")<>""
    If cstr(right(.cells(i,"B"),2))="01" then
         counter=counter+1
        .cells(i,"B").EntireRow.copy ws2.cells(counter,"A")
    End if
    i=i+1
Loop
end with

Set ws1=nothing
Set ws2= nothing
End sub
 
Upvote 0

Forum statistics

Threads
1,224,766
Messages
6,180,846
Members
453,001
Latest member
coulombevin

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