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