【问题标题】:Can I assign a string value containing the name of a workbook as an argument?我可以分配一个包含工作簿名称的字符串值作为参数吗?
【发布时间】:2019-05-15 20:14:40
【问题描述】:

总而言之,工作簿的名称每周更改两次,我没有考虑另一种优化的自动执行方式,而是决定简单地显示一个输入框,在其中输入当前工作簿的名称,然后将其分配给:

Public fileName As String
fileName = InputBox("Type here the current workbook's name: ")  

Dim ext As String
ext = ".xlsb"

Set wk = Excel.Workbooks(fileName & ext)

错误9:下标超出范围

【问题讨论】:

  • 您要查找的工作簿是否已经打开?如果不是,那么您将收到该错误(并且需要使用 Workbooks.Open 方法)。如果它已经打开,那么手动输入工作簿名称的错字是最有可能的罪魁祸首。一般来说,您应该完全避免这种情况,我建议显示“打开文件”对话框,以便用户可以选择他们感兴趣的文件,这样可以减少出错的空间。更好的是,如果您可以预测工作簿的名称(因为它以已知的方式更改),最好生成/计算名称。
  • 它实际上没有打开我现在觉得很愚蠢,谢谢你的建议。我使用宏还不到一个月,所以还有很多东西要学。
  • 您尝试使用的工作簿是否包含代码?如果是这种情况,您可以直接使用 Set wk=ThisWorkbook 并完全避免使用名称。
  • 如果您需要打开工作表,请考虑使用Application.FileDialog(msoFileDialogFilePicker) 而不是要求用户输入文件名(他们将不可避免地有一半的时间会出错)
  • @DavidN 不幸的是

标签: excel vba


【解决方案1】:

从技术上讲,这并不完全符合您的要求,但让用户在其中选择文件是一种更好的编程实践 操作系统界面,而不是让他们输入文件名, 最终达到相同的目标。

我创建了一个自定义函数,用于通过FileDialog 对象选择文件。作为可选参数,您可以在 format 参数中强制执行特定的文件类型。

get_file([format], [fullpath])

' Returns a file name of a selected file via the .FileDialog object
' Arguments:
'    [format] : Optional argument, if left empty any file format can be selected, _
                otherwise enforce what [format] was put in to be selected
'    [fullpath] : Optional argument, if left empty will only return FileName.xxx, _
                  otherwise will return full path (eg. D:\MyDir\Book.xlsx")
' Returns: A <String> with the selected filename.

函数如下所示:

Private Function get_file(Optional ByVal format As String = "nomatch", _
                          Optional ByVal fullpath As Boolean = False) As String

    Dim fs As FileDialog: Set fs = Application.FileDialog(msoFileDialogFilePicker)
    Dim goodmatch As Boolean: goodmatch = False

    Do Until goodmatch = True
        With fs
            If .Show <> -1 Then
                .Title = "Choose a Workbook to work with"
                .AllowMultiSelect = False
                .InitialFileName = Application.DefaultFilePath
            End If

            If format = "nomatch" Then
                goodmatch = True
            Else
                format = Replace(format, ".", "")
                If Mid(.SelectedItems(1), InStrRev(.SelectedItems(1), ".") + 1) <> format Then
                    MsgBox "Please select a " & format & " file format", vbCritical
                Else
                    goodmatch = True
                End If
            End If
        End With
    Loop

    If fullpath = True Then
       get_file = fs.SelectedItems(1)
    Else
       get_file = Mid(fs.SelectedItems(1), InStrRev(fs.SelectedItems(1), "\") + 1)
    End If

End Function

函数的示例用户:

Private Sub test()
    Dim wb As Workbook: Set wb = Workbooks.Open(get_file(".xlsx", fullpath:= True))
    ' saves a workbook of only .xlsx type from what user selected into wb variable
    Debug.Print wb.Name
End Sub

使用 [fullpath] 打开工作簿以获得正确的链接 (True) 和强制执行的 .xlsx [format] 并打印保存在 wb 变量中的 Workbook 对象的 Name 属性

【讨论】:

    猜你喜欢
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 2022-12-08
    相关资源
    最近更新 更多