move rows based on cell criteria from sheet to another

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,438
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
i have this code works well i would if is possible to make it copy rows what contains "not available" in one time in my case i have to press every time each row contains "not available " i find this is not practical and take me more time if i have 1000 rows of data
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F:F")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Dim Lastrow As Long
Lastrow = Sheets("RESULT").Cells(Rows.Count, "F").End(xlUp).Row + 1

If Target.Value = "NOT AVAILABLE" Then
Rows(Target.Row).Copy Destination:=Sheets("RESULT").Rows(Lastrow)
Rows(Target.Row).Delete
End If
End If
End Sub
thanks
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
So you don'treally need the change event code. What would better suit your purposese is a code you can attach to a button like.

VBA Code:
Sub notAvail()
Dim rng As Range
With ActiveSheet
    .UsedRange.AutoFilter 6, "Not available"
    Set rng = .UsedRange.Offset(1).SpecialCells(xlCellTypeVisible)
    rng.Copy Sheets("RESULT").Cells(Rows.Count, 6).End(xlUp).Offset(1, -5)
    rng.EntireRow.Delete
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
many thanks i just want to ask you this is not possible that by change event code
 
Upvote 0
many thanks i just want to ask you this is not possible that by change event code
The change event code would copy and paste as you make entries, one at a time as your original code did. You can use the change event to trigger the other code but then you would have duplicate entries of all the "Not available" items. You still might end up with duplicates with this code unless you delete the old data before running the code. Here is a modified version that would do that.

VBA Code:
Sub notAvail()
Dim rng As Range
With ActiveSheet
    .UsedRange.AutoFilter 6, "Not available"
    Set rng = .UsedRange.Offset(1).SpecialCells(xlCellTypeVisible)
    If Not rng Is Nothing Then 
        Sheets("RESULT").UsedRange.Offset(1).ClearContents 'Removes old data from sheet
    End If
    rng.Copy Sheets("RESULT").Cells(Rows.Count, 6).End(xlUp).Offset(1, -5)
    rng.EntireRow.Delete
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
thanks JLGWhiz two codes are important but i have a last request how can i make the code when copy data also copy headers without i do that manually
 
Upvote 0
thanks JLGWhiz two codes are important but i have a last request how can i make the code when copy data also copy headers without i do that manually
VBA Code:
Sub notAvail()
Dim rng As Range
With ActiveSheet
    .UsedRange.AutoFilter 6, "Not available"
    Set rng = .UsedRange.SpecialCells(xlCellTypeVisible)
    If Not rng Is Nothing Then
        Sheets("RESULT").UsedRange.ClearContents 'Removes old data from sheet
    End If
    rng.Copy Sheets("RESULT").Range("A1")
    rng.EntireRow.Delete
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
you misunderstood it should stay the headers in sheet"data" not remove just copy to sheet "result " sorry if i don't make clear it
 
Upvote 0
Sorry bout that. This should do what you want.

VBA Code:
Sub notAvail()
Dim rng As Range
With ActiveSheet
    .UsedRange.AutoFilter 6, "Not available"
    Set rng = .UsedRange.SpecialCells(xlCellTypeVisible)
    If Not rng Is Nothing Then
        Sheets("RESULT").UsedRange.ClearContents 'Removes old data from sheet
        rng.Copy Sheets("RESULT").Range("A1")
    End If
    Set rng = rng.Offset(1)
    rng.Offset(1).EntireRow.Delete
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
actually i no know how the code works it causes some confusing it copy data without delete any rows from sheet"data" but when i add a new data based on "not available " it removes the lastrow and copy to sheet "result" it take me so long time to i explain you what happened for me
 
Upvote 0
actually i no know how the code works it causes some confusing it copy data without delete any rows from sheet"data" but when i add a new data based on "not available " it removes the lastrow and copy to sheet "result" it take me so long time to i explain you what happened for me
Maybe you would be better off staying with your original code.
 
Upvote 0

Forum statistics

Threads
1,216,129
Messages
6,129,046
Members
449,482
Latest member
al mugheen

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