我正在處理此代碼並不斷收到錯誤。它會全部編譯,但我一直收到運行時錯誤。我試圖比較兩張不同的紙張,然後突出顯示不匹配的單元格。我不確定錯誤發生的位置。任何幫助將不勝感激。Excel運行時錯誤'13':類型不匹配代碼問題
Sub David()
Dim Initial_PO As Double
Dim Initial_Firmed As Range
Dim Initial_Agreed_Ship As Range
Dim Initial_Actual_Ship As Range
Dim Initial_Agreed_Delivery As Range
Dim Initial_Actual_Delivery As Range
Dim Initial_Requested_Quantity As Range
Dim Initial_Actual_Quantity As Range
Dim Initial_QMetric As Double
Dim Initial_DMetric As Double
Dim Final_PO As Double
Dim Final_Firmed As Range
Dim Final_Agreed_Ship As Range
Dim Final_Actual_Ship As Range
Dim Final_Agreed_Delivery As Range
Dim Final_Actual_Delivery As Range
Dim Final_Requested_Quantity As Range
Dim Final_Actual_Quantity As Range
Dim Final_QMetric As Double
Dim Final_DMetric As Double
Dim Initial_Agreed_Delivery_Date As Date
Dim Final_Agreed_Delivery_Date As Date
Dim Initial_Actual_Delivery_Date As Date
Dim Final_Actual_Delivery_Date As Date
Dim Today As Date
'Dim NumRow As Integer
Dim i As Long
Dim BulkLT As Double
For i = 2 To 3000
Sheets("Initial").Select
Set Initial_PO = Cells(i, 7)
Set Initial_Firmed = Cells(i, 9)
Set Initial_Agreed_Ship = Cells(i, 10)
Set Initial_Actual_Ship = Cells(i, 11)
Set Initial_Agreed_Delivery = Cells(i, 13)
Set Initial_Actual_Delivery = Cells(i, 14)
Set Initial_Requested_Quantity = Cells(i, 15)
Set Initial_Actual_Quantity = Cells(i, 16)
Sheets("Final").Select
Set Final_PO = Cells(i, 7)
Set Final_Firmed = Cells(i, 9)
Set Final_Agreed_Ship = Cells(i, 10)
Set Final_Actual_Ship = Cells(i, 11)
Set Final_Agreed_Delivery = Cells(i, 13)
Set Final_Actual_Delivery = Cells(i, 14)
Set Final_Requested_Quantity = Cells(i, 15)
Set Final_Actual_Quantity = Cells(i, 15)
'Initial Highlighting
If (Initial_PO = Final_PO) Then
If Not (Initial_Firmed = Final_Firmed) Then
Initial_Firmed.Interior.Color = RGB(225, 225, 0) And Final_Firmed.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Agreed_Ship = Final_Agreed_Ship) Then
Initial_Agreed_Ship.Interior.Color = RGB(225, 225, 0) And Final_Agreed_Ship.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Actual_Ship = Final_Actual_Ship) Then
Initial_Actual_Ship.Interior.Color = RGB(225, 225, 0) And Final_Actual_Ship.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Agreed_Delivery = Final_Agreed_Delivery) Then
Initial_Agreed_Delivery.Interior.Color = RGB(225, 225, 0) And Final_Agreed_Delivery.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Actual_Delivery = Final_Actual_Delivery) Then
Initial_Actual_Delivery.Interior.Color = RGB(225, 225, 0) And Final_Actual_Delivery.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Requested_Quantity = Final_Requested_Quantity) Then
Initial_Requested_Quantity.Interior.Color = RGB(225, 225, 0) And Final_Requested_Quantity.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Actual_Quantity = Final_Actual_Quantity) Then
Initial_Actual_Quantity.Interior.Color = RGB(225, 225, 0) And Final_Actual_Quantity.Interior.Color = RGB(225, 225, 0)
End If
If Not (Initial_Requested_Quantity = Initial_Actual_Quantity) Then
Initial_Requested_Quantity.Interior.Color = RGB(225, 225, 0) And Initial_Actual_Quantity.Interior.Color = RGB(225, 225, 0)
End If
If Not (Final_Requested_Quantity = Final_Actual_Quantity) Then
Final_Requested_Quantity.Interior.Color = RGB(225, 225, 0) And Final_Actual_Quantity.Interior.Color = RGB(225, 225, 0)
End If
'Metric Calculation
Initial_QMetric = ((Initial_Actual_Quantity/Initial_Requested_Quantity) * 100)
Final_QMetric = ((Final_Actual_Quantity/Final_Requested_Quantity) * 100)
Sheets("Initial").Select
Cells(i, 27) = Initial_QMetric
Sheets("Final").Select
Cells(i, 27) = Final_QMetric
If (Initial_QMetric < 90 Or Initial_QMetric > 110) Then
Sheets("Initial").Select
Cells(i, 27).Interior.Color = RGB(225, 225, 0)
End If
If (Final_QMetric < 90 Or Final_QMetric > 110) Then
Sheets("Final").Select
Cells(i, 27).Interior.Color = RGB(225, 225, 0)
End If
Initial_DMetric = DateDiff("d", Initial_Agreed_Delivery_Date, Initial_Actual_Delivery_Date)
Final_DMetric = DateDiff("d", Final_Agreed_Delivery_Date, Final_Actual_Delivery_Date)
Sheets("Initial").Select
Cells(i, 28) = Initial_DMetric
Sheets("Final").Select
Cells(i, 28) = Final_DMetric
If (Initial_DMetric > 5 Or Initial_DMetric < (-5)) Then
Sheets("Initial").Select
Cells(i, 28).Interior.Color = RGB(225, 225, 0)
End If
If (Final_DMetric > 5 Or Final_DMetric < (-5)) Then
Sheets("Final").Select
Cells(i, 28).Interior.Color = RGB(225, 225, 0)
End If
'Bulk Lead time
BulkLT = DateDiff("d", Today, Final_Agreed_Ship)
If IsEmpty(Final_Firmed) = True Then
If (BulkLT < 90) Then
Final_Firmed.Interior.Color = RGB(225, 225, 0)
End If
End If
Else: MsgBox ("PO Numbers in row" & i & "do not match")
End If
Next i
End
End Sub
哪條線路產生錯誤?如果你用F8執行它,它會工作嗎? – Tim
請向我們展示錯誤 –
我發現您的代碼很少出現問題。首先,如果您使用Dim作爲範圍,則使用「Set」命令正確分配它。例如'Set Initial_Firmed = Cells(i,9)'。 –