我有一个if语句,用于检查工作表是否是我想要在for循环中使用的工作表。该语句有多个条件可以满足该语句。当我运行它时,它给了我一个错误
运行时错误'438':
Object不支持此属性或方法
以下是相关代码。
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语句是给我错误的语句。
答案 0 :(得分:0)
好的,所以我在@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)")
至少我很确定这应该有效。