Can anyone troubleshoot my code?

outlawdevil

Board Regular
Joined
Jun 30, 2009
Messages
238
VBA Code:
    Sub CopyFilteredData()
    Dim sName1 As String
    Dim sName2 As String
    Dim sName3 As String
    Dim sh1 As Worksheet
    Dim sh2 As Worksheet
    Dim Last As Long
    
    ''
    ''
    sName1 = "Blue"
    sName2 = "Red"
    sName3 = "white"
    Set sh1 = Sheets("Data Tab")
    Set sh2 = Sheets("Upload Tab")
    Last = sh1.Cells(Rows.Count, "C").End(xlUp).Row
    ''
    'Filter rows based on Name which is in column 3
    sh1.Range("A1:C" & Last).AutoFilter
    sh1.Range("A1:C" & Last).AutoFilter Field:=3, Criteria1:=sName
    ''
    'Copy filtered table and paste it in Destination cell.
    sh1.Range("A2:C" & Last).SpecialCells(xlCellTypeVisible).Copy
    sh2.Range("B4").PasteSpecial Paste:=xlPasteAll
    Application.CutCopyMode = False
    ''
    'Remove filter that was applied.
    ActiveSheet.AutoFilterMode = False
    ''
    End Sub

Trying to copy the "Blue" filtered data to my upload tab range but got runtime error 1004 at this line. sh1.Range("A2:C" & Last).SpecialCells(xlCellTypeVisible).Copy Thanks.

My goal is to try to filter by specific critieria, copy and paste to designated area. Screen prints for reference. thanks.
 

Attachments

  • data tab.png
    data tab.png
    18 KB · Views: 9
  • Upload tab.jpg
    Upload tab.jpg
    52.2 KB · Views: 9

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
will I be able to modify if I add more color to the item arrays and maybe other color will only need 50 rows in the future? so keeping 110 and then 50 for others.
Yes, you can add more colors into the array line
VBA Code:
items = Array("Blue", "Red", "White")
and you can modify your number of row spacings here..
VBA Code:
 last2 = last2 + 110
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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