Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I enter a large string into an Excel cell with VBA?
    primarykey
    data
    text
    <p>I'm using Excel VBA. I have a string that I want write to a cell. I'm not sure what the actual character limitation is, but with really long ones I get a run-time error. Here's the line that the program breaks on:</p> <pre><code>Cells(i, 7).Formula = region_opp_amount &amp; ")" </code></pre> <p>The string "region_opp_amount" in this scenario has a length of 1691. It's basically a large SUM formula. Here's what the string contains (a large list of cells to sum): "=sum(G2057,G2066,G2069,G2072,G2076,G2079,G2082,....."</p> <p>Any way to overcome this limitation?</p> <p>Here's the actual loop that is creating this string:</p> <pre><code>Do While i &lt;= lastRow If Cells(i, 2).value &lt;&gt; "" Or i = lastRow Then If i &lt;&gt; 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 &lt;&gt; last_rep And i &lt;&gt; 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 &amp; " Subtotal:" Cells(i, 7).Formula = rep_opp_amount &amp; ")" Cells(i, 9).Formula = replace(rep_opp_amount, "G", "I") &amp; ")" Cells(i, 12).Formula = "=sum(L" &amp; CStr(rep_start) &amp; ":L" &amp; CStr(i - 1) &amp; ")" Cells(i, 13).Formula = "=sum(M" &amp; CStr(rep_start) &amp; ":M" &amp; CStr(i - 1) &amp; ")" Cells(i, 14).Formula = "=sum(N" &amp; CStr(rep_start) &amp; ":N" &amp; CStr(i - 1) &amp; ")" Cells(i, 15).Formula = "=sum(O" &amp; CStr(rep_start) &amp; ":O" &amp; CStr(i - 1) &amp; ")" Range("B" &amp; CStr(i) &amp; ":O" &amp; CStr(i)).Interior.Color = 10092543 'Rows(CStr(rep_start) &amp; ":" &amp; CStr(i - 1)).Group ReDim Preserve rep_group_array(y) rep_group_array(y) = CStr(rep_start) &amp; ":" &amp; CStr(i - 1) y = y + 1 i = i + 1 rep_opp_amount = "=sum(" rep_start = i End If If region &lt;&gt; last_region And i &lt;&gt; 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 &amp; " Subtotal:" Cells(i, 7).Formula = region_opp_amount &amp; ")" Cells(i, 9).Formula = replace(region_opp_amount, "G", "I") &amp; ")" Cells(i, 12).Formula = replace(region_opp_amount, "G", "L") &amp; ")" Cells(i, 13).Formula = replace(region_opp_amount, "G", "M") &amp; ")" Cells(i, 14).Formula = replace(region_opp_amount, "G", "N") &amp; ")" Cells(i, 15).Formula = replace(region_opp_amount, "G", "O") &amp; ")" total_opp_amount = total_opp_amount &amp; "G" &amp; CStr(i) &amp; "," Range("B" &amp; CStr(i) &amp; ":O" &amp; CStr(i)).Interior.Color = 5296274 'Rows(CStr(region_start) &amp; ":" &amp; CStr(i - 1)).Group ReDim Preserve group_array(index) group_array(index) = CStr(region_start) &amp; ":" &amp; CStr(i - 1) index = index + 1 i = i + 1 region_opp_amount = "=sum(" region_start = i End If rep_opp_amount = rep_opp_amount &amp; "G" &amp; CStr(i) &amp; "," region_opp_amount = region_opp_amount &amp; "G" &amp; CStr(i) &amp; "," last_region = region last_rep = rep End If i = i + 1 Loop </code></pre> <p>The string contains this when it errors out: =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,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,G3147,G3150,G3151,G3152,G3155,G3156,G3157,G3160,G3174,G3176,G3181,G3188,G3191,G3198,G3203,G3206,G3210,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload