Copy a range of cell to another sheet based on critieria.

outlawdevil

Board Regular
Joined
Jun 30, 2009
Messages
238
I have a worksheet with data tab and upload tab. I want to create a macro that can copy the range of cell from the data tab based on critieria(Blue, Red, White).

I found this macro that almost does what I want but it is pasting critieria range into new sheets. I want everything to be pasted in to specific range of the upload tab. ( I want to reuse this macro everytime, is it possible that it will do a clear content each time when paste into the range?) Can anyone help me modify this macro? thanks. I have attached screenshot for your review as well.

VBA Code:
Sub filter()
Application.ScreenUpdating = False
Dim x As Range
Dim rng As Range
Dim last As Long
Dim sht As String

'specify sheet name in which the data is stored
sht = "Data Tab"

'change filter column in the following code
last = Sheets(sht).Cells(Rows.Count, "C").End(xlUp).Row
Set rng = Sheets(sht).Range("A1:C" & last)

Sheets(sht).Range("C1:C" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True

For Each x In Range([AA2], Cells(Rows.Count, "AA").End(xlUp))
With rng
.AutoFilter
.AutoFilter Field:=3, Criteria1:=x.Value
.SpecialCells(xlCellTypeVisible).Copy

Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
ActiveSheet.Paste
End With
Next x

' Turn off filter
Sheets(sht).AutoFilterMode = False

With Application
.CutCopyMode = False
.ScreenUpdating = True
End With

End Sub
 

Attachments

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

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,

VBA Code:
Sub UsingColection()
    Dim cUnique As Collection
    Dim rng As Range, fRng As Range
    Dim c As Range
    Dim sh As Worksheet, ws As Worksheet
    Dim vNum As Variant

    Set sh = ThisWorkbook.Sheets("Data Tab")
    Set ws = Sheets("Upload Tab")
    
    With sh
        Set rng = .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
        Set fRng = .Range("A1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
        Set cUnique = New Collection

        On Error Resume Next
        For Each c In rng.Cells
            cUnique.Add c.Value, CStr(c.Value)
        Next c
        On Error GoTo 0

        For Each vNum In cUnique
            .Range("A1").AutoFilter field:=3, Criteria1:=vNum
            With ws
                .Cells(.Rows.Count, 2).End(xlUp).Offset(1, -1) = vNum
                fRng.Copy .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 1)
            End With
        Next vNum
        .AutoFilterMode = False
        
    End With
End Sub
 
Upvote 0
Solution
Try this,

VBA Code:
Sub UsingColection()
    Dim cUnique As Collection
    Dim rng As Range, fRng As Range
    Dim c As Range
    Dim sh As Worksheet, ws As Worksheet
    Dim vNum As Variant

    Set sh = ThisWorkbook.Sheets("Data Tab")
    Set ws = Sheets("Upload Tab")
   
    With sh
        Set rng = .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
        Set fRng = .Range("A1:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
        Set cUnique = New Collection

        On Error Resume Next
        For Each c In rng.Cells
            cUnique.Add c.Value, CStr(c.Value)
        Next c
        On Error GoTo 0

        For Each vNum In cUnique
            .Range("A1").AutoFilter field:=3, Criteria1:=vNum
            With ws
                .Cells(.Rows.Count, 2).End(xlUp).Offset(1, -1) = vNum
                fRng.Copy .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 1)
            End With
        Next vNum
        .AutoFilterMode = False
       
    End With
End Sub

Is there a way to make it where I can designate where each critieria range is copy to so I can change based on my preference? It is copying all range under white at the moment. I was thinking maybe make each section 100 rows so I can reuse it each time. It is important to copy the range in to their designated area.
 

Attachments

  • UPload tab.png
    UPload tab.png
    8.8 KB · Views: 25
Upvote 0
Is there a way to make it where I can designate where each critieria range is copy to so I can change based on my preference? It is copying all range under white at the moment. I was thinking maybe make each section 100 rows so I can reuse it each time. It is important to copy the range in to their designated area.

Clear the ws cells before running the code
thank you for your help!
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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