Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 动态命名范围的输出不准确_Vba_Excel_Named Ranges - Fatal编程技术网

Vba 动态命名范围的输出不准确

Vba 动态命名范围的输出不准确,vba,excel,named-ranges,Vba,Excel,Named Ranges,我使用完全相同的代码命名两个工作簿中的动态范围。在将代码实现到工作簿111中的常规代码主体之前,我使用工作簿TestBook1测试代码 代码是: Dim HDaER As Worksheet Dim HDaERReturnLR As Long Dim HDaERReturnLC As Long Dim HDaERReturnsDNR As Range Dim HDaERReturns As String Set HDaER = Sheets("H

我使用完全相同的代码命名两个工作簿中的动态范围。在将代码实现到工作簿111中的常规代码主体之前,我使用工作簿TestBook1测试代码

代码是:

    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中得到的输出不准确:


我做错了什么?

之后的规范
:=HDaER.Cells(…)
显示为起点。。。在本例中,我使用了
After:=HDaER.Cells(107,1)
,但是我在
HDaER.Cells(105,1)
中使用了标题“Volatility”,这意味着使用HDaER.Cells(108,2)为行
指定范围的起始单元格.CurrentRegion
并包含另一个方向规范
SearchDirection:=xlPrevious
导致将
HDaER.Range(HDaER.Cells(108,2),HDaER.Cells(105,1))标记为要命名的动态范围

输出准确的代码为:

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(105, 1), _
            LookIn:=xlFormulas, LookAt:=xlPart, _
            SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row

            HDaERReturnLC = .Find(What:="*", After:=HDaER.Cells(105, 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
引用
HDaER.Cell(105,1)
作为起始点,后面的规范为
:=HDaER.Cell(105,1)
,从而得到准确的
.范围
选择。

回答@DisplayName的评论和回答:

好主意,坏结果

MsgBox
最后一行显示此结果:

MsgBox
最后一列显示此结果:

代码输出(如我之前回答的):

精确代码输入:


您的整个代码可能会崩溃为以下内容

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

是否缺少父工作簿引用?如果它们有相同的工作表名称,可能会产生混淆。Hello Reach@Jeeped:这两个工作表彼此没有关系。没有父工作簿。我只是想把工作表中的范围命名为“HDaER”。工作簿TestBook1.xlsm只是为了测试一段代码,然后将其实现为工作簿111.xlsm中的常规代码。没有任何其他工作簿或工作表的关联和引用。您确定第二个工作簿中的B108为空吗?
msgbox HDaER.Cells(108,2).CurrentRegion.address
显示了什么?有很多使用偏移量、索引、匹配、计数和计数来创建动态扩展命名范围的示例,而不使用VBA。这些在计算上自动扩展和收缩非常好的建议!我是最小代码的粉丝。我得到了一个精确的列和行计数的输出,但是从单元格B214到E222…你认为这是为什么呢?那么试试下面的方法:在
工作表(“HistoricalDataandExcessReturns”).cells(107,1)
place
MsgBox.End(xlDown)。Address
然后place
MsgBox.End(xlToRight)。地址
并查看结果,因此请准确了解您正在处理的实际数据设置并获得正确答案。顺便说一句,您会接受解决原始问题的任何答案,而不是取悦任何人。这意味着1)您必须根据您在问题中实际给出的数据测试答案,并接受答案(如果有的话)解决它。2) 你不必取悦任何人