Macro to dynamically copy new data and delete data from dynamic height

earthworm

Well-known Member
Joined
May 19, 2009
Messages
759
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I am trying to create dynamic macro if the data set does not contain the value Rizwan then copy paste it somewhere else , and then remove the data containing Rizwan from the source data . the height of data can vary and sequence is also not guranteed. how to make the macro dynamic i tried relative and absolute and made some minor modification but still i am not sure and data seems to be deleting other cells . i only need to remove filtered data keeping other data behind filter intact . :confused:


Range("F4:I4").Select
Selection.AutoFilter
Range("G4").Select
ActiveSheet.Range("$F$4:$I$12").AutoFilter Field:=2, Criteria1:="<>Rizwan" _
, Operator:=xlAnd
Range("F4:I4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("J18").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.AutoFilter
Range("F4").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("G4").Select
ActiveSheet.Range("$F$4:$I$12").AutoFilter Field:=2, Criteria1:="=Rizwan", _
Operator:=xlAnd
Range("F4").Select
ActiveCell.Offset(2, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(1, 0).Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.End(xlToRight).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Delete Shift:=xlUp
Selection.AutoFilter
Range("A1").Select
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Untested, but try
Code:
   Dim UsdRws As Long
   
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      UsdRws = Range("G" & Rows.count).End(xlUp).Row
      .Range("G4").AutoFilter 1, "<>Rizwan"
      .Range("F4:I" & UsdRws).SpecialCells(xlVisible).Copy .Range("J" & UsdRws + 10)
      .Range("G4").AutoFilter 1, "=Rizwan"
      .Range("F4:I" & UsdRws).SpecialCells(xlVisible).Delete xlUp
      .AutoFilterMode = False
      .Range("A1").Select
   End With
 
Upvote 0
code is only pasting data to other cell but not deleting
 
Upvote 0
Ok, try
Code:
Sub chk()
   Dim UsdRws As Long
   
   With ActiveSheet
      If .AutoFilterMode Then .AutoFilterMode = False
      UsdRws = Range("G" & Rows.count).End(xlUp).Row
      .Range("G4:G" & UsdRws).AutoFilter 1, "<>Rizwan"
      .Range("F4:I" & UsdRws).SpecialCells(xlVisible).Copy .Range("J" & UsdRws + 10)
      .Range("G4").AutoFilter 1, "=Rizwan"
      .Range("F4:I" & UsdRws).SpecialCells(xlVisible).Delete xlUp
      .AutoFilterMode = False
      .Range("A1").Select
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,146
Members
449,098
Latest member
Doanvanhieu

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