Numbers from average

giovyledzep

New Member
Joined
Jun 9, 2018
Messages
8
Hello everybody,

Is there a way in Excel to generate a list of numbers from an average value?

What I mean is for example, if I have a cell with the number 100, can I generate a list of numbers that average 100?
Of course telling Excel how long the list should be and what numbers are acceptable (for example I only want 25, 100, 200 to appear)

Any ideas?
Thanks
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Talking this one out without a clear solution.

Seems to me you might want to provide a range of values within the set, either by a percentage + or - of the average or by a value. As you stated, the number of numbers in the list.

A macro is in order. It could start by throwing out random numbers + and - until it reaches 90% of the number of items, then it would have to start evaluating how to correct the average and restrict the remaining numbers.

Jeff
 
Upvote 0
This is a pretty good random selection of values

Book3 20240221.xlsm
BCDEFGH
1100200
2ListPercent
312710.50%
4Average:10027321.00%
5Number of Items:20015331.50%
6Swing:300442.00%
7-17252.50%
83163.00%
923473.50%
1017584.00%
11-7894.50%
12389105.00%
13369115.50%
14119126.00%
15-188136.50%
16-143147.00%
17363157.50%
18-129168.00%
19-110178.50%
20-184189.00%
2169199.50%
22-842010.00%
233042110.50%
24-252211.00%
251602311.50%
262732412.00%
27-1292512.50%
282062613.00%
293652713.50%
301762814.00%
312682914.50%
323243015.00%
33-423115.50%
34-1393216.00%
352503316.50%
361973417.00%
372653517.50%
383753618.00%
393033718.50%
401293819.00%
411723919.50%
42444020.00%
43234120.50%
443324221.00%
45-1674321.50%
46-734422.00%
47-1094522.50%
481244623.00%
49-1674723.50%
501704824.00%
512314924.50%
52585025.00%
532335125.50%
54-1495226.00%
55835326.50%
56-55427.00%
572255527.50%
58-1775628.00%
59595728.50%
601565829.00%
611385929.50%
62936030.00%
63346130.50%
641376231.00%
653626331.50%
66196432.00%
671576532.50%
681256633.00%
692336733.50%
701516834.00%
713216934.50%
72-987035.00%
733757135.50%
74727236.00%
75-957336.50%
762807437.00%
771547537.50%
78-907638.00%
79377738.50%
80917839.00%
81907939.50%
82-98040.00%
83-1818140.50%
842268241.00%
851898341.50%
862698442.00%
873598542.50%
88758643.00%
89-1318743.50%
90-538844.00%
91-658944.50%
922389045.00%
93299145.50%
94-1649246.00%
95769346.50%
963459447.00%
971489547.50%
98-1739648.00%
99999748.50%
100-1479849.00%
1011339949.50%
10236010050.00%
10333010150.50%
104-15810251.00%
10535710351.50%
106-13210452.00%
1075310552.50%
1084810653.00%
109-1210753.50%
11036510854.00%
11122710954.50%
11238211055.00%
11326111155.50%
1147611256.00%
1159511356.50%
116-2011457.00%
11737111557.50%
118-7511658.00%
11927911758.50%
120911859.00%
12126311959.50%
12221112060.00%
12321612160.50%
12416812261.00%
125-19312361.50%
126-19712462.00%
1274912562.50%
12817012663.00%
1295812763.50%
13012312864.00%
131-19812964.50%
13213213065.00%
13324213165.50%
13418913266.00%
1351913366.50%
13632113467.00%
13731713567.50%
138-19413668.00%
1398713768.50%
140-16813869.00%
14122913969.50%
14239114070.00%
143-13514170.50%
1445814271.00%
1458714371.50%
1463914472.00%
147-4014572.50%
1484414673.00%
14938714773.50%
15011014874.00%
15123114974.50%
15220015075.00%
15314215175.50%
154-19515276.00%
155-18615376.50%
15639015477.00%
1573215577.50%
158-4815678.00%
15922415778.50%
16026715879.00%
1612215979.50%
16233216080.00%
1638216180.50%
164-1616281.00%
16513916381.50%
16624716482.00%
16724116582.50%
16811316683.00%
169616783.50%
1704616884.00%
171-2916984.50%
1721917085.00%
173-16617185.50%
174617286.00%
17538817386.50%
17630517487.00%
17720417587.50%
178-5717688.00%
17929317788.50%
18021517889.00%
181-9917989.50%
18211118090.00%
18310718190.50%
184-1618291.00%
185-10118391.50%
18630118492.00%
18726918592.50%
1888418693.00%
189-17418793.50%
190-17818894.00%
19128618994.50%
1929419095.00%
193-18119195.50%
19414319296.00%
1956319396.50%
19621419497.00%
19719319597.50%
198-14519698.00%
19930319798.50%
20027719899.00%
2014319999.50%
202-32200100.00%
Sheet1
Cell Formulas
RangeFormula
F1F1=AVERAGE(F3:F1579)
G1G1=COUNT(F3:F409)
H3:H202H3=G3/ItemNum
Named Ranges
NameRefers ToCells
ItemNum=Sheet1!$C$5H3:H202


VBA Code:
Sub RandomizeAverage()
  Dim Cel As Range
  Dim Rng As Range
  Dim AvgNum As Long
  Dim ItemNum As Long
  Dim Swing As Long
  Dim LSwing As Long
  Dim HSwing As Long
  Dim LstHdr As Range
  Dim X As Long
  Dim Avg As Double
  Dim RemItms As Long
  Dim Dif As Long
  Dim LowSwing As Long
  Dim HighSwing As Long
  Dim TempSwing As Long
  Dim Sum As Long
  
  Set LstHdr = Range("F2")
  AvgNum = Range("AvgNum").Value
  ItemNum = Range("ItemNum").Value
  Swing = Range("Swing").Value
  LSwing = AvgNum - Swing
  HSwing = AvgNum + Swing
  
  Set Rng = Range(LstHdr.Offset(1, 0), LstHdr.End(xlDown))
  Rng.ClearContents
  
  For X = 1 To ItemNum
    CurPerc = X / ItemNum
    If X < ItemNum Then               'And CurPerc - LastPerc >= 0.01 Then
      Set Rng = Range(LstHdr.Offset(1, 0), LstHdr.End(xlDown))
      Sum = Application.Sum(Rng)
      Dif = (ItemNum * AvgNum) - Sum
      TempSwing = Dif / (ItemNum - X)
      
      If TempSwing - AvgNum < 0 Then
        LowSwing = LSwing
        HighSwing = HSwing - Abs(TempSwing - AvgNum) * 4
      ElseIf TempSwing - AvgNum > 0 Then
        LowSwing = LSwing + Abs(TempSwing - AvgNum) * 4
        HighSwing = HSwing
      End If
      LstHdr.Offset(X, 0).Value = Application.RandBetween(LowSwing, HighSwing)
        
    ElseIf X = ItemNum Then
      Set Rng = Range(LstHdr.Offset(1, 0), LstHdr.End(xlDown))
      Sum = Application.Sum(Rng)
      Dif = (ItemNum * AvgNum) - Sum
      LstHdr.Offset(X, 0).Value = Dif
    
    End If
    
  Next X
  
End Sub
 
Upvote 0
ok, I fixed an error that was happening every tenth run

VBA Code:
Sub RandomizeAverage()
  Dim Cel As Range
  Dim Rng As Range
  Dim AvgNum As Long
  Dim ItemNum As Long
  Dim Swing As Long
  Dim LSwing As Long
  Dim HSwing As Long
  Dim LstHdr As Range
  Dim X As Long
  Dim Avg As Double
  Dim RemItms As Long
  Dim Dif As Long
  Dim LowSwing As Long
  Dim HighSwing As Long
  Dim TempSwing As Long
  Dim Sum As Long
  
  Set LstHdr = Range("F2")
  AvgNum = Range("AvgNum").Value
  ItemNum = Range("ItemNum").Value
  Swing = Range("Swing").Value
  LSwing = AvgNum - Swing
  HSwing = AvgNum + Swing
  
  Set Rng = Range(LstHdr.Offset(1, 0), LstHdr.End(xlDown))
  Rng.ClearContents
  Application.Calculation = xlCalculationManual
  
  For X = 1 To ItemNum
    CurPerc = X / ItemNum
    If X < ItemNum Then               'And CurPerc - LastPerc >= 0.01 Then
      Set Rng = Range(LstHdr.Offset(1, 0), LstHdr.End(xlDown))
      Sum = Application.Sum(Rng)
      Dif = (ItemNum * AvgNum) - Sum
      TempSwing = Dif / (ItemNum - X)
      
      If TempSwing - AvgNum < 0 Then
        LowSwing = LSwing
        HighSwing = HSwing - Abs(TempSwing - AvgNum) * 4
        If HighSwing < LowSwing Then HighSwing = LowSwing + 10
      ElseIf TempSwing - AvgNum > 0 Then
        LowSwing = LSwing + Abs(TempSwing - AvgNum) * 4
        HighSwing = HSwing
        If LowSwing > HighSwing Then LowSwing = HighSwing - 10
      End If
      LstHdr.Offset(X, 0).Value = Application.RandBetween(LowSwing, HighSwing)
              
    ElseIf X = ItemNum Then
      Set Rng = Range(LstHdr.Offset(1, 0), LstHdr.End(xlDown))
      Sum = Application.Sum(Rng)
      Dif = (ItemNum * AvgNum) - Sum
      LstHdr.Offset(X, 0).Value = Dif
    
    End If
    
  Next X
  
  Application.Calculation = xlCalculationAutomatic

End Sub
 
Upvote 0
Another option...

VBA Code:
Option Explicit
Sub GetNumbersToAverage()
    Dim MyTarget As Long, NumberN As Long, i As Long, j As Long
    MyTarget = Range("B1")
    NumberN = Range("B2")
    j = WorksheetFunction.RoundDown(NumberN / 2, 1)
    Range("B4", Cells(Rows.Count, "B").End(xlUp)).ClearContents
    If WorksheetFunction.IsOdd(NumberN) Then
        Cells(4, 2) = MyTarget
        i = 5
    Else
        i = 4
    End If
    With Cells(i, 2).Resize(j)
        .Formula = "=randbetween(1," & MyTarget & ")"
        .Value = .Value
    End With
    With Cells(i + j, 2).Resize(j)
        .FormulaR1C1 = "=" & MyTarget & "+(" & MyTarget & "-R[-" & j & "]C)"
        .Value = .Value
    End With
End Sub

Example 1:
Book1
ABCDE
1Given number:100
2Number of numbers:25
3
4List:100100=Average
517
616
790
871
913
1018
1162
1283
1364
1442
1578
1631
17183
18184
19110
20129
21187
22182
23138
24117
25136
26158
27122
28169
29
Sheet4
Cell Formulas
RangeFormula
D4D4=AVERAGE(B4:B59)


Example 2:
Book1
ABCDE
1Given number:79
2Number of numbers:50
3
4List:1379=Average
54
69
745
850
972
109
1114
122
139
1452
1531
1646
1756
1845
1959
2051
2174
2246
2335
2443
2576
2656
2773
2830
29145
30154
31149
32113
33108
3486
35149
36144
37156
38149
39106
40127
41112
42102
43113
4499
45107
4684
47112
48123
49115
5082
51102
5285
53128
54
Sheet4
Cell Formulas
RangeFormula
D4D4=AVERAGE(B4:B59)
 
Upvote 0
Edited to accommodate the first time you run it:
VBA Code:
Option Explicit
Sub GetNumbersToAverage()
    Dim MyTarget As Long, NumberN As Long, i As Long, j As Long
    MyTarget = Range("B1")
    NumberN = Range("B2")
    If MyTarget > 0 And NumberN > 0 Then
        j = WorksheetFunction.RoundDown(NumberN / 2, 1)
        Range("B4:B" & Application.Max(Cells(Rows.Count, 2).End(xlUp).Row, 2)).ClearContents
        If WorksheetFunction.IsOdd(NumberN) Then
            Cells(4, 2) = MyTarget
            i = 5
        Else
            i = 4
        End If
        With Cells(i, 2).Resize(j)
            .Formula = "=randbetween(1," & MyTarget & ")"
            .Value = .Value
        End With
        With Cells(i + j, 2).Resize(j)
            .FormulaR1C1 = "=" & MyTarget & "+(" & MyTarget & "-R[-" & j & "]C)"
            .Value = .Value
        End With
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,093
Messages
6,123,067
Members
449,090
Latest member
fragment

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top