VBA Code to Randomly Replace a Cell Value in a Table Range

hassanleo1987

Board Regular
Joined
Apr 19, 2017
Messages
56
Hi,

I have a table range to 30 columns and 100 rows with some blank cells in it. The cells values are 6 , 8 , 10 , SL , VC and RE other than the blanks.
Lets suppose the current sum of table range mentioned above is 6000. I need a VBA code that will target the cells with numerical values only ignoring the blanks and other values i.e. SL, VC or RE, change it to a specific value of 8 until the sum of table range is 5560.

The targeted cells that are being changed must be random and loop should continue until the target sum of 5560 is achieved.

Target sum value is in a fixed range along with specific value "8" . (Fixed cell references in the same sheet for reference in VBA code).

Actual application is a big table of more than 100 columns and 6000 rows, doing it manually takes a lot of time!

Can somebody please help with this!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How do we know that the result is even possible?

How many cells in the 30 column x 100 row range contain numbers?
 
Upvote 0
How do we know that the result is even possible?

How many cells in the 30 column x 100 row range contain numbers?
The way I understand, It is doable in VBA is this!

Suppose we have the fixed range of 30 columns x 100 rows, the total cells will be fixed 3000 cells, each having a unique reference.

The first part of the code will exclude the cell which does not fit our criteria i.e. if the cell value is SL, VC, RE or blank, it will be ignored. Lets suppose we have 300 such cells. Remaining cells will be 2700 with numerical values of 6, 8 and 10.

Since the sum of table range is already fixed along with the target sum to be achieved, a simple calculation is need to determine the number of cell to be changed in order to achieve the goal.

Let suppose out of remaining 2700 cells, 2000 cells contain the value of 10 which can be decreased to the specific value of 8 in order to achieve the target sum.

The target sum being 5560 gives us the total negative value to be applied. 6000 - 5560 = 440

Since we are going to replace 10 with 8, so a decrement of 2 will be applied on randomly selected cells.

440 / 2 = 220 will give us the total number of random cells to be selected out of 2000 cells with cell value of 10, where the decrement will be applied.

The code will randomly select 220 cell out of 2000 cells that fits our criteria and change the cell values from 10 to 8, achieving the target sum of 5560.


This is my understanding on how this code should work.
I hope this explains the problem and solution for anybody to try and write a code for it.
 
Upvote 0
Yes, I can do that calculation. ;)

You said that your range contains blanks and certain text values.
My question was
The number of cells containing numbers is not fixed, hence need to be calculated and identified by the code either by using a function or something like ISNUMBER... I don't know what is the best way to do it. But a loop should do it. checking each cell based on the number criteria.

This way it will work faster by identifying and recording the cells with only numbers, ignoring text and blanks.
 
Upvote 0
I don't know if array are possible in VBA but an array will help since the table range is fixed and an array will iterate through every cell in it, finding the cells based on the number criteria and then using a randomization code to implement the change.
 
Upvote 0
Let suppose out of remaining 2700 cells, 2000 cells contain the value of 10 which can be decreased to the specific value of 8 in order to achieve the target sum.

The target sum being 5560 gives us the total negative value to be applied. 6000 - 5560 = 440
I don't follow that. :unsure:
If you have 2000 cells containing 10 you start with 20,000. Reducing them all to 8 still leaves you way over your target of 5560.

Perhaps there is a typo there that you can clarify, but that is the sort of (impossible) thing I was trying to get at.
 
Upvote 0
@hassanleo1987
Let's say, the initial sum is 1000 and the target sum is 980 which is lower than the initial sum. Do you want only some 10 changes to 8, or it's fine that some 6 also changes to 8?
 
Upvote 0
I don't follow that. :unsure:
If you have 2000 cells containing 10 you start with 20,000. Reducing them all to 8 still leaves you way over your target of 5560.

Perhaps there is a typo there that you can clarify, but that is the sort of (impossible) thing I was trying to get at.
Sorry, I think the numbers are confusing. the current of table range 6000 is just assumed along with the target sum for easy calculation of decrement and number of cells to be targeted.

Please see this mini sheet where I have shown before and after change.

aa.xlsx
ABCDEFGHIJKLM
1
2Original Table
3SR #ABCDEFGHIJ
411061010101010101010
521010610SL10108108
636101010101010101010
7410SL1061010RE610
85101010106106101010
96108101010101010RE
1078101010VC1010101010
11810101010810VC1010
129108101010101010106
131010101010101010101010
14
15Current sum866
16Target sum850
17Sum Variance-16
18Current Cell with Value of 1077
19Number of cell to be randomly changed to 88
20
21After implementing the random change
22SR #ABCDEFGHIJ
23186108101010101010
242810610SL1088108
253610810881010810
26410SL10610810RE610
275810810686101010
28610888101010810RE
2978101010VC10810810
308101081010810VC1010
3198810810101010106
321010108108101010810
33
34Sum850
35
Sheet2


The 1st table shows the target range or table.
The 2nd table shows the result where 8 cells where changed from 10 to 8 get the total sum to target sum of 850.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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