fixing error code copy filtered data from sheet to another

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i need help fixing this code from the firs time it works very well but when i run again filtered data in sheet "data" is gone and gives me runtime error 1004 no cells were found



VBA Code:
Sub CopyFiltered()
    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim filterRange As Range
    Dim copyRange As Range
    Dim lastRow As Long

    Set src = ThisWorkbook.Sheets("DATA")
    Set tgt = ThisWorkbook.Sheets("Sheet2")
    src.AutoFilterMode = False
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    Set filterRange = src.Range("A1:F" & lastRow)
    Set copyRange = src.Range("B2:F" & lastRow)
    filterRange.AutoFilter field:=2, Criteria1:="=" & Range("F2").Value
    copyRange.SpecialCells(xlCellTypeVisible).Copy tgt.Range("B6")

End Sub


thanks advance
 
That problem is because you have an extra column in the output sheet that is not in the data sheet.
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
i changed to this
VBA Code:
src.AutoFilter.Range.Offset(1, 1).Resize(, 5).Copy tgt.Range("B6")

it success thanks Fluff for your code and your time (y)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
hi, fluff sorry if i ask again about this thread i no know if i have to issue a new thread or you can answer me i'd just like amend the code i want when the autofilter and copy data from sheet to another and when i search a new data should copy a new data under the old data and if i repeat the data already existed the overwrite the old data

thanks advance
 
Upvote 0
Please start a new thread for this question, thanks.
 
Upvote 0

Forum statistics

Threads
1,215,227
Messages
6,123,738
Members
449,116
Latest member
alexlomt

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