使用 CurrentRegion 的自定义范围将第一列的单元格索引设置为 -10?

Custom Range using CurrentRegion sets the cell index for the first column as -10?

这很奇怪,因为我什至不知道这是可能的,但希望有解决方案。提前致谢。

ThisWorkbook.gRosterPermData.Address

Returns: $L$1:$Z$6

这是奇怪的地方

.Range(.Cells(1, 2), .Cells(.Rows.Count, (14 + 1))).Address

Returns: $X$1:$AK$6(当我将所有'空白.'替换为ThisWorkbook.gRosterPermData时同样适用。)

因此,在测试实际获得范围后,我必须这样做:

.Range(.Cells(1, -10), .Cells(6, 4)).Address

我很困惑,我使用 CurrentRegion 和“L1”作为输入。

任何帮助或解释将不胜感激,因为我拥有的任何其他范围都没有发生过这种情况。谢谢

我对你的开头语法有点迷惑ThisWorkbook.gRosterPermData.Address

但是,如果引用(对于 . )是范围 $L$1:$Z$6(命名范围 gRosterPermData?),则 X1:AK6 是正确的。正如您将参考的是相对于该范围,而不是工作表。

Public Sub GetDimensions()
    
    '$L:$Z
    With ActiveSheet.Range("gRosterPermData")
                           'row 1        'column 26
        Debug.Print .Range(.Cells(1, 2), .Cells(.Rows.Count, (14 + 1))).Address
        Debug.Print "row = " & .Cells(1, 2).row, " column = " & .Cells(.Rows.Count, (14 + 1)).Column
        Debug.Print "row = " & .Cells(.Rows.Count, (14 + 1)).row
    End With
      
End Sub

with statement:

https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/with-statement

正如 Ron 在评论中提到的,可能是你调用了错误的对象(我假设在 With 语句中,并且打算在没有前导的情况下使用 .