我想知道为什么这段代码无效?

时间:2017-03-31 22:21:31

标签: vba

我知道这是因为“或”,但我希望有人可以解释为什么它不起作用。

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

1 个答案:

答案 0 :(得分:1)

我对这篇文章的“两分钱”:

尝试使用有意义的变量名称,但 NOT 过于接近Excel的保存单词。 Dim SheetSheets相似约85%,这是Object的一种类型,我在这里看到很多帖子,人们cellCells混淆并得到运行时错误。

我已将IfSelect 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;
}
相关问题