VBA转置超过255个字符的多个数组

时间:2017-05-16 20:28:09

标签: vba excel-vba excel

我尝试了很多不同的方法,但无法弄清楚。类似的帖子对我没有帮助,因为我以前从未这样做过,但是我有多个阵列需要转置,因为它有超过255个字符。下面是我的阵列。

Sub Quick()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Target As Range
Dim lastRow As Long
Dim FormulaR1C1

FormulaR1C1 = getR1C1Array
Set Target = Range("A1:ZZ1").Find("LiquidD")

If Not Target Is Nothing Then
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Set Target = Target.Offset(1).Resize(UBound(FormulaR1C1, 1), UBound(FormulaR1C1, 2))
    Target.FormulaR1C1 = FormulaR1C1
    Set Target = Target.Rows(Target.Rows.Count).Resize(lastRow - Target.Rows.Count)
    Target.Rows(1).AutoFill Destination:=Target
    'Uncomment to replace worksheet formulas with their value for better performance
    'Target.EntireColumn.Value = Target.EntireColumn.Value
End If

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

Function getR1C1Array()
Dim data
ReDim data(6)
data(0) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[1048574]C[-18]=RC[-18],COUNTIF(RC[-9]:R[1048574]C[-9],0)=0),AVERAGE(RC[-9]:R[1048574]C[-9]),0)", "=IF(AND(R[1048574]C[-19]=RC[-19], COUNTIF(RC[-9]:R[1048574]C[-9],0)=0),AVERAGE(RC[-9]:R[1048574]C[-9]),0)", "=IF(AND(R[1048574]C[-20]=RC[-20],COUNTIF(RC[-9]:R[1048574]C[-9],0)=0),AVERAGE(RC[-9]:R[1048574]C[-9]),0)", "=IF(AND(R[1048571]C[-21]=RC[-21],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-22]=RC[-22],COUNTIF(RC[6]:R[1048571]C[6],0)=0),AVERAGE(RC[6]:R[1048571]C[6]),0)", "=IF(R[1048571]C[-23]=RC[-23],AVERAGE(RC[6]:R[1048571]C[6]),0)", "=(INTERCEPT(RC[-11]:R[-5]C[-11],RC[-15]:R[-5]C[-15])+(RC[-15]*SLOPE(RC[-11]:R[5]C[-11],RC[-15]:R[5]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
data(1) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[-2]C[-18]=RC[-18],COUNTIF(R[-2]C[-9]:RC[-9],0)=0),AVERAGE(R[-2]C[-9]:RC[-9]),0)", "=IF(R[-2]C[-19]=RC[-19],AVERAGE(R[-2]C[-9]:RC[-9]),0)", "=IF(AND(R[-2]C[-20]=RC[-20],COUNTIF(R[-2]C[-9]:RC[-9],0)=0),AVERAGE(R[-2]C[-9]:RC[-9]),0)", "=IF(AND(R[1048571]C[-21]=RC[-21],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[-1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-22]=RC[-22],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-23]=RC[-23],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=(INTERCEPT(R[-1]C[-11]:R[4]C[-11],R[-1]C[-15]:R[4]C[-15])+(RC[-15]*SLOPE(R[-1]C[-11]:R[4]C[-11],R[-1]C[-15]:R[4]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
data(2) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[-2]C[-18]=RC[-18],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(R[-2]C[-19]=RC[-19],AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-2]C[-20]=RC[-20],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[1048571]C[-21]=RC[-21],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[-1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-22]=RC[-22],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-23]=RC[-23],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=(INTERCEPT(R[-2]C[-11]:R[3]C[-11],R[-2]C[-15]:R[3]C[-15])+(RC[-15]*SLOPE(R[-2]C[-11]:R[3]C[-11],R[-2]C[-15]:R[3]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
data(3) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[-2]C[-18]=RC[-18],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(R[-2]C[-19]=RC[-19],AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-2]C[-20]=RC[-20],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[1048571]C[-21]=RC[-21],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[-1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-22]=RC[-22],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=IF(AND(R[1048571]C[-23]=RC[-23],COUNTIF(RC[-6]:R[1048571]C[-6],0)=0),AVERAGE(RC[-6]:R[1048571]C[-6]),0)", "=(INTERCEPT(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])+(RC[-15]*SLOPE(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
data(4) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[-2]C[-18]=RC[-18],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(R[-2]C[-19]=RC[-19],AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-2]C[-20]=RC[-20],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-5]C[-21]=RC[-21],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=IF(AND(R[-5]C[-22]=RC[-22],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=IF(AND(R[-5]C[-23]=RC[-23],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=(INTERCEPT(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])+(RC[-15]*SLOPE(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
data(5) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[-2]C[-18]=RC[-18],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(R[-2]C[-19]=RC[-19],AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-2]C[-20]=RC[-20],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-5]C[-21]=RC[-21],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=IF(AND(R[-5]C[-22]=RC[-22],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=IF(AND(R[-5]C[-23]=RC[-23],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=(INTERCEPT(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])+(RC[-15]*SLOPE(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
data(6) = Array("=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]/DAY(EOMONTH(RC[-2],0))", "=RC[-2]", "=RC[-3]+RC[-2]/6", "=RC[-4]+RC[-3]/14", "=IF(AND(R[-2]C[-18]=RC[-18],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(R[-2]C[-19]=RC[-19],AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-2]C[-20]=RC[-20],COUNTIF(R[-2]C[-3]:RC[-3],0)=0),AVERAGE(R[-2]C[-3]:RC[-3]),0)", "=IF(AND(R[-5]C[-21]=RC[-21],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=IF(AND(R[-5]C[-22]=RC[-22],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=IF(AND(R[-5]C[-23]=RC[-23],COUNTIF(R[-5]C[-6]:RC[-6],0)=0),AVERAGE(R[-5]C[-6]:RC[-6]),0)", "=(INTERCEPT(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])+(RC[-15]*SLOPE(R[-3]C[-11]:R[2]C[-11],R[-3]C[-15]:R[2]C[-15])))-RC[-11]", "=IF(RC[-11]=0,0,IF(RC[-1]>RC[-11],0,1))", "=IF(RC[-1]=1,RC[-11],0)", "=0", "=0")
End Function

Function getFormulaR1C1Array(Source As Range)
Dim r As Range
Dim Result As String
Result = "Array("
For Each r In Source
    Result = Result & Chr(34) & Replace(r.FormulaR1C1, Chr(34), Chr(34) & Chr(34)) & Chr(34) & ","
Next

Result = Left(Result, Len(Result) - 1) & ")"

getFormulaR1C1Array = Result

End Function

目前没有超过255个字符的字符串,b / c我一直在单独处理它们,直到我能弄清楚如何转置它们。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

以下是我最近遇到这个问题时如何解决这个问题的一个例子。我包含了整个过程,因此您可以看到如何填充数组,但我使用的变通方法的核心是代码With wsStaging的最后一部分。

在这种情况下,arrSubjectivity由单元格填充,其中一些单元格具有> 255个字符的文字。

Sub StageSubjectivities()

sProcName = "StageSubjectivities"

With wsSubjectivities

    Dim lRow As Long
    lRow = .Cells(.Rows.Count, .Range("subjectivity_list").Column).END(xlUp).Row

    Dim rSubjectivity As Range

    For Each rSubjectivity In .Range(.Cells(.Range("subjectivity_list").Row + 2, 1), .Cells(lRow, 1))

        If rSubjectivity.Value = "X" Then

            Dim arrLOB() As Variant
            Dim arrSubjectivity() As Variant

            Dim iCnt As Integer
            ReDim Preserve arrLOB(iCnt)
            ReDim Preserve arrSubjectivity(iCnt)

            arrLOB(iCnt) = rSubjectivity.Offset(, 9)
            arrSubjectivity(iCnt) = rSubjectivity.Offset(, 1)

            iCnt = iCnt + 1

        End If

    Next

End With

With wsStaging
    Range(.Range("SubjectivityList").Offset(2), .Range("SubjectivityList").Offset(2).END(xlDown).Offset(0, 1)).Clear 'first it will clear the range and fill again
    .Range("SubjectivityList").Offset(2).Resize(UBound(arrLOB) + 1, 1).Value = Application.Transpose(arrLOB)
    'The below line errors out because of text > 255 not working with Array Transpose
    '.Range("SubjectivityList").Offset(2).Resize(UBound(arrSubjectivity) + 1, 1).Value = Application.Transpose(arrSubjectivity)
    'iCnt declared above to use as a counter
    For iCnt = 0 To UBound(arrSubjectivity)
        With .Range("SubjectivityList").Offset(iCnt + 2, 1)
            .Value = arrSubjectivity(iCnt)
            .WrapText = False 'because sometimes it wraps text aotmatically
        End With
    Next
End With

End Sub