我正在使用Excel VBA。我有一個字符串,我想要寫入一個單元格。我不確定實際的字符限制是什麼,但是對於很長的字符,我會遇到運行時錯誤。下面是該方案突破上線:如何使用VBA將大字符串輸入到Excel單元格中?
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,G221 3,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,G2 952,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,
命名範圍,然後在公式中使用它? – 2013-04-25 21:44:20
該限制是255個字符。我不知道有什麼辦法可以繞過這個限制,即使使用'Names'。 – 2013-04-25 22:11:19
@DavidZemens:如果OP粘貼用於生成'region_opp_amount'的確切代碼,我可以展示一個示例:) – 2013-04-25 22:33:34