range.Address& Range.Address(,, xlR1c1)

时间:2017-02-28 12:13:51

标签: excel vba excel-vba

发现它很奇怪,但第一行和第一行的输出之间存在差异。第二行的输出。注意行641& R1C1格式错过了641。

有任何线索吗?

PS:手动添加空格以便于阅读。

Debug.Print rngVisibleRange.Address
  <$> $ A $ 1:$ XEW $ 1,$ A $ 226:$ XEW $ 226,$ A $ 239:$ XEW $ 239,$ A $ 370:$ XEW $ 370,$ A $ 373:$ XEW $ 374,$ A $ 462:$ XEW $ 462 ,$ 474美元:$ 474美元,479美元:$ 4100美元,491美元:491美元,5100美元:523美元:5100美元,560美元:560美元,560美元:5800美元,$ 584美元590美元:$ XEW $ 591,$ 637:$ XEW $ 639,$ 641美元:$ XEW $ 643,$ A $ 648:$ XEW $ 648

Debug.Print rngVisibleRange.Address(, , xlR1C1)
  

R1C1:R1C16377,R226C1:R226C16377,R239C1:R239C16377,R370C1:R370C16377,R373C1:R374C16377,R462C1:R462C16377,R474C1:R474C16377,R479C1:R481C16377,R491C1:R491C16377,R523C1:R524C16377,R560C1:R560C16377,R582C1:R584C16377 ,R590C1:R591C16377,R637C1:R639C16377

可验证示例:

Public Sub test()
    Dim r As Range
    Set r = [A1:XEW1,A226:XEW226,A239:XEW239,A370:XEW370,A373:XEW374,A462:XEW462,A474:XEW474,A479:XEW481,A491:XEW491,A523:XEW524,A560:XEW560,A582:XEW584,A590:XEW591,A637:XEW639,A641:XEW643,A648:XEW648]
    Debug.Print r.Areas.Count                              ' 16
    Debug.Print UBound(Split(r.Address, ","))              ' 15
    Debug.Print UBound(Split(r.Address(0, 0), ","))        ' 15
    Debug.Print UBound(Split(r.Address(, , xlR1C1), ","))  ' 13 (2 areas missing)
End Sub

2 个答案:

答案 0 :(得分:1)

为避免255个字符的限制,以下两个功能可能有所帮助:

Public Function SetRange(s As String) As Range

Dim i As Long

For i = LBound(Split(s, ",")) To UBound(Split(s, ","))
    If SetRange Is Nothing Then
        Set SetRange = Worksheets(1).Range(Split(s, ",")(i))
    Else
        Set SetRange = Union(SetRange, Range(Split(s, ",")(i)))
    End If
Next i

End Function
Public Function GetRangeAddress(r As Range) As String

Dim i As Range

For Each i In r.Areas
    GetRangeAddress = GetRangeAddress & "," & i.Address(, , xlR1C1)
Next i

GetRangeAddress = Mid(GetRangeAddress, 2)

End Function

这是两个函数完成工作的简短sub

Option Explicit

Private Sub tmpSO()

Dim r As Range, s As String

s = "$A$1:$XEW$1, $A$226:$XEW$226, $A$239:$XEW$239, $A$370:$XEW$370, $A$373:$XEW$374, $A$462:$XEW$462, $A$474:$XEW$474, $A$479:$XEW$481, $A$491:$XEW$491, $A$523:$XEW$524, $A$560:$XEW$560, $A$582:$XEW$584, $A$590:$XEW$591, $A$637:$XEW$639, $A$641:$XEW$643, $A$648:$XEW$648"

Set r = SetRange(s)

Debug.Print r.Address
Debug.Print r.Address(, , xlR1C1)
Debug.Print GetRangeAddress(r)

End Sub

请注意,这些函数中有验证可确保传递给SetRange的字符串实际上可用于指向范围。此外,SetRange函数会在任何给定的Excel文件中自动采用Worksheet(1)。当然,这可以很容易地调整,并且两个函数都可以得到很大改进:可选参数,例如用于SetRange的工作表GetRangeAddress是否应返回 xlR1C1中的地址< / em>风格与否等。

然而,我想保持简单,只是解决原来的问题。

答案 1 :(得分:0)

不确定这是否有用,但当区域位于同一列时,相交形式的地址可以短得多。例如A:B (1:1,3:4)C1:C2 (R1,R3:R4)

Dim r As Range, s As String
Set r = [A1:XEW1,A226:XEW226,A239:XEW239,A370:XEW370,A373:XEW374,A462:XEW462,A474:XEW474,A479:XEW481,A491:XEW491,A523:XEW524,A560:XEW560,A582:XEW584,A590:XEW591,A637:XEW639,A641:XEW643,A648:XEW648]

' A1 and R1C1 regular form
s = r.Address(0, 0)
Debug.Print Len(s); UBound(Split(s, ",")); s ' " 187  15 A1:XEW1,A226:XEW226,A239:XEW239,A370:XEW370,A373:XEW374,A462:XEW462,A474:XEW474,A479:XEW481,A491:XEW491,A523:XEW524,A560:XEW560,A582:XEW584,A590:XEW591,A637:XEW639,A641:XEW643,A648:XEW648"

s = r.Address(, , xlR1C1)
Debug.Print Len(s), UBound(Split(s, ",")), s ' " 247  13 R1C1:R1C16377,R226C1:R226C16377,R239C1:R239C16377,R370C1:R370C16377,R373C1:R374C16377,R462C1:R462C16377,R474C1:R474C16377,R479C1:R481C16377,R491C1:R491C16377,R523C1:R524C16377,R560C1:R560C16377,R582C1:R584C16377,R590C1:R591C16377,R637C1:R639C16377"

' A1 and R1C1 intersect form
s = r.Areas(1).EntireColumn.Address(0, 0) & " (" & r.EntireRow.Address(0, 0) & ")"
Debug.Print Len(s), UBound(Split(s, ",")), s ' " 131  15 A:XEW (1:1,226:226,239:239,370:370,373:374,462:462,474:474,479:481,491:491,523:524,560:560,582:584,590:591,637:639,641:643,648:648)"

' R1C1 intersect
s = r.Areas(1).EntireColumn.Address(, , xlR1C1) & " (" & r.EntireRow.Address(, , xlR1C1) & ")"
Debug.Print Len(s), UBound(Split(s, ",")), s ' " 124  15 C1:C16377 (R1,R226,R239,R370,R373:R374,R462,R474,R479:R481,R491,R523:R524,R560,R582:R584,R590:R591,R637:R639,R641:R643,R648)"