Excel 2016中VBA引用SortFields时报‘对象不支持该属性或方法’问题
Excel 2016 VBA报错“Object doesn't support this property or method”排查与修复
我尝试在包含所有题号的工作表中引用下述VBA函数,在另一独立工作表创建计分卡,点击“Create Scorecard”按钮时弹出“Object doesn't support this property or method”错误。使用的是Excel 2016版本,不确定是函数版本兼容问题还是代码本身存在错误。
原代码如下:
Sub A_CreateQuestion() Dim i, j, k As Integer Dim irow, icol, urow, ucol, ipat, iboo, upat, uboo, inam, unam As String Application.DisplayAlerts = False Application.CutCopyMode = False Sheets("Scorecard Build").Activate irow = Range("A65583").End(xlUp).Row If Cells(1, 14) <> "" Then Cells(2, 1).Copy Range(Cells(3, 1), Cells(irow, 1)) For j = irow To 2 Step -1 If Cells(j, 1) = "" Then Rows(j).Delete xlUp Next ActiveWorkbook.Worksheets("Scorecard Build").ListObjects("SB").Sort. _ SortFields.Clear ActiveWorkbook.Worksheets("Scorecard Build").ListObjects("SB").Sort. _ SortFields.Add2 Key:=Range("SB[[#All],[Item '#]]"), SortOn:= _ xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Scorecard Build").ListObjects("SB").Sort ActiveWorkbook.Worksheets("Scorecard Build").ListObjects("SB").Sort. _ SortFields.Add2 Key:=Range("SB[[#All],[Item '#]]"), SortOn:= _ xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal With ActiveWorkbook.Worksheets("Scorecard Build").ListObjects("SB").Sort .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End
错误原因分析
- 版本兼容性问题:
SortFields.Add2是Excel 2019及后续版本新增的方法,Excel 2016不支持该属性,这是报错的核心原因。 - 语法结构错误:代码存在多处不完整结构,包括重复的排序字段添加代码、未闭合的
With语句,同时变量声明不规范(多个变量仅最后一个被指定类型,其余默认Variant),且依赖Activate操作易引发对象引用混乱。
修正后的代码
Sub A_CreateQuestion() Dim i As Integer, j As Integer, k As Integer Dim irow As String, icol As String, urow As String, ucol As String Dim ipat As String, iboo As String, upat As String, uboo As String Dim inam As String, unam As String Dim wsScorecard As Worksheet Dim tblSB As ListObject Application.DisplayAlerts = False Application.CutCopyMode = False ' 直接引用工作表,避免Activate操作 Set wsScorecard = ThisWorkbook.Worksheets("Scorecard Build") Set tblSB = wsScorecard.ListObjects("SB") ' 适配Excel 2016的行范围获取方式 irow = wsScorecard.Cells(wsScorecard.Rows.Count, 1).End(xlUp).Row ' 填充空行的第一列 If wsScorecard.Cells(1, 14).Value <> "" Then wsScorecard.Cells(2, 1).Copy wsScorecard.Range(wsScorecard.Cells(3, 1), wsScorecard.Cells(irow, 1)) End If ' 删除空行 For j = irow To 2 Step -1 If wsScorecard.Cells(j, 1).Value = "" Then wsScorecard.Rows(j).Delete xlUp End If Next j ' 重置并设置排序,适配Excel 2016 With tblSB.Sort .SortFields.Clear ' 用Add替代Add2兼容旧版本 .SortFields.Add Key:=tblSB.ListColumns("Item '#").Range, _ SortOn:=xlSortOnValues, _ Order:=xlAscending, _ DataOption:=xlSortNormal .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With ' 恢复系统提示 Application.DisplayAlerts = True End Sub
关键修正点
- 替换
SortFields.Add2为SortFields.Add,适配Excel 2016版本; - 修正
With语句结构,添加End With完成闭合; - 规范变量声明,每个变量明确指定数据类型;
- 取消
Activate操作,直接通过对象引用操作工作表和列表对象,提升代码稳定性; - 替换固定行号
65583为Rows.Count,适配不同Excel版本的行范围; - 删除重复的排序字段添加代码段;
- 恢复
Application.DisplayAlerts = True,避免屏蔽后续系统提示。
内容的提问来源于stack exchange,提问作者Saurabh Dalve
相关产品推荐
相关产品推荐

