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
 

Some videos you may like

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,951
Office Version
2010
Platform
Windows
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
 

Mentor82

Active Member
Joined
Dec 30, 2018
Messages
307
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
 

Watch MrExcel Video

Forum statistics

Threads
1,102,673
Messages
5,488,206
Members
407,632
Latest member
DwayneMcgee

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top