【问题标题】:Syntax Error with Named Range命名范围的语法错误
【发布时间】:2017-08-09 03:30:18
【问题描述】:

我似乎无法得到如下语法更正:

Sheet(DCRLog) 在 AE365 有一个名为 EndSrtRnge 的命名范围

我正在尝试将电子表格从 A3 排序到 AE365

Dim StSrtRnge As Range
Set StSrtRnge = Range("A3")

Dim NdSrtRnge As Range
Set NdSrtRnge = Range("EndSrtRnge")

Dim SortRnge As Range
Set SortRnge = Range(StSrtRnge:NdSrtRnge)*********Syntax Error

最后使用的语句是

SortRnge.Sort Key1:=SortKey, Order1:=xlAscending, Header:=xlYes

【问题讨论】:

    标签: vba excel syntax-error


    【解决方案1】:

    你应该使用:

    Set SortRnge = Range(StSrtRnge,NdSrtRnge)
    

    或者用你的语法,你需要:

    Set SortRnge = Range(StSrtRnge.Address & ":" & NdSrtRnge.Address)
    

    【讨论】:

    • 非常感谢,谢谢
    • @ChrisSmith:不客气!顺便说一句,当一个答案解决了您的问题时,您应该接受它(投票下的灰色勾号)以将您的问题标记为已回答! ;)
    【解决方案2】:

    Range 需要用逗号或字符串地址分隔的两个单元格。
    目前RANGE("A3")Range("EndSrtRnge") 可能在不同的工作表上,如果在设置Range("A3") 时激活了错误的工作表 - 您尚未指定它所在的工作表。

    Sub Test()
    
    Dim NdSrtRnge As Range
    Set NdSrtRnge = Range("EndSrtRnge")
    
    Dim StSrtRnge As Range
    'Ensure the start range is on the same sheet as the named range.
    'Using just RANGE will place it on the currently active sheet.
    Set StSrtRnge = NdSrtRnge.Parent.Range("A3")
    
    Dim SortRnge As Range
    Set SortRnge = NdSrtRnge.Parent.Range(StSrtRnge, NdSrtRnge)
    
    End Sub
    

    只需仔细检查 - 在 Sheet1 上设置 EndSrtRnge,然后在运行代码之前选择 Sheet2 会在仅使用 Range("A3") 时生成 Application-defined or object-defined error

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多