我是VBA的新手,遇到了一個對我來說沒有意義的錯誤。我正在創建一個宏,該宏將進入工作簿並採用第一列中的所有條目,並使用條目作爲電子郵件地址創建電子郵件。這是我的代碼:嘗試獲取列中的條目數時,下標超出範圍
Public Sub emailList()
'Setting up the Excel variables.
Dim olApp As Object
Dim olMailItm As Object
Dim iCounter As Integer
Dim Dest As Variant
Dim SDest As String
'Create the Outlook application and the empty email.
Set olApp = CreateObject("Outlook.Application")
Set olMailItm = olApp.CreateItem(0)
'Using the email, add multiple recipients, using a list of addresses in column A.
With olMailItm
SDest = ""
For iCounter = 1 To WorksheetFunction.CountA(Workbooks("Book1.xls").Sheets(1).Columns(1))
If SDest = "" Then
SDest = Cells(iCounter, 1).Value
Else
SDest = SDest & ";" & Cells(iCounter, 1).Value
End If
Next iCounter
'Do additional formatting on the BCC and Subject lines, add the body text from the spreadsheet, and send.
.BCC = SDest
.Subject = "FYI"
.Body = ActiveSheet.TextBoxes(1).Text
.Send
End With
'Clean up the Outlook application.
Set olMailItm = Nothing
Set olApp = Nothing
End Sub
行,上面寫着:
For iCounter = 1 To WorksheetFunction.CountA(Workbooks("Book1.xls").Sheets(1).Columns(1))
拋出一個下標超出範圍的錯誤,但我在第1列三個條目,所以我不明白爲什麼它拋出一個徹頭徹尾的邊界錯誤。
可能意味着你沒有與確切名稱的工作簿...... –
我這樣做雖然。我把它保存在我的Documents文件夾中,這是一個問題嗎? –
是運行此代碼時打開的工作簿?如果不是,您需要先打開它。只要它是開放的,它在哪裏被保存並不重要。 –