本文介绍了动态命名范围的输出不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用完全相同的代码在两个工作簿中命名一个动态范围.在将其实施到Workbook 111中通用代码的正文之前,我使用Workbook TestBook1进行代码测试.

I am using the exact same code to name a dynamic range in two workbooks. I use Workbook TestBook1 for testing the code before implementing it to the body of the general code in Workbook 111.

代码是:

    Dim HDaER As Worksheet
    Dim HDaERReturnLR As Long
    Dim HDaERReturnLC As Long
    Dim HDaERReturnsDNR As Range
    Dim HDaERReturns As String

    Set HDaER = Sheets("HistoricalDataandExcessReturns")

        With HDaER.Cells(108, 2).CurrentRegion
            HDaERReturnLR = .Find(What:="*", After:=HDaER.Cells(107, 1), _
            LookIn:=xlFormulas, LookAt:=xlPart, _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row

            HDaERReturnLC = .Find(What:="*", After:=HDaER.Cells(107, 1), _
            LookIn:=xlFormulas, LookAt:=xlPart, _
            SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

    Set HDaERReturnsDNR = HDaER.Range(HDaER.Cells(108, 2), _
            HDaER.Cells(HDaERReturnLR, HDaERReturnLC))
        End With

        HDaER.Names.Add Name:="HDaERReturns", RefersTo:=HDaERReturnsDNR

        Range("HDaERReturns").Select

我在TestBook1中获得的输出是准确的:

我在工作簿111中获得的输出不准确:

我在做什么错了?

推荐答案

您的整个代码可能会折叠到以下内容

your whole code could collapse to the following

With Sheets("HistoricalDataandExcessReturns").Cells(107, 1)
    With .Parent.Range(.End(xlDown), .End(xlToRight))
        .Parent.Names.Add name:="HDaERReturns", RefersTo:=.Resize(.Rows.Count - 1, .Columns.Count - 1).Offset(1, 1)
    End With

    .Range("HDaERReturns").Select
End With

这篇关于动态命名范围的输出不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 21:18
查看更多