简体   繁体   中英

VBA “Invalid Qualifier” Error on Sub name

I'm in the early stages of creating an Excel workbook that will allow you to generate and send Outlook emails based on client data in the workbook using VBA. However, I'm getting a Compile Error for "Invalid Qualifier" in the Sub declaration line.

My problem code (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

The basic gist of this is to get it to prompt the user to select a file then to put that file path in a cell so I can use it as an attachment in the Outlook window later.

This ran correctly the first time but will no longer work and throws the "Invalid Qualifier" error on the Sub name line.

Problem line highlighted in debugger

I have tried:

  • Changing the Sub name in case there was a conflict
  • Clearing the cell that the first successful run output to
  • Restarting Excel

The problematic line is:

Range("D").Column.AutoFit
  • "D" is not a valid address for Range : should be "D:D" .
  • Column is the column number, not a reference to the entire column.

To fix:

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

or

Columns("D").AutoFit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM