Excel中的范围地址

时间:2018-05-16 11:25:27

标签: excel vba

我使用以下代码将选择的地址复制到变量(仅可见单元格),但最多只复制255个字符。如果地址超过255个字符,则它是有限的。假设字符串变量长度为63000个字符,不是吗?我怎么解决这个问题?非常感谢你提前。

代码:

Sel=Selection.SpecialCells(xlCellTypeVisible).Address(False, False)

可变内容:

C6:C10,C12:C13,C15:C27,C29:C31,C33:C54,C56:C64,C66,C68:C71,C73:C81,C83:C84,C86:C87,C89:C101,C103:C122,C124:C132,C135:C155,C158:C162,C164:C166,C168:C187,C189:C192,C194:C199,C201:C212,C214,C216:C233,C235:C251,C253:C262,C264:C267,C269:C270,C272:C292,C294

(251个字符)

它看起来应该是什么样的(例如):

C6:C10,C12:C13,C15:C27,C29:C31,C33:C54,C56:C64,C66,C68:C71,C73:C81,C83:C84,C86:C87,C89:C101,C103:C122,C124:C132,C135:C155,C158:C162,C164:C166,C168:C187,C189:C192,C194:C199,C201:C212,C214,C216:C233,C235:C251,C253:C262,C264:C267,C269:C270,C272:C292,C294:C296,C299:C316

(266个字符)

1 个答案:

答案 0 :(得分:0)

考虑:

Sub adres()
    Dim r As Range, s As String, rr As Range
    Set r = Selection.Cells.SpecialCells(xlCellTypeVisible)
    s = ""

    For Each rr In r
        s = s & "," & rr.Address(0, 0)
    Next rr

    s = Mid(s, 2)
    MsgBox s
End Sub

如果您想稍微“压缩”字符串,可以使用Areas