如果文件扩展名为 .xls,则 MsgBox

If file extension is .xls, then MsgBox

我编写了一个 VBA 脚本,它为具有大量数据(从 45,000 到 500,000 行不等)的客户端执行不同的 sorting/matching/filtering 功能。

问题是,如果用户将此数据导出或保存为 .xls 文件,则只会保存 65,536 行,这可能不够。在执行脚本时,如果用户 "mistakenly" 将数据导出为 .xls 文件,我希望出现 MsgBox,否则继续。

我已经尝试了下面的代码以查看我是否可以正确获取文件扩展名,但是我的 MsgBox 没有返回任何内容:

Sub ext()

Dim extFind As String
Dim sFile As String

    Dim FilePath As String
    FilePath = Application.ActiveWorkbook.Path
    sFile = Dir(FilePath & Filename & "*")
    extFind = Right$(sFile, Len(sFile) - InStrRev(sFile, "."))
    MsgBox extFind
End Sub

如有任何建议,我们将不胜感激。

您在 FilePathFilename 之间漏掉了一个斜线:

sFile = Dir(FilePath & "\" & Filename & "*")

顺便问一下,你在哪里给变量Filename赋值?如果您打算使用活动工作簿的名称,则应使用 ActiveWorkbook.Name.

FileSystemObject 库有一个 GetExtensionName() 函数可以让生活更轻松:

With CreateObject("Scripting.FileSystemObject")
    strExt = .GetExtensionName(ActiveWorkbook.Path)
End With

If StrComp(strExt, "xls", vbTextCompare) = 0 Then
    ' Display error
End If

或者,您可以只检查工作簿中的行数:

If ActiveSheet.Rows.Count = 65536 Then
    ' Display error
End If