慢速查找和计数

Slow vlookup and countifs

我正在尝试用我有多少寄存器的计数来填充 table,具有同一天、同一周和同一小时,并将该计数除以我可以找到的年数同一周。

我已经在 VBA 中完成了这段代码,但它真的很慢,所以如果你能帮助我改进这个解决方案,我将非常感激。

    Sub formulacion()
    Dim a As Integer
    Dim b As Integer
    Dim years As Integer
    Dim rango_semana As Range
    Dim rango_dia As Range
    Dim rango_hora As Range
    Dim rango_sede As Range
    Dim rango_busqueda As Range



    a = 2
    For a = 2 To 319
        If Sheets("Dinamicos").Cells(5, a) <> "" Then
        b = 6
            For b = 6 To 20

            semana = Sheets("Dinamicos").Cells(3, a)
            dia = Sheets("Dinamicos").Cells(5, a)
            hora = Sheets("Dinamicos").Cells(b, 1)
            sede = Sheets("Dinamicos").Cells(4, 1)
            LastRow = Sheets("Base").Cells(Sheets("Base").Rows.Count, "A").End(xlUp).Row
            Set rango_semana = Sheets("Base").Range("AK2:AK" & LastRow)
            Set rango_dia = Sheets("Base").Range("AG2:AG" & LastRow)
            Set rango_hora = Sheets("Base").Range("AJ2:AJ" & LastRow)
            Set rango_sede = Sheets("Base").Range("J2:J" & LastRow)
            Set rango_busqueda = Sheets("Base").Range("AK2:AN" & LastRow)

            lookupvalue = Application.VLookup(semana, rango_busqueda, 4, False)
               If IsError(lookupvalue) Then
               years = 1
              'Si lo encuentra lo devuelve
               Else
               years = lookupvalue
               End If

            Sheets("Dinamicos").Cells(b, a) = (WorksheetFunction.CountIfs(rango_semana, semana, rango_dia, dia, rango_hora, hora, rango_sede, sede)) / years

            Next b
        End If
        b = 6
  Next a


  End Sub

一些 var 赋值在嵌套的 For ... Next 循环迭代中发生变化;其他人没有。不要继续重新分配不变的变量。

Application.MatchApplication.Vlookup.

在循环和嵌套循环中使用它们之前,您不必设置和重置 ab 中的值。它们在进入循环时被赋予起始值。

lastRow = Worksheets("Base").Cells(Worksheets("Base").Rows.Count, "A").End(xlUp).Row
Set rango_semana = Worksheets("Base").Range("AK2:AK" & lastRow)
Set rango_dia = Worksheets("Base").Range("AG2:AG" & lastRow)
Set rango_hora = Worksheets("Base").Range("AJ2:AJ" & lastRow)
Set rango_sede = Worksheets("Base").Range("J2:J" & lastRow)
Set rango_busqueda = Worksheets("Base").Range("AK2:AN" & lastRow)
sede = Worksheets("Dinamicos").Cells(4, 1)

For a = 2 To 319
    If Worksheets("Dinamicos").Cells(5, a) <> "" Then
        semana = Worksheets("Dinamicos").Cells(3, a)
        dia = Worksheets("Dinamicos").Cells(5, a)

        For b = 6 To 20

            hora = Sheets("Dinamicos").Cells(b, 1)

            lookupvalue = Application.Match(semana, rango_busqueda.Columns(1), False)
            If IsError(lookupvalue) Then
               years = 1
              'Si lo encuentra lo devuelve
            Else
               years = rango_busqueda.Cells(lookupvalue, 4).Value2
            End If

            Worksheets("Dinamicos").Cells(b, a) = (WorksheetFunction.CountIfs(rango_semana, semana, rango_dia, dia, rango_hora, hora, rango_sede, sede)) / years

        Next b
    End If
Next a

最后,请记住 Sheets 与 Worksheets 不同。