0
我想填写一张表格,其中包含我有多少个寄存器,具有同一天,每周和每小时,并且该计数除以我可以找到同一周的年数。慢速查找和标识
我已经在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
如果代码正常工作,并且您所需要的只是帮助改进代码,那么这对于此网站来说太宽泛了,应该位于https://codereview.stackexchange.com/ –
1.您可以先设置所有范围在您的For .. Next循环之外。似乎没有必要为循环的每次迭代不断地设置和重置它们。 2.'application.match'比vlookup快。 – Jeeped