错误发生在另一个文件上进行排序时运行时错误 – 2147023170 (800706be)。

huangapple go评论71阅读模式
英文:

Error when Sorting on another file Runtime error - 2147023170 (800706be)

问题

错误发生在这一行:

.SetRange Range("V:W")

Excel崩溃,然后我收到以下消息:

运行时错误 '2147023170 (800706be)':

自动化错误

远程过程调用失败

英文:

I have a database file that stores everyone's holiday.
This runs great, however I have issues when trying to Sort it after it has cleaned up some of the names on the list.

This is my code:

Option Explicit

Sub clear_holiday()
Dim wn As Worksheet
Dim ws As Worksheet
Dim i As Long
Dim name As String
Dim wDate As Date
Dim holiday As Double
Dim Rlastrow As Long

Application.ScreenUpdating = False

open_wb_onedrive 'opens the document where the database is.
Set ws = openwb.Worksheets("data") 'assigns the sheet to a variable

With ws

    .Unprotect Password:=pass
    i = 2
    
    Do Until .Cells(i, 22).value = "" 'go through all the values in the date column
    
        name = Trim(.Cells(i, 22).value) 'remove blank spaces and get employees name
        wDate = .Cells(i, 23).value 'retrieves date of the holiday
        
        .Activate
        Rlastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
        
        If wDate <= Date Then 'if the persons holiday is in the past we update the data
            
            'updates employees data on the table
            holiday = FirstPartMatch(name, .Range("A1:A" & Rlastrow))
            .Cells(holiday, 1).value = name
            
            'clears the data from holiday column
            .Cells(i, 22).value = ""
            .Cells(i, 23).value = ""
        
        End If
        
        i = i + 1
        
    Loop
    
    'Sorts holiday information removing blank rows
    .Activate
    openwb.Worksheets("data").Columns("V:W").Select
    openwb.Worksheets("data").Sort.SortFields.Clear
    openwb.Worksheets("data").Sort.SortFields.Add2 Key:=Range("W1:W1000"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With .Sort
        .SetRange Range("V:W")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
    .Protect Password:=pass
    
End With

get_data ws
End Sub

The error occurs on this line:

.SetRange Range("V:W")

Excel crashes, and then I get the message:

Run-time error '2147023170 (800706be)':

Automation error

The remote procedure call failed

答案1

得分: 0

您没有将范围与相关的工作表关联起来。请使用以下代码:

.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=.Range("W1:W1000"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
    .SetRange ws.Range("V:W")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
英文:

You're not qualifying the ranges with the relevant worksheet. Use:

.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=.Range("W1:W1000"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
    .SetRange ws.Range("V:W")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

huangapple
  • 本文由 发表于 2023年7月24日 17:37:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定