VBA "Invalid Qualifier" 子名称错误

VBA "Invalid Qualifier" Error on Sub name

我正处于创建 Excel 工作簿的早期阶段,您可以使用 VBA 根据工作簿中的客户数据生成和发送 Outlook 电子邮件。但是,我在 Sub 声明行中收到“无效限定符”的编译错误。

我的问题代码(sourced from here):

'Get file path and put it in the proper cell
Sub GetFilePath()

    Dim DialogBox As FileDialog
    Dim path As String

    Set DialogBox = Application.FileDialog(msoFileDialogFilePicker)

    DialogBox.Title = "Select quarterly report for " & Range("A" & ActiveCell.Row) & _
        " " & Range("B" & ActiveCell.Row)
    DialogBox.Filters.Clear
    DialogBox.Show

    If DialogBox.SelectedItems.Count = 1 Then
        path = DialogBox.SelectedItems(1)
    End If

    Range("D" & ActiveCell.Row) = path
    Range("D").Column.AutoFit

End Sub

基本要点是让它提示用户 select 一个文件,然后将该文件路径放在一个单元格中,这样我就可以将它用作 Outlook 中的附件 window 稍后。

这 运行 第一次正确,但将不再起作用并在子名称行上抛出“无效限定符”错误。

Problem line highlighted in debugger

我试过:

有问题的行是:

Range("D").Column.AutoFit
  • "D" 不是 Range 的有效地址:应该是 "D:D".
  • Column是列号,不是对整列的引用。

修复:

Range("D:D").EntireColumn.AutoFit

Columns("D").AutoFit