检查excel 2010 vba

时间:2015-12-04 17:40:43

标签: excel-vba vba excel

我让用户输入将用于创建目录的一些信息MyBarcode and MyScan。如果该目录存在,我想显示一条消息,指示该目录并返回用户可以输入数据的步骤。 vba在下面并且似乎起作用,除了我需要一些帮助的目录检查。我希望这是一个好的开始。谢谢你:)。

Private Sub CommandButton3_Click()

Dim MyBarCode   As String      ' Enter Barcode
Dim MyScan      As String      ' Enter ScanDate
Dim MyDirectory As String

MyBarCode = Application.InputBox("Please enter the last 5 digits of the barcode", "Bar Code", Type:=2)

If MyBarCode = "False" Then Exit Sub   'user canceled
Do
    MyScan = Application.InputBox("Please enter scan date", "Scan Date", Date - 1, Type:=2)
    If MyScan = "False" Then Exit Sub   'user canceled
    If IsDate(MyScan) Then Exit Do
    MsgBox "Please enter a valid date format. ", vbExclamation, "Invalid Date Entry"
Loop

'Create nexus directory and folder check
MyDirectory = "N:\1_DATA\MicroArray\NexusData\" & "2571683" & MyBarCode & "_" & Format(CDate(MyScan), "m-d-yyyy") & "\"
If MyDirectory("MyDirectory") Then
    MsgBox "Folder exists! Please try again", Goto MyBarcode

Else
If Dir(MyDirectory, vbDirectory) = "" Then MkDir MyDirectory

2 个答案:

答案 0 :(得分:8)

使用DirGetAttr函数检查目录是否存在,如下所示:

Function DirectoryExists(Directory As String) As Boolean
    DirectoryExists = False
    If Not Dir(Directory, vbDirectory) = "" Then
        If GetAttr(Directory) = vbDirectory Then
            DirectoryExists = True
        End If
    End If
End Function

Sub TestDirectoryExists()
    Dim Directory As String
    Directory = "c:\src"                    ' is a valid directory
    Debug.Print DirectoryExists(Directory)  ' shows True
    Directory = "c:\src1"                   ' is not a valid directory
    Debug.Print DirectoryExists(Directory)  ' shows False
    Directory = "x:\src"                    ' is not a valid drive
    Debug.Print DirectoryExists(Directory)  ' shows False
    Directory = "c:\test.txt"               ' is a file
    Debug.Print DirectoryExists(Directory)  ' shows False
End Sub

当我们将Dir与vbDirectory属性一起使用时,除了没有特殊属性的文件外,我们还在查找文件夹。为了确认我们确实在寻找文件夹,我们会使用 GetAttr并检查路径确实是一个文件夹。

您可以在按钮点击事件中使用此功能。

答案 1 :(得分:0)

不是吗

int main ()
{
    struct student info[3];   //declare 3 students [3]
    int i;
    int r;
    for(i=0; i < 3; i++) {
        printf("Enter name for student %d : ",i);
        fgets(info[i].name, 50, stdin);
        printf("Enter age for sudent %d : ",i);
        r = scanf("%d", &info[i].age);
        if (r < 1) {
            printf("input error, exiting\n");
            return 1;
        }
        printf("Enter grade for student %d : ",i);
        r = scanf("%d", &info[i].grade);
        if (r < 1) {
            printf("input error, retry\n");
            return 1;
        }
    }
    return 0;
}

代替

If GetAttr(Directory) AND vbDirectory Then

根据Microsoft吗?