好的打击是我到这一点的代码。我遇到的问题与以下 if 有关。如果我试图选择的工作表不在工作簿中,我该怎么办?我想跳过 if 并继续前进,不要出错。
If k = 1 Then
Sheets(Key).Select
IF
End If
Start of full code
Set xlWorkbook = Workbooks.Open(FILEPATH)
Y = xlWorkbook.Name
Set xlWorkbook = Workbooks.Open(Import)
xy = xlWorkbook.Name
Workbooks(Y).Activate
Sheets("Sheet1").Select
ActiveSheet.Range("$A$1:$K$490").AutoFilter Field:=3, Criteria1:="1"
CP = Worksheets("Sheet1").Cells(Rows.count, "B").End(xlUp).Row - 1
For COUNTER = 1 To CP
COMBO = ActiveSheet.Range("A" & rowC).Value
k = ActiveSheet.Range("K" & rowC).Value
KEY1 = ActiveSheet.Range("B" & rowC).Value
If k > 1 Then
rowC = rowC + 1
KEY2 = ActiveSheet.Range("B" & rowC).Value
End If
Workbooks(xy).Activate
If k = 1 Then
Sheets(Key).Select
IF
End If
rowC = rowC + 1
Next