我正在使用 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
该字符串在出错时包含以下内容: =sum(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,G22254,G2257,G2257,G2260,G22266,G2272,G2275,G22275,G22280,G2283,G2283,G2287,G2291,G2291,G2291 ,, G2291 ,, G2330,G2333,G2337,G2340,G2345,G2348,G2348,G2353,G2356,G2360,G2360,G2390,G2390,G2393,G2393,G2397,G2404,G2404,G2407,G2413,G2413 ,, G2413,G2413,G2413,G2413,G2413 ,, 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,G2885,G2888,G2892,G2892,G2896,G2900,G2901,G2901,G2904,G2909,G2909,G2912,G2912,G2915,915,9,9,19,9,19,9,19,9,9,19,9,9,19,9,9,19,9,9,19,9,9,19,9,9,15,9,9,19 ,, 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,G3147,G3150,G3151,G3152,G3155,G3156,G3157,G3160,G3174,G3176,G3181,G3188,G3191,G3198,G3203,G3206,G3210,