Mejorar velocidad del procedimiento del codigo

dragonfire33

Board Regular
Joined
Oct 7, 2021
Messages
90
Office Version
  1. 365
Platform
  1. Windows
Buenos días como puedo mejorar el procedimiento de el siguiente código
VBA Code:
Sub Repetidos()

'Por.Dante Amor

    col = "TG"

    '

    Application.ScreenUpdating = False

    c = Columns(col).Column

    Range(Cells(1, c), Cells(1, c + 2)).EntireColumn.ClearContents

    For Each n In Range("A1:SZ217").SpecialCells(xlCellTypeConstants, 23)

        Set b = Columns(c).Find(n.Value, lookat:=xlWhole)

        If Not b Is Nothing Then

            Cells(b.Row, c + 1) = Cells(b.Row, c + 1) + 1

            Cells(b.Row, c + 2) = Cells(b.Row, c + 2) & ", " & n.Address(False, False)

        Else

            u = Range(col & Rows.Count).End(xlUp).Row + 1

            Cells(u, c) = n.Value

            Cells(u, c + 1) = 1

            Cells(u, c + 2) = n.Address(False, False)

        End If

    Next

    For i = u To 1 Step -1

        If Cells(i, c + 1) = 1 Then

            Range(Cells(i, c), Cells(i, c + 2)).Delete Shift:=xlUp

        End If

    Next

    '

    With ActiveSheet.Sort

        .SortFields.Clear

        .SortFields.Add Key:=Range(Cells(1, c + 1), Cells(u, c + 1)), _

            SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal

        .SetRange Range(Cells(1, c), Cells(u, c + 2))

        .Header = xlGuess

        .MatchCase = False

        .Orientation = xlTopToBottom

        .SortMethod = xlPinYin

        .Apply

    End With

    Application.ScreenUpdating = True

    MsgBox "Fin"

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Buenos días Dragon:

Prueba el siguiente código, le agregué un Dictionary para hacerlo más rápido.

VBA Code:
Sub Repetidos_v2()
  Dim dic As Object
  Dim a As Variant, b As Variant, ky As Variant
  Dim col As String, d As String, numS As String, addS As String
  Dim i%, j%, y%, k%
  
  Application.ScreenUpdating = False
  Application.StatusBar = False
  
  col = "TG"
  a = Range("A1:SZ217").Value2
  With Range(col & 1)
    .Resize(1, 3).EntireColumn.ClearContents
    .EntireColumn.NumberFormat = "@"
  End With
  
  Set dic = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(a, 2)
      numS = a(i, j)
      addS = Cells(i, j).Address(0, 0)
      If numS <> "" Then
        If Not dic.exists(numS) Then
          dic(numS) = 1 & "|" & addS
        Else
          y = Split(dic(numS), "|")(0)
          d = Split(dic(numS), "|")(1)
          y = y + 1
          dic(numS) = y & "|" & d & ", " & addS
        End If
      End If
    Next
  Next
  
  ReDim b(1 To dic.Count, 1 To 3)
  For Each ky In dic.keys
    y = Split(dic(ky), "|")(0)
    If y > 1 Then
      k = k + 1
      b(k, 1) = ky
      b(k, 2) = Split(dic(ky), "|")(0)
      b(k, 3) = Split(dic(ky), "|")(1)
    End If
  Next
    
  With Range(col & 1).Resize(dic.Count, 3)
    .Value = b
    .Sort Range(col & 1), xlAscending, Header:=xlNo
  End With
  
  Application.ScreenUpdating = False
End Sub

Prueba y nos comentas.
🤗
 
Upvote 0
Maestro Dante el código funciona, pero no me está arrojando los números que más se repiten en una columna ni la cantidad de veces en la otra columna
 
Upvote 0
El código pone 3 columnas: el número, la cantidad de veces que se repite y las celdas donde se encuentra el número:
1723472691612.png


Tal vez lo que necesitas es ordenas los datos por la cantidad de repetido de mayor a menor.
Solamente cambia esta línea:
VBA Code:
.Sort Range(col & 1), xlAscending, Header:=xlNo

Por esta:
VBA Code:
.Sort Cells(1, Columns(col).Column + 1), xlDescending, Header:=xlNo

😇
 
Upvote 0
Solution
Listo Dante me arroja el número y la cantidad de repeticiones que tiene ese número en ese rango , pero ahora el problema es que me da la ubicación errónea de la celda donde está ese número , me está arrojando una celda más atrás de su ubicación ya lo probé con el buscador de excel y si el número está en la celda G3 el código me arroja que esta en F3 podremos solucionarlo graciss
 
Upvote 0
Listo Dante era mi rango de datos el problema pero ya lo he solucionado gracias a su comentario en la anterior pregunta
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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