A macro to move or copy a row with a condition

aysam

Board Regular
Joined
Jun 24, 2009
Messages
224
Office Version
  1. 2010
Platform
  1. Windows
Hi All
I need a macro to move or copy any row that has the word (( moved )) to another sheet with the same name.
the file has thousands of rows.
thanks in advance
 
Is fine, I just want to help you, in case you need help next time, would be easier if you follow my lines.
ok, I think I got your problem solve on column F you have the word moved, so my code will get that from sheet 1 will copy on sheet 2 and then delete from 1
enjoy.
VBA Code:
Sub montecarlo()

    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet

   
    Set Source = ActiveWorkbook.Worksheets("Sheet1")
    Set Target = ActiveWorkbook.Worksheets("Sheet2")

    j = 1
    For Each c In Source.Range("F1:F1000")
        If c = "moved" Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           j = j + 1
        End If
    Next c
       Range("F:F").Delete
            
End Sub
As an example I write on sheet 1 like this
1599437727911.png

after I run my code on sheet 2 you will get this
1599437786505.png

then sheet 1 column F will be delete.
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this macro. It assumes that you have headers in row 1 and your data starts in row2 and that there are no blank rows in your data. Change the sheet name that contains your data (in red) to suit your needs.

Rich (BB code):
Sub MoveRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Moved")
    With srcWS
        .Cells(1, 1).CurrentRegion.AutoFilter 6, "moved"
        .AutoFilter.Range.Offset(1).Copy desWS.Cells(desWS.Rows.Count, "A").End(xlUp).Offset(1)
        .AutoFilter.Range.Offset(1).EntireRow.Delete
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Sorry
one more edit needed
this line of the code deletes the entire row
.AutoFilter.Range.Offset(1).EntireRow.Delete
Is it possible to re-edit it to delete the range (A: L) and shift the cells up ????
I hope so
 
Upvote 0
Try replacing that line of code with this one:
VBA Code:
.Range("A2:L" & LastRow).SpecialCells(xlCellTypeVisible).Delete shift:=xlUp
 
Upvote 0
Unfortunately
It still deletes the entire row
 
Upvote 0
when there is a new use of the code,it shouldn't delete the old information in the destination sheet
then
 
Upvote 0
I mean it deletes the entire row in sheet 1.What I want is to delete the range. A.L. in sheet 1
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. (de-sensitized if necessary).
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

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