我知道这是因为“或”,但我希望有人可以解释为什么它不起作用。
If (Sheet.Name <> "Dep 1" Or "Test") Then
^
Sub DeleteSheet()
Dim Sheet As Worksheet
Application.DisplayAlerts = False
For Each Sheet In ActiveWorkbook.Sheets
If (Sheet.Name <> "Dep 1" Or "Test") Then
Sheet.Delete
End If
Next Sheet
Application.DisplayAlerts = False
End Sub
答案 0 :(得分:1)
我对这篇文章的“两分钱”:
尝试使用有意义的变量名称,但 NOT 过于接近Excel的保存单词。 Dim Sheet
与Sheets
相似约85%,这是Object
的一种类型,我在这里看到很多帖子,人们cell
与Cells
混淆并得到运行时错误。
我已将If
与Select Case .Name
切换为Worksheets
,如果将来您添加了更多Worksheets
或想要对其他Option Explicit
Sub DeleteSheet()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ActiveWorkbook.Sheets
With ws
Select Case .Name
Case "Dep 1", "Test"
' do nothing for now
Case Else
.Delete
End Select
End With
Next ws
Application.DisplayAlerts = True ' <-- restore setting
End Sub
执行其他操作,修改代码会更容易。
<强>代码强>
//Variable size of Array program to print Array elements
#include <iostream>
using namespace std;
int main()
{
cout << "Enter Array size x:" << endl;
int x = 0;
cin >> x;
int *ptrArray = new int[x];
//Inittialise Array
for (int i = 0; i < x; i++)
{
ptrArray[i] = i + 1;
}
//Print Array elemts
cout << "Array elements:";
for (int i = 0; i < x; i++)
{
cout << ptrArray[i] << endl;
}
delete[] ptrArray;
return 0;
}