感谢所有帮助和评论。我已经编写了这段代码来使用" Automatisierung"在其他工作簿中,过滤所有关键字(很多),然后执行这些操作。 当我一步一步地执行它并且不会抛出错误时,代码本身运行正常。在启动代码之后3或4秒仍然完全崩溃。 理论: 过滤器太多了 变量/定义太多
我感谢所有评论和见解。
说明:
Private Sub CommandButton1_Click()
Dim c As Range, i As Long
Dim d As Range, h As Long
Dim e As Range, j As Long
Dim f As Range, k As Long
Dim g As Range, l As Long
Dim count As Integer, lastrow As Long
Dim nombre As String, Planta As String
Dim Temporada As String, Mes As String
Dim Kilo As Long, rnge As Range
Dim m As Range, o As Long
Dim rnge2 As Range, Kilo2 As Long
count = 1
Planta = ActiveWorkbook.Worksheets("Informacion").Range("B2").Text
Mes = ActiveWorkbook.Worksheets("Informacion").Range("D2").Text
Temporada = ActiveWorkbook.Worksheets("Informacion").Range("C2").Text
nombre = ActiveWorkbook.Worksheets("Informacion").Range("A2").Text
i = WorksheetFunction.CountA(ActiveSheet.Range("E2:E" & Range("E2").End(xlDown).Row))
h = WorksheetFunction.CountA(ActiveSheet.Range("F2:F" & Range("F2").End(xlDown).Row))
j = WorksheetFunction.CountA(ActiveSheet.Range("G2:G" & Range("G2").End(xlDown).Row))
k = WorksheetFunction.CountA(ActiveSheet.Range("H2:H" & Range("H2").End(xlDown).Row))
l = WorksheetFunction.CountA(ActiveSheet.Range("I2:I" & Range("I2").End(xlDown).Row))
o = WorksheetFunction.CountA(ActiveSheet.Range("J2:J20"))
for循环和要执行的操作:
Windows(nombre).Activate
lastrow = ActiveSheet.Cells(Rows.count, "Y").End(xlUp).Row
Application.ScreenUpdating = False
Application.EnableEvents = False
Sheets("Hoja1").Activate
For Each c In Windows("Automatisierung").ActiveSheet.Range("E2:E" & i + 1)
Sheets("Hoja1").Range("A2:Y" & lastrow).AutoFilter field:=8, Criteria1:="=" & c.Value
For Each d In Windows("Automatisierung").ActiveSheet.Range("F2:F" & h + 1)
Sheets("Hoja1").Range("A2:Y" & lastrow).AutoFilter field:=7, Criteria1:="=" & d.Value
For Each e In Windows("Automatisierung").ActiveSheet.Range("G2:G" & j + 1)
Sheets("Hoja1").Range("A2:Y" & lastrow).AutoFilter field:=3, Criteria1:="=" & e.Value
For Each f In Windows("Automatisierung").ActiveSheet.Range("H2:H" & k + 1)
Sheets("Hoja1").Range("A2:Y" & lastrow).AutoFilter field:=16, Criteria1:="=" & f.Value
For Each g In Windows("Automatisierung").ActiveSheet.Range("I2:I" & l + 1)
Sheets("Hoja1").Range("A2:Y" & lastrow).AutoFilter field:=18, Criteria1:="=" & g.Value
Set rnge = ActiveSheet.Range("T3:T" & Range("T3").End(xlDown).Row).Rows.SpecialCells(xlCellTypeVisible)
Kilo = Application.WorksheetFunction.Sum(rnge)
If Kilo <> 0 Then
Sheets("Tabelle1").Select
count = count + 1
ActiveSheet.Range("B" & count).Value = d.Value
ActiveSheet.Range("E" & count).Value = e.Value + " " + f.Value
ActiveSheet.Range("F" & count).Value = g.Value
ActiveSheet.Range("G" & count).Value = -Kilo
For Each m In Windows("Automatisierung").ActiveSheet.Range("J2:J" & o + 1)
Sheets("Hoja1").Range("A2:Y" & lastrow).AutoFilter field:=24, Criteria1:="=" & m.Value
Set rnge2 = ActiveSheet.Range("Y3:Y" & Range("Y3").End(xlDown).Row).Rows.SpecialCells(xlCellTypeVisible)
Kilo2 = Application.WorksheetFunction.Sum(rnge2)
If Kilo2 <> 0 Then
Sheets("Tabelle1").Select
ActiveSheet.Range("H" & count).Value = m.Value
ActiveSheet.Range("I" & count).Value = Kilo2
count = count + 1
End If
Next m
End If
Next g
Next f
Next e
Next d
Next c
结束:
ActiveSheet.AutoFilterMode = False
Sheets("Tabelle1").Select
ActiveSheet.Range("A2:A" & count).Value = Temporada
ActiveSheet.Range("C2:C" & count).Value = Planta
ActiveSheet.Range("D2:D" & count).Value = Mes
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub