刷新listobject命名范围时出现运行时错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了刷新listobject命名范围时出现运行时错误相关的知识,希望对你有一定的参考价值。

以下是我发送邮件的代码:

Sub SendRequestForApprovalMail()

 Application.ScreenUpdating = False
 Application.StatusBar = "Step 1/2 - Retrieving your pending approvals from SharePoint..."
  Range("T_myPendingApprovals[#All]").ListObject.Refresh

 'new in v.5.0 - date format is changed as for some users the date format looks strange
   Union(Range("T_myPendingApprovals[Start]"), Range("T_myPendingApprovals[End]")).NumberFormat = "m/d/yyyy"
   Union(Range("T_myPendingApprovals[Modified]"), Range("T_myPendingApprovals[Created]")).NumberFormat = "m/d/yyyy h:mm"

 If Application.WorksheetFunction.CountA(Range("T_myPendingApprovals[Person]")) = 0 Then
  Application.StatusBar = False
  Application.ScreenUpdating = True
  MsgBox "No entries with pending approval existing for you." & vbLf & vbLf & "Your supervisor should enter his name in the column 'Approved by', not you. Only then the records will be displayed in the mail for approval.", vbExclamation, "Approval mail generation"
  End
 End If

 Application.StatusBar = "Step 2/2 - Preparing approval mail..."

 SendMail "Request for vacation approval for " & UserNameWindows, _
           "Hi ..., <br><br>" & _
          "Please approve my vacation as stated below. To evaluate potential capacity issues in the team as well as " & _
          "towards important milestones, please refresh your offine copy of the " & _
          "<a href=""" & Range("DownloadPage") & """>Team availability Tracker</a> and consider all other parallel vacations in the chart for your approval decision.<br><br>" & _
          "Please enter your name in the column approver on the " & _
          "<a href=""" & Range("ApprovalView") & """>SharePoint</a>. " & _
          "Please do not just reply on this mail, make use of the SharePoint for 100% traceability and do the update(s) there. Only if the name of the supervisor appears in the column '[last] modified by', the approval can be considered as valid. Use the voting button to indicate that the approval took place.<br><br>Best regards,<br>" & FirstName _
           , , , , Export_Range_asImage(Range("T_myPendingApprovals[#All]"))
End Sub

我在代码的底线收到运行时错误(应用程序定义或对象定义错误)。

 Range("T_myPendingApprovals[#All]").ListObject.Refresh

T_myPendingApprovals [#All]“在工作表”my pending approvals“中命名为范围

答案

ListObject.Refresh需要一个表示ListObject对象的变量。您需要使用名称框中显示的对象的全名。

例如

Option Explicit

Sub GetListObjectNames()

    Dim currObject As ListObject

    For Each currObject In ActiveSheet.ListObjects

        Debug.Print currObject.Name

    Next currObject

End Sub

如果返回“Table1”作为名称

Dim myTable As ListObject
Set myTable = ActiveSheet.ListObjects("Table1") 'use actual sheet name. Don't assume Activesheet
myTable.Refresh

在你的例子中应该是这样的:

ThisWorkbook.Worksheets("my pending approvals").ListObjects("T_myPendingApprovals").Refresh

没有那些昂贵的刷新事件,例如:

ThisWorkbook.RefreshAll

以上是关于刷新listobject命名范围时出现运行时错误的主要内容,如果未能解决你的问题,请参考以下文章

在运行SQL时出现错误,请问如何解决?

使用 Angular 7、NGINX 和 Docker 刷新页面时出现 404 错误

在 R 中运行简单的“重命名”函数时出现错误消息

从字符数组更改为字符串时出现“字符串下标超出范围”错误?

从 keycloak 刷新访问令牌时出现 CORS 错误

登录responsinator.com时出现“页面因不活动而过期。请刷新并重试”