Excel 使用或'的带有多个条件的If语句;s方法错误

Excel 使用或'的带有多个条件的If语句;s方法错误,excel,vba,Excel,Vba,我有一个if语句,用于检查该表是否是我要在for循环中使用的表。该语句具有多个可以满足该语句的条件。当我运行它时,它会给我一个错误 运行时错误“438”: 对象不支持此属性或方法 这是有问题的代码 Application.Sheets.Add(After:=ActiveWorkbook.Sheets _ (ActiveWorkbook.Sheets.count)).Name = "Data Sheet" Dim WSds As Worksheet Dim WScolumbo As Worksh

我有一个if语句,用于检查该表是否是我要在for循环中使用的表。该语句具有多个可以满足该语句的条件。当我运行它时,它会给我一个错误

运行时错误“438”:

对象不支持此属性或方法

这是有问题的代码

Application.Sheets.Add(After:=ActiveWorkbook.Sheets _
(ActiveWorkbook.Sheets.count)).Name = "Data Sheet"

Dim WSds As Worksheet
Dim WScolumbo As Worksheet
Dim WSscm As Worksheet
Dim WSselexx As Worksheet
Dim WSsashclamp As Worksheet
Dim WSframeclamp As Worksheet
Dim i As Long
Dim k As Long
Dim countrows As Long
Dim countcolumns As Long
Dim Sheetscount As Long
Dim WSdsindexx As Long


Set WSds = Worksheets("Data Sheet")
Set WScolumbo = Worksheets("Columbo (sash)")
Set WSscm = Worksheets("SCM Pratix Z2 (Sash)")
Set WSselexx = Worksheets("Selex PAL (Sash)")
Set WSsashclamp = Worksheets("Sash Clamp")
Set WSframeclamp = Worksheets("Frame Clamp")

countrows = ActiveSheet.Range("A" & Rows.count).End(xlUp).Row
countcolumns = ActiveSheet.Cells(Columns.count, 1).End(xlToRight).Column
WSdsindexx = 1

For Sheetcount = 1 To ActiveWorkbook.Sheets.count
    If ((Worksheets(Sheetcount) = WScolumbo) Or _
    (Worksheets(Sheetcount) = WSscm) Or _
    (Worksheets(Sheetcount) = WSselexx) Or _
    (Worksheets(Sheetcount) = WSsashclamp) Or _
    (Worksheets(Sheetcount) = WSframeclamp)) Then
        For i = 2 To countcolumns
            For k = 1 To countrows
                If Worksheets(Sheetcount).Cells(k, i).Value = "Machine Cycle" Then
                    WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Swap Out Parts and Adjust Size" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Unload/Load" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Take Parts Out and Put New In" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Top Clamp" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Bottom Clamp" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Load Top" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Load Bottom" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Unload Top" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                ElseIf Worksheets(Sheetcount).Cells(k, i).Value = "Unload Bottom" Then WSds.Cells(WSdsindexx, 3).Value = Worksheets(Sheetcount).Cells(k, i).Offset(1, 1).Value
                    WSds.Cells(WSdsindexx, 2) = Worksheets(Sheetcount).Cells(k, i).Value
                    WSds.Cells(WSdsindexx, 1) = Worksheets(Sheetcount).Name
                Else: MsgBox "No Matches Criteria"
                End If
            Next k
        Next i
    Else: MsgBox "No Matches Sheet"
    End If
Next Sheetcount

第一个if语句就是给我错误的语句。

好的,我在@ScottCraner的帮助下找到了答案

为了比较图纸,必须使用图纸名称或其索引号

就我而言,我用的是名字,所以

If ((Worksheets(Sheetcount).Name = WScolumbo.Name) Or _
    (Worksheets(Sheetcount).Name = WSscm.Name) Or _
    (Worksheets(Sheetcount).Name = WSselexx.Name) Or _
    (Worksheets(Sheetcount).Name = WSsashclamp.Name) Or _
    (Worksheets(Sheetcount).Name = WSframeclamp.Name)) Then
为我工作

在索引编号的情况下

你必须

Set WScolumbo = Worksheets(1)
而不是

Set Wscolumbo = Worksheets("Columbo (sash)")

至少我很确定这应该行得通

尝试比较工作表的名称,而不是实际工作表<代码>(工作表(Sheetcount).Name=WScolumbo.Name)等等。@ScottCraner好的,我想可能是语法错误,或者工作表名称无法识别。我会很快玩的。@ScottCraner你说得对。你想提出答案吗?否则我会回答的。请随意回答。我很高兴能帮上忙。