如何使用VBA将大字符串输入Excel单元格?

时间:2013-04-25 21:42:02

标签: excel excel-vba vba

我正在使用Excel VBA。我有一个字符串,我想写入一个单元格。我不确定实际的字符限制是什么,但是如果有很长的限制,我会遇到运行时错误。这是程序中断的行:

Cells(i, 7).Formula = region_opp_amount & ")"

此场景中的字符串“region_opp_amount”的长度为1691.它基本上是一个大的SUM公式。这是字符串包含的内容(要汇总的大量单元格):“= sum(G2057,G2066,G2069,G2072,G2076,G2079,G2082,.....”

有什么方法可以克服这个限制吗?

这是创建此字符串的实际循环:

Do While i <= lastRow
    If Cells(i, 2).value <> "" Or i = lastRow Then
        If i <> lastRow Then
            region = Left(Cells(i, 2).value, InStr(Cells(i, 2).value, " - ") - 1)
            rep = Right(Cells(i, 2).value, Len(Cells(i, 2).value) - InStr(Cells(i, 2).value, " - ") - 2)
        Else
            region = ""
            rep = ""
            i = i + 1
        End If
        If rep <> last_rep And i <> 2 Then
            Rows(i).Insert
            With Rows(i)
                .ClearFormats
                .Font.Bold = True
                On Error Resume Next
                .Ungroup
                On Error GoTo 0
            End With
            lastRow = lastRow + 1
            Cells(i, 2).value = last_rep & " Subtotal:"
            Cells(i, 7).Formula = rep_opp_amount & ")"
            Cells(i, 9).Formula = replace(rep_opp_amount, "G", "I") & ")"
            Cells(i, 12).Formula = "=sum(L" & CStr(rep_start) & ":L" & CStr(i - 1) & ")"
            Cells(i, 13).Formula = "=sum(M" & CStr(rep_start) & ":M" & CStr(i - 1) & ")"
            Cells(i, 14).Formula = "=sum(N" & CStr(rep_start) & ":N" & CStr(i - 1) & ")"
            Cells(i, 15).Formula = "=sum(O" & CStr(rep_start) & ":O" & CStr(i - 1) & ")"
            Range("B" & CStr(i) & ":O" & CStr(i)).Interior.Color = 10092543
            'Rows(CStr(rep_start) & ":" & CStr(i - 1)).Group
            ReDim Preserve rep_group_array(y)
            rep_group_array(y) = CStr(rep_start) & ":" & CStr(i - 1)
            y = y + 1
            i = i + 1
            rep_opp_amount = "=sum("
            rep_start = i
        End If
        If region <> last_region And i <> 2 Then
            Rows(i).Insert
            With Rows(i)
                .ClearFormats
                .Font.Bold = True
                On Error Resume Next
                .Ungroup
                On Error GoTo 0
            End With
            lastRow = lastRow + 1
            rep_start = rep_start + 1
            Cells(i, 2).value = last_region & " Subtotal:"
            Cells(i, 7).Formula = region_opp_amount & ")"
            Cells(i, 9).Formula = replace(region_opp_amount, "G", "I") & ")"
            Cells(i, 12).Formula = replace(region_opp_amount, "G", "L") & ")"
            Cells(i, 13).Formula = replace(region_opp_amount, "G", "M") & ")"
            Cells(i, 14).Formula = replace(region_opp_amount, "G", "N") & ")"
            Cells(i, 15).Formula = replace(region_opp_amount, "G", "O") & ")"
            total_opp_amount = total_opp_amount & "G" & CStr(i) & ","
            Range("B" & CStr(i) & ":O" & CStr(i)).Interior.Color = 5296274
            'Rows(CStr(region_start) & ":" & CStr(i - 1)).Group
            ReDim Preserve group_array(index)
            group_array(index) = CStr(region_start) & ":" & CStr(i - 1)
            index = index + 1
            i = i + 1
            region_opp_amount = "=sum("
            region_start = i
        End If
        rep_opp_amount = rep_opp_amount & "G" & CStr(i) & ","
        region_opp_amount = region_opp_amount & "G" & CStr(i) & ","
        last_region = region
        last_rep = rep
    End If
    i = i + 1
Loop

字符串在出错时包含:     =总和(G2057,G2066,G2069,G2072,G2075,G2079,G2082,G2085,G2089,G2092,G2097,G2100,G2103,G2106,G2109,G2112,G2118,G2122,G2125,G2128,G2133,G2134,G2137,G2142 ,G2145,G2152,G2155,G2159,G2162,G2165,G2170,G2173,G2176,G2182,G2188,G2191,G2195,G2200,G2203,G2207,G2210,G2213,G2220,G2224,G2227,G2232,G2236,G2239,G2243 ,G2248,G2254,G2257,G2260,G2266,G2272,G2275,G2280,G2283,G2287,G2291,G2295,G2298,G2302,G2308,G2311,G2315,G2318,G2321,G2324,G2330,G2333,G2337,G2340,G2345 ,G2348,G2353,G2356,G2360,G2363,G2390,G2393,G2397,G2404,G2407,G2410,G2413,G2422,G2425,G2433,G2437,G2441,G2448,G2449,G2453,G2456,G2466,G2470,G2476,G2480 ,G2484,G2485,G2499,G2505,G2508,G2513,G2516,G2519,G2526,G2530,G2533,G2537,G2543,G2547,G2550,G2553,G2556,G2557,G2560,G2563,G2566,G2580,G2581,G2586,G2589 ,G2601,G2605,G2611,G2614,G2619,G2625,G2628,G2633,G2638,G2646,G2649,G2653,G2659,G2670,G2673,G2677,G2680,G2683,G2686,G2698,G2701,G2704,G2705,G2708,G2711 ,G2714,G2718,G2721,G2725,G2729,G2733,G2742,G2745,G2748,G2754,G2757,G2762,G2766,G2769,G2773,G2776,G2779 ,G2784,G2789,G2792,G2795,G2798,G2801,G2804,G2808,G2811,G2814,G2818,G2834,G2837,G2840,G2845,G2848,G2851,G2854,G2864,G2868,G2869,G2872,G2876,G2879,G2882 ,G2885,G2888,G2892,G2896,G2900,G2901,G2904,G2909,G2912,G2915,G2919,G2923,G2926,G2930,G2933,G2936,G2946,G2952,G2956,G2959,G2964,G2967,G2971,G2974,G2977 ,G2980,G2983,G2984,G2989,G2992,G2996,G3000,G3003,G3009,G3014,G3024,G3029,G3032,G3035,G3038,G3041,G3044,G3045,G3048,G3052,G3055,G3056,G3059,G3062,G3065 ,G3069,G3073,G3077,G3081,G3084,G3088,G3093,G3096,G3099,G3100,G3103,G3107,G3110,G3113,G3119,G3124,G3127,G3130,G3135,G3138,G3141,G3142,G3143,G3146,G​​3147 ,G3150,G3151,G3152,G3155,G3156,G3157,G3160,G3174,G3176,G3181,G3188,G3191,G3198,G3203,G3206,G3210,

1 个答案:

答案 0 :(得分:3)

好的,这是一个例子。在这个例子中,我直接采用了问题中提到的字符串。

<强> LOGIC

  1. 拆分单元格地址并将其存储在数组中
  2. 重新制作字符串,确保长度不超过240(我使用过230.我正在考虑像XFD1048576这样的单元格)
  3. 达到该限制后,创建一个命名范围。我使用了像MyRange1,MyRange2,MyRange3...这样的名字。可以使用更短的名称,例如Rng1,Rng2...
  4. 重复步骤2和3,直到完成所有命名范围。
  5. 只需在公式中使用命名范围
  6. <强> CODE

    Option Explicit
    
    Sub Sample()
        Dim MyAr() As String
        Dim strRng As String, strFormula As String, strTmp As String
        Dim i As Long, nmRngCount As Long
        Dim ws As Worksheet
    
        Set ws = ThisWorkbook.Sheets("Sheet1")
    
        '~~> Sample Cells
        strRng = "G2057,G2066,G2069,G2072,G2075,G2079,G2082,G2085,G2089,G2092,G2097,G2100,G2103,G2106,G2109,G2112,G2118,G2122,G2125,G2128,G2133,G2134,G2137,G2142,"
        strRng = strRng & "G2145,G2152,G2155,G2159,G2162,G2165,G2170,G2173,G2176,G2182,G2188,G2191,G2195,G2200,G2203,G2207,G2210,G2213,G2220,G2224,G2227,G2232,G2236,"
        strRng = strRng & "G2239,G2243,G2248,G2254,G2257,G2260,G2266,G2272,G2275,G2280,G2283,G2287,G2291,G2295,G2298,G2302,G2308,G2311,G2315,G2318,G2321,G2324,G2330,"
        strRng = strRng & "G2333,G2337,G2340,G2345,G2348,G2353,G2356,G2360,G2363,G2390,G2393,G2397,G2404,G2407,G2410,G2413,G2422,G2425,G2433,G2437,G2441,G2448,G2449,"
        strRng = strRng & "G2453,G2456,G2466,G2470,G2476,G2480,G2484,G2485,G2499,G2505,G2508,G2513,G2516,G2519,G2526,G2530,G2533,G2537,G2543,G2547,G2550,G2553,G2556,"
        strRng = strRng & "G2557,G2560,G2563,G2566,G2580,G2581,G2586,G2589,G2601,G2605,G2611,G2614,G2619,G2625,G2628,G2633,G2638,G2646,G2649,G2653,G2659,G2670,G2673,"
        strRng = strRng & "G2677,G2680,G2683,G2686,G2698,G2701,G2704,G2801,G2804,G2808,G2811,G2814,G2818,G2834,G2837,G2840,G2845,G2848,G2851,G2854,G2864,G2868,G2869,"
        strRng = strRng & "G2882,G2885,G2888,G2892,G2896,G2900,G2901,G2904,G2909,G2912,G2915,G2919,G2923,G2926,G2930,G2933,G2936,G2946,G2952,G2956,G2959,G2964,G2967,"
        strRng = strRng & "G2983,G2984,G2989,G2992,G2996,G3000,G3003,G3009,G3014,G3024,G3029,G3032,G3035,G3038,G3041,G3044,G3045,G3048,G3052,G3055,G3056,G3059,G3062,"
        strRng = strRng & "G3081,G3084,G3088,G3093,G3096,G3099,G3100,G3103,G3107,G3110,G3113,G3119,G3124,G3127,G3130,G3135,G3138,G3141,G3142,G3143,G3146,G3147,G3150,"
        strRng = strRng & "G3157,G3160,G3174,G3176,G3181,G3188,G3191,G3198,G3203,G3206,G3210"
    
        '~~> Split each cell and store it's address in an array
        MyAr = Split(strRng, ",")
    
        nmRngCount = 1
    
        '~~> Loop through the array and rejoin the cell addresses
        For i = LBound(MyAr) To UBound(MyAr)
            strTmp = strTmp & "," & MyAr(i)
    
            '~~> 230 seems a pretty safe number even if I consider cells like XFD1048576)
            If Len(strTmp) > 230 Then
                '~~> Ignore the 1st Comma
                strTmp = Mid(strTmp, 2)
    
                '~~> Creat Names for the range
                ws.Range(strTmp).Name = "MyRange" & nmRngCount
    
                nmRngCount = nmRngCount + 1
                strTmp = ""
            End If
        Next i
    
        '~~> Join the Names so that we get something like
        '~~> MyRange1,MyRange2,MyRange3,....
        For i = 1 To (nmRngCount - 1)
            strFormula = strFormula & "," & "MyRange" & i
        Next
    
        '~~> Ignore the 1st Comma
        strFormula = Mid(strFormula, 2)
    
        '~~> Insert the final formula
        ws.Range("A1").Formula = "=SUM(" & strFormula & ")"
    End Sub
    

    <强> SCREENSHOT

    enter image description here

    所以这六个命名范围有大约240个细胞。