使用vba的新手并尝试锻炼错误出在哪里,但无法解决。我得到
运行时错误438对象不支持此属性或方法
在“ LastRow”行中,不确定原因。
我正在尝试根据excel工作表中的数据自动设置多个Outlook任务(提醒您更新证书)。每行包括为每个证书设置每个任务所需的数据。 为此,我需要确定数据的最后一行。
这是我的宏的完整代码。你能看到我哪里出问题了吗?谢谢!
Sub Add_New_Task()
Dim olApp As Outlook.Application
Dim olTask As TaskItem
Dim wsMEDCMain As Worksheet
Dim LastRow As Long, RowNumber As Long
Set olApp = New Outlook.Application
Set wsMain = ThisWorkbook.Worksheets("Main")
With wsMain
LastRow = Cells(Rows.Count, "C").EndofUp.Row '<------- Line with error
If 2 > LastRow Then Exit Sub
For RowNumber = 2 To LastRow
If Len(Cells(RowNumber, "C").Value) > 0 Then
'Create Task Item
Set olTask = olApp.CreateItem(olTaskItem)
With olTask
.Subject = Cells(RowNumber, "C").Value
.Status = olTaskNotStarted
.Importance = olImportanceHigh
.StartDate = Cells(RowNumber, "B").Value
.DueDate = Cells(RowNumber, "A").Value
.ReminderSet = True
.ReminderTime = .StartDate & (" 09:00:00")
.Body = Cells(RowNumber, "D").Value & vbNewLine & ("Certificate Details: ") & Cells(RowNumber, ("C")).Value
.Save
End With
'Clear Object Variable
Set olTask = Nothing
End If
Next RowNumber
End With
End Sub
答案 0 :(得分:1)
您需要将EndofUp
替换为End(xlUp)
,这样才能为您解决!
答案 1 :(得分:0)
您应该对代码进行一些修改,其中一些已经由@Darren Bartrup-Cook列出
更改:
LastRow = Cells(Rows.Count, "C").EndofUp.Row
使用:
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
Len
,也未完全符合wsMain
工作表对象的作用。更改:
If Len(Cells(RowNumber, "C").Value) > 0 Then
使用:
If Len(.Range("C" & RowNumber).Value) > 0 Then
With
内使用With
语句。此行:
With olTask ' <-- Setting a With inside another With statement
在里面:
With wsMain