运行时错误。自动化错误。远程过程调用失败

Posted

技术标签:

【中文标题】运行时错误。自动化错误。远程过程调用失败【英文标题】:run time error. automation error. the remote procedure call failed 【发布时间】:2021-12-29 04:53:47 【问题描述】:

我编写了下面的代码来对 excel 列进行排序,例如:

Sub deneme()
Workbooks("macro.xlsm").Activate
    
Dim excelApp As New Excel.Application
excelApp.Visible = False

excelApp.Workbooks.Open Filename:="C:\Users\cildi\Desktop\sortdeneme\New\new.xlsx"

Dim excelsWbk As Object    
Set excelsWbk = excelApp.Workbooks("new.xlsx")  

excelsWbk.Sheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key _
    :=Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
With excelsWbk.Sheets("Sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
End Sub

但我收到错误“” 在线:

excelsWbk.Sheets("Sheet1").AutoFilter.Sort.SortFields.Add2 Key _
    :=Range("A2"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal

我搜索但找不到任何解决方案。

感谢您的帮助。

【问题讨论】:

Range("A2") 需要限定:excelsWbk.Sheets("Sheet1").Range("A2") - 另外,请确保 excelsWbk 已正确设置为您想要的。 感谢您的评论。还是一样的错误:( 为什么要打开一个新的 Excel 实例? 这是否意味着您验证的excelsWbk 不等于Nothing 如果没有应用自动过滤器,可能会发生错误。要让excelsWbk.Sheets("Sheet1").AutoFilter..... 工作,应该已经有一个自动过滤器......否则你会得到Run-time error 91: Object variable or With block variable not set,因为它找不到自动过滤器。 【参考方案1】:

在另一个 Excel 实例中

Option Explicit

Sub HiddenSort()
    Const ProcName As String = "HiddenSort"
    
    Const FolderPath As String = "C:\Users\cildi\Desktop\sortdeneme\New\"
    Const fName As String = "new.xlsx"
    Const wsName As String = "Sheet1"
    Const SortColumn As Long = 1
    
    On Error Resume Next
    Dim twb As Workbook: Set twb = Workbooks(fName)
    On Error GoTo 0
    If Not twb Is Nothing Then
        MsgBox "The file '" & fName & "' in folder '" & FolderPath _
            & "' is already open.", vbCritical
        Exit Sub
    End If
    
    Dim xlApp As Object: Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True ' just out-comment, don't use 'False'.
    
    On Error GoTo ClearError
    
    Dim wb As Object: Set wb = xlApp.Workbooks.Open(FolderPath & fName)
    
    Dim ws As Object: Set ws = wb.Worksheets(wsName)
    If ws.AutoFilterMode Then
        ws.AutoFilterMode = False
    End If
    
    Dim rg As Range: Set rg = ws.Range("A1").CurrentRegion
    Dim krg As Range: Set krg = rg.Columns(SortColumn)
    
    rg.Sort Key1:=krg, Order1:=xlAscending, Header:=xlYes
    ws.AutoFilterMode = False
    wb.Close SaveChanges:=True
    Set wb = Nothing ' prevent 'Run-time error '-2147417848': Automation error'
    
SafeExit:
    If Not wb Is Nothing Then
        wb.Close SaveChanges:=False
    End If
    xlApp.Quit

    Exit Sub
ClearError:
    Debug.Print "'" & ProcName & "': Unexpected Error!" & vbLf _
              & "    " & "Run-time error '" & Err.Number & "':" & vbLf _
              & "    " & Err.Description
    Resume SafeExit
End Sub

【讨论】:

以上是关于运行时错误。自动化错误。远程过程调用失败的主要内容,如果未能解决你的问题,请参考以下文章

解决python在cmd运行时导入包失败,出现错误信息 "ModuleNotFoundError: No module named ***"

运行时错误:SWIG std::function 调用失败,在 azure databricks 中

使用 sql server 2008 R2 的远程过程调用失败

在 Invoke-AzVMRunCommand 错误时将 Azure 自动化状态设置为失败以触发警报

附录3 常见问题

断言失败:在 .js 文件 (emscripten) 中调用 c 函数时,在运行时初始化错误之前调用了本机函数 `int_sqrt`