2017-06-15 67 views
0

我在这行得到错误我的VBA宏:VBA:“定义或对象定义的错误”在if和then功能

If Sheets("Check Foil").Cells(y, 15) = Sheets("Data").Cells(x, 19) And Sheets("Check Foil").Cells(y, 24).Value <> "0" Then 

这是我的代码:

x = 1 
    y = 16 
    Do 
     If Sheets("Check Foil").Cells(y, 15) = Sheets("Data").Cells(x, 19) And Sheets("Check Foil").Cells(y, 24).Value <> "0" Then 
      Sheets("Data").Cells(x, 21).Copy 
      Sheets("Check Foil").Cells(y, 21).PasteSpecial xlPasteAll 
      Sheets("Check Foil").Cells(y, 22) = "Anode Foil not scan completely" 
      With Sheets("Check Foil").Range(Sheets("Check Foil").Cells(y, 21), Sheets("Check Foil").Cells(y, 24)).Borders 
        .LineStyle = xlContinuous 
        .Weight = xlThin 
        .ColorIndex = xlAutomatic 
      End With 
      y = y + 1 
     Else 
      Sheets("Check Foil").Cells(y, 22) = "Foil Info Not Found" 
     End If 
     x = x + 1 
    Loop Until Sheets("Check Foil").Cells(y, 15) = "" 

    Sheets("Check Foil").Range("M16:X1016").VerticalAlignment = xlCenter 
    Sheets("Check Foil").Range("M:M").HorizontalAlignment = xlCenter 
    Sheets("Check Foil").Range("N16:O1016").HorizontalAlignment = xlLeft 
    Sheets("Check Foil").Range("P16:V1016").HorizontalAlignment = xlRight 

    y = 16 
    Do 
     If Sheets("Check Foil").Cells(y, 24) <= Application.WorksheetFunction.Sum(Sheets("Check Foil").Cells(y, 23) - (Sheets("Check Foil").Cells(y, 23) * 0.1)) And Sheets("Check Foil").Cells(y, 24).Value <> "0" Then 
      Sheets("Check Foil").Range(Sheets("Check Foil").Cells(y, 13), Sheets("Check Foil").Cells(y, 24)).Interior.ColorIndex = 6 
     End If 
     y = y + 1 
    Loop Until Sheets("Check Foil").Cells(y, 14) = "" 

    Worksheets("Check Foil").Activate 
    ActiveSheet.Range("A1").Select 

    MsgBox "Program Complete Run" 

End Sub 
+0

这条线可以得到同样的错误:如果表( “检查箔”)细胞(Y,24)<= Application.WorksheetFunction细胞(y,23) - (细胞膜(“检查箔”)细胞(y,23)* 0.1))和片(“检查箔”)细胞(y, 24).Value <>“0”Then – Falhuddin

+0

'x','y','Sheets(“Check Foil”)的值是什么?单元格(y,15)','表格(数据) (x,19)'''Sheets(“Check Foil”)。细胞(y,24)'代码崩溃时? – YowE3K

回答

0

您的实际问题,您在第一次比较中缺少“.value”。 这样:

If (Sheets("Check Foil").Cells(y, 15).Value = Sheets("Data").Cells(x, 19).Value) And (Sheets("Check Foil").Cells(y, 24).Value <> "0") Then 
      Sheets("Data").Cells(x, 21).Copy 

但这可以改进:

Dim wb As Workbook 
Dim ws_foil As Worksheet 
Dim ws_data As Worksheet 

Set wb = ThisWorkbook 
Set ws_foil = wb.Sheets("Check Foil") 
Set ws_foil = wb.Sheets("Data") 

foil_data = ws_foil.Cells(y, 15).Value 
data_data = ws_data.Cells(x, 19).Value 

foil_data1 = ws_foil.Cells(y, 24).Value 

If ((foil_data = data_data) And (foil_data1 <> 0)) Then 
    ws_data.Cells(x, 21).Copy 
End If 
+0

'.Value'是(间接)Range对象的默认属性。虽然我个人比较喜欢总是使用它,但它**会在没有它的情况下工作。 – YowE3K

相关问题