Copy sht1 to sht2 based criteria (Script.Dictionary)

marreco

Well-known Member
Joined
Jan 1, 2011
Messages
609
Office Version
  1. 2010
Platform
  1. Windows
I need copy data to another sheet
My tab("Relatorio") hava CFOP in column M, if find any value start 1 or 2 like this(

<tbody>
[TD="width: 64"]1253/AA[/TD]
[TD="width: 64"]1949/ZZ[/TD]
[TD="width: 64"]2102/AA[/TD]
[TD="width: 64"]2353/AA[/TD]
[TD="width: 64"]2920/AA)
[/TD]

</tbody>
Copy to tab("Saida") case start like this([TABLE="width: 192"]
<colgroup><col width="64" span="3"></colgroup><tbody>[TR]
[TD="width: 64"]5921/AA[/TD]
[TD="width: 64"]6152/AA[/TD]
[TD="width: 64"]6202/AA[/TD]
[/TR]
</tbody>[/TABLE]
)

Copy to tab("Entrada")


[TABLE="width: 128"]
<colgroup><col style="width:48pt" width="64" span="2"> </colgroup><tbody>[TR]
[TD="width: 64"]CFOP[/TD]
[TD="width: 64"]Go to tab[/TD]
[/TR]
[TR]
[TD]1253/AA[/TD]
[TD]Entrada[/TD]
[/TR]
[TR]
[TD]1949/ZZ[/TD]
[TD]Entrada[/TD]
[/TR]
[TR]
[TD]2102/AA[/TD]
[TD]Entrada
[/TD]
[/TR]
[TR]
[TD]2353/AA[/TD]
[TD]Entrada[/TD]
[/TR]
[TR]
[TD]2920/AA[/TD]
[TD]Entrada[/TD]
[/TR]
[TR]
[TD]5921/AA[/TD]
[TD]Saida[/TD]
[/TR]
[TR]
[TD]6152/AA[/TD]
[TD]Saida[/TD]
[/TR]
[TR]
[TD]6202/AA[/TD]
[TD]Saida[/TD]
[/TR]
</tbody>[/TABLE]
This is the code:
Code:
Sub Copy_sht1_To_sht2()
'This code made by  Fluff, I try adapt
    Dim ws As Worksheet
    Dim UsdRws As Long
    Dim Fltr As Variant
    Dim Val As Variant
    Dim Cl As Range
    Dim Dict As Object
    
    Set ws = Sheets("Relatorio")
    UsdRws = ws.Cells(Rows.Count, 1).End(xlUp).Row
    Set Dict = CreateObject("scripting.dictionary")
    Fltr = Array("5*", "6*") 'Array("5921/AA", "6152/AA", "6202/AA")
    
    With ws
        For Each Val In Fltr
            For Each Cl In .Range("M2:M" & UsdRws)
                If Cl.Value Like Val Then Dict(Cl.Value) = vbNullString
            Next Cl
        Next Val
        .Range("A1:AS" & UsdRws).AutoFilter Field:=13, Criteria1:=Dict.Keys, Operator:=xlFilterValues
        .AutoFilter.Range.Offset(1).Copy Sheets("Saida").Range("A" & Rows.Count).End(xlUp).Offset(1)
        'If Cl.Value = Fltr Then
        '    .Range("A1:AS" & UsdRws).AutoFilter Field:=13, Criteria1:=Dict.Keys, Operator:=xlFilterValues
        '    .AutoFilter.Range.Offset(1).Copy Sheets("Saida").Range("A" & Rows.Count).End(xlUp).Offset(1)
        'Else
        '    .Range("A1:AS" & UsdRws).AutoFilter Field:=13, Criteria1:=Dict.Keys, Operator:=xlFilterValues
        '    .AutoFilter.Range.Offset(1).Copy Sheets("Entrada").Range("A" & Rows.Count).End(xlUp).Offset(1)
        'End If
    End With
    ws.AutoFilterMode = False
    Set ws = Nothing
    Set Dict = Nothing
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try
Code:
Sub marreco()
   Dim UsdRws As Long
   
   With Sheets("Relatorio")
      If .AutoFilterMode Then .AutoFilterMode = False
      UsdRws = .Range("M" & Rows.Count).End(xlUp).Row
      .Range("A1:AS" & UsdRws).AutoFilter 13, "5*", xlOr, "6*"
      .AutoFilter.Range.Offset(1).Copy Sheets("Entrada").Range("A" & Rows.Count).End(xlUp).Offset(1)
      .Range("A1:AS" & UsdRws).AutoFilter 13, "1*", xlOr, "2*"
      .AutoFilter.Range.Offset(1).Copy Sheets("Saida").Range("A" & Rows.Count).End(xlUp).Offset(1)
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Yeah, this work!

But I try learn about script.Dictinary. Can you provide a example using dictionary?

Thank you!!
 
Upvote 0
There's no need to use a Dictionary for that.
If you want examples of Dictionaries, just do an advance search and it will pull up loads of threads.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,320
Members
452,635
Latest member
laura12345

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