复制和转置时克服255个字符的限制

时间:2018-03-22 14:07:39

标签: excel vba range transpose copying

基本上在单元格B42和B43中,我有超过255个字符,并且我的代码断开并导致RUN类型错误不匹配13。

当我运行以下行时:

    CopyTranspose wb.Sheets("Apple").Range("B17:B46"), shtDest.Cells(pasteRow, "R")

我在这里遇到不匹配错误:

Sub CopyTranspose(rngCopy As Range, rngDest As Range)
rngDest.Resize(rngCopy.Columns.Count, rngCopy.Rows.Count).Value = _
Application.Transpose(rngCopy.Value)
End Sub

2 个答案:

答案 0 :(得分:1)

如果您使用Google,则会发现Application.Transpose有几个奇怪的限制。但是你可以编写自己的TransposeArray函数,看看它是否更好。

Public Function TransposeArray(myarray As Variant) As Variant
    Dim x As Long
    Dim y As Long
    Dim Xlower As Long, Xupper As Long
    Dim Ylower As Long, Yupper As Long
    Dim tempArray As Variant

    Xlower = LBound(myarray, 2)
    Ylower = LBound(myarray, 1)
    Xupper = UBound(myarray, 2)
    Yupper = UBound(myarray, 1)
    ReDim tempArray(Xlower To Xupper, Ylower To Yupper)
    For x = Xlower To Xupper
        For y = Ylower To Yupper
            tempArray(x, y) = myarray(y, x)
        Next y
    Next x
    TransposeArray = tempArray
End Function

只需像Application.Tanspose

一样使用它
Sub CopyTranspose(rngCopy As Range, rngDest As Range)
    rngDest.Resize(rngCopy.Columns.Count, rngCopy.Rows.Count).Value = _
        TransposeArray(rngCopy.Value)
End Sub

答案 1 :(得分:0)

如果您只是想复制和转置范围,那么这样做:

wb.Sheets("Apple").Range("B17:B46").Copy
shtDest.Cells(pasteRow, "R").PasteSpecial Transpose:=True

或者如果只想复制没有格式的值,那么

shtDest.Cells(pasteRow, "R").PasteSpecial xlPasteValues, Transpose:=True

编辑: 问题可能是Application.Transpose(rngCopy.Value)作为转置输入数组或单元格范围,而不是值。