excel 在另一个文件上排序时出错运行时错误- 2147023170(800706 be)

z9ju0rcb  于 2023-08-08  发布在  其他
关注(0)|答案(1)|浏览(218)

我有一个数据库文件,存储每个人的假期。这运行得很好,但我有问题时,试图排序后,它已经清理了一些名单上的名字。
下面是我的代码:

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

字符串
错误发生在这一行:

.SetRange Range("V:W")


Excel崩溃了,然后我得到消息:
运行时错误“2147023170(800706be)”:
自动化错误
远程过程调用失败

noj0wjuj

noj0wjuj1#

您没有使用相关工作表限定范围。用途:

.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

字符串

相关问题