Я пытаюсь сделать список тянуть из данных первенствовать и, наконец, написал этот код:Требуется объект ошибка ан Excel
Sub subPartsQueue()
Dim part As String
Dim numParts As Integer
Dim subPart As String
Dim numSubparts As Integer
Dim mach As String
Dim state As Integer
Dim startH As Date
Dim endH As Date
Dim difH As Integer
Dim difM As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
z = 2
numParts = ThisWorbook.Sheets("partsQueue").Range("G1")
numSubparts = ThisWorbook.Sheets("relations").Range("S2")
ThisWorkbook.Sheets("resume").Range("A1") = "Subpart"
ThisWorkbook.Sheets("resume").Range("B1") = "Start time"
ThisWorkbook.Sheets("resume").Range("C1") = "End time"
ThisWorkbook.Sheets("resume").Range("D1") = "Nº of Reps"
ThisWorkbook.Sheets("resume").Range("E1") = "Acumulated loot"
For x = 2 To numParts
part = ThisWorbook.Sheets("partsQueue").Range("A" & x)
startH = ThisWorbook.Sheets("partsQueue").Range("B" & x)
For y = 2 To numSubparts
If ThisWorbook.Sheets("relations").Range("A" & y) = part Then
mach = ThisWorbook.Sheets("relations").Range("D" & y)
If part = ThisWorkbook.Sheets("machines").Range(mach & "3") Then
endH = DateAdd("n", ThisWorbook.Sheets("relations").Range("I" & y), startH)
Else
endH = DateAdd("n", ThisWorbook.Sheets("relations").Range("J" & y), startH)
End If
If freeMachine(mach, startH) = True Then
ThisWorkbook.Sheets("machines").Range(mach & "1") = 1
ThisWorkbook.Sheets("machines").Range(mach & "2") = endH
ThisWorkbook.Sheets("machines").Range(mach & "3") = ThisWorbook.Sheets("relations").Range("B" & y)
ThisWorkbook.Sheets("resume").Range("A" & z) = ThisWorbook.Sheets("relations").Range("B" & y)
ThisWorkbook.Sheets("resume").Range("B" & z) = startH
ThisWorkbook.Sheets("resume").Range("C" & z) = endH
ThisWorkbook.Sheets("resume").Range("D" & z) = numReps(ThisWorkbook.Sheets("resume").Range("A" & z), z) + 1
ThisWorkbook.Sheets("resume").Range("E" & z) = acumulatedLoot(ThisWorkbook.Sheets("resume").Range("A" & z), z, ThisWorbook.Sheets("relations").Range("H" & y), ThisWorbook.Sheets("relations").Range("K" & y), ThisWorbook.Sheets("relations").Range("L" & y))
If sameMold(ThisWorkbook.Sheets("resume").Range("A" & z), ThisWorbook.Sheets("relations").Range("C" & y)) = True Then
z = z + 1
ThisWorkbook.Sheets("resume").Range("A" & z) = otherSubpartMold(ThisWorkbook.Sheets("resume").Range("A" & z - 1), ThisWorbook.Sheets("relations").Range("C" & y))
ThisWorkbook.Sheets("resume").Range("B" & z) = ThisWorkbook.Sheets("resume").Range("B" & z - 1)
ThisWorkbook.Sheets("resume").Range("C" & z) = ThisWorkbook.Sheets("resume").Range("C" & z - 1)
ThisWorkbook.Sheets("resume").Range("D" & z) = ThisWorkbook.Sheets("resume").Range("D" & z - 1)
ThisWorkbook.Sheets("resume").Range("E" & z) = ThisWorkbook.Sheets("resume").Range("E" & z - 1)
z = z + 1
Else
z = z + 1
End If
Else
Resume Next
End If
End If
Next y
Next x
End Sub
Function freeMachine(machine As String, startH As Date) As Boolean
Dim difH As Integer
Dim difM As Integer
If ThisWorkbook.Sheets("machines").Range(machine & "1") = 0 Then
freeMachine = True
Else
difH = DateDiff("h", startH, ThisWorkbook.Sheets("machines").Range(machine & "2"))
difM = DateDiff("n", startH, ThisWorkbook.Sheets("machines").Range(machine & "2"))
If difH = 0 Then
If difM = 0 Then
freeMachine = True
Else
freeMachine = False
End If
Else
freeMachine = False
End If
End If
End Function
Function numReps(subPart As String, numEntries As Integer) As Integer
x As Integer
y As Integer
y = 0
For x = numEntries To 2 Step -1
If ThisWorkbook.Sheets("resume").Range("A" & x) = subPart Then
y = y + 1
Else
y = 0
End If
Next x
numReps = y
End Function
Function acumulatedLoot(subPart As String, numEntries As Integer, loot As Integer, units As Integer, uses As Integer) As Integer
x As Integer
total As Integer
total = 0
For x = numEntries To 2 Step -1
If ThisWorkbook.Sheets("resume").Range("A" & x) = subPart Then
total = ThisWorkbook.Sheets("resume").Range("E" & x) + (loot * units * uses)
GoTo out
Else
total = loot * units * uses
End If
Next x
out:
acumulatedLoot = total
End Function
Function sameMold(subPart As String, mold As String) As Boolean
x As Integer
numSubparts As Integer
numSubparts = ThisWorbook.Sheets("relations").Range("S2")
For x = 2 To numSubparts
If mold = ThisWorbook.Sheets("relations").Range("C" & x) Then
If subPart = ThisWorbook.Sheets("relations").Range("B" & x) Then
Resume Next
Else
GoTo out
End If
Else
Resume Next
End If
Next x
out:
sameMold = True
End Function
Function otherSubpartMold(subPart As String, mold As String) As String
x As Integer
numSubparts As Integer
otherSubpartName As String
numSubparts = ThisWorbook.Sheets("relations").Range("S2")
For x = 2 To numSubparts
If mold = ThisWorbook.Sheets("relations").Range("C" & x) Then
If subPart = ThisWorbook.Sheets("relations").Range("B" & x) Then
Resume Next
Else
otherSubpartName = ThisWorbook.Sheets("relations").Range("B" & x)
GoTo out
End If
Else
Resume Next
End If
Next x
out:
otherSubpartMold = otherSubpartName
End Function
Но когда я запускаю его послать мне «Объект не требуется» ошибка в этой строке:
numParts = ThisWorbook.Sheets("partsQueue").Range("G1")
Где находится формула CountA, содержащаяся в A Колонка partsQueue.
Я попытался использовать Set перед предложением и активировать лист, но та же ошибка появилась. Читая код, я не нашел ошибку или чего-то не хватает для меня.
Это была смущающая ошибка. Спасибо за помощь, я попробую сейчас. – Tilan04
@ Tilan04: Не беспокойтесь, опечатки часты! Но с помощью «Option Explicit» вы должны избегать их гораздо легче! ;) Они будут указаны при компиляции, а не во время выполнения! ;) – R3uK