VBA - Find all the White Cells and populate them with a Random Value.

Tim77

New Member
Joined
Jan 25, 2020
Messages
6
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
Hi all,

I am Junior on VBA and I can not make it with the below task. I have a huge worksheet with White and Grey Cells. My task is to find all the white Cells and Populate them with a Random value. Can you advise?

Thanks in advance,
 
How about
VBA Code:
Sub Tim77()
   Dim Cl As Range
   Const MinNum As Long = 1
   Const MaxNum As Long = 1000
   
   ActiveSheet.Unprotect "Password"
   Randomize
   For Each Cl In Range("A2:X100")
      If Cl.Interior.Color = vbWhite Then
         Cl.Value = Int((MaxNum - MinNum + 1) * Rnd() + MinNum)
      End If
   Next Cl
   ActiveSheet.Protect "Password"
End Sub
Change the password to suit
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
How about
VBA Code:
Sub Tim77()
   Dim Cl As Range
   Const MinNum As Long = 1
   Const MaxNum As Long = 1000
  
   ActiveSheet.Unprotect "Password"
   Randomize
   For Each Cl In Range("A2:X100")
      If Cl.Interior.Color = vbWhite Then
         Cl.Value = Int((MaxNum - MinNum + 1) * Rnd() + MinNum)
      End If
   Next Cl
   ActiveSheet.Protect "Password"
End Sub
Change the password to suit
...some error with the comment "The password you supplied is not correct"
 
Upvote 0
You need to change to the password you used when protecting the worksheet.
 
Upvote 0
Only the Grey Cells are been protected with Password (that I do not know) and the White ones are the ones that I have to populate.

So I made a Loop

For Each Rng In ActiveSheet.Range("N9:CJ10000")
If Rng.Interior.ColorInder <> 15 Then
Rng.Value = Int ((MaxNum- MinNum +1)* Rnd +MinNum)
End If
Next Rng


The issue is that some random values are been populated and assigned and after some seconds a Run-time Error 1004 appears on the Rng.Value = Int line. Talking about protected sheet etc...
 
Upvote 0
after some seconds a Run-time Error 1004 appears on the Rng.Value = Int line. Talking about protected sheet etc..
That means you are trying to change a locked cell. Either remove the sheet protection before running, as I showed, or make sure that all white cells are unlocked.
 
Upvote 0

Forum statistics

Threads
1,215,811
Messages
6,127,017
Members
449,351
Latest member
Sylvine

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