我正在尝试填写一张表,其中包含我拥有的寄存器数量,具有相同的日期,星期和小时,并且该计数除以我可以找到同一周的年数。
我已经在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
答案 0 :(得分:0)
一些var赋值在嵌套的For ... Next循环迭代中发生变化;别人不这样做。不要重新分配不变的变量。
Application.Match
比Application.Vlookup
快。
在循环和嵌套循环中使用它们之前,您不必设置和重置a
和b
中的值。在进入循环时为它们分配起始值。
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不同。