Randomly remove a number

hartless43

New Member
Joined
Dec 28, 2022
Messages
35
Office Version
  1. 365
Platform
  1. Windows
I found a few similar posts regarding this, but not what I was looking for. I have a homemade Excel Sudoku game and it works great. Some of the levels are easy to solve and some are not. What I have discovered when playing the game, if a certain number is not available anywhere on the sheet it makes the game a little harder to play. So, I am looking for a way to randomly select a number from 1-9 such as =(RandBetween1,9), and wherever all the (Example 9's) are located all 9's will be deleted from the worksheet. This would provide for a missing number and create a more difficult game. The place where the game is played is Range("B2:J10") so the code would be for this range. The Algorithm that creates each game is not the best, but it works goes by so fast that the number cleared off would probably not be noticed.

Thanks for your help and I really like this forum.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi,
You can easily have an Event macro perform what you need ... but when should this "event" happen ???
 
Upvote 0
An Algorithm is used to create a solution. Another algorithm takes the solution, 81 numbers, copies it, and pastes all 81 numbers to where the game is played and then removes numbers based on the percentage as mentioned, and a playable game is made available, so removing a random number would take place after a playable game appears on the board. This is so quick you never see all that going on. (Maybe a flickering). Each level has a list of routines they go through before any game is placed, so a routine like I need would probably be the last routine on the list. Hope this makes sense.
Below is the list for a Medium Game.

Sub MediumRountine()
ActiveSheet.Unprotect
Range("P11").Value = "-"
SayNo
NOHints '/Clear all hints shown
ClearAllNumbers
HidePencil
AddNumbers '/This is the Algorithm. that creates a Solution.
CopyTo
MediumLevels '/Algorithm included here also.
NewGameTwo
'/RandBetween would go here.

ActiveSheet.Protect
End Sub

thanks
 
Upvote 0
Hi again,
You could test following instruction
VBA Code:
Range("B2:J10").Replace What:=Application.WorksheetFunction.RandBetween(1, 9), Replacement:=""
 
Upvote 1
Solution
Jame006, Thanks that works. It is the last thing to do. If you are familiar with Sudoku, you may see at the bottom of the puzzles are the numbers from 1 - 9 and below those numbers are very small numbers that tell you how many of the numbers 1 - 9 are left to play. My Sudoku has this feature also and I can tell which number was removed because it will show a 9, but other numbers may not have been used normally and they will show a 9, so thanks again. Sorry, this took so long to replay, but just got off a long road tour of beautiful East Tennessee back roads.
 
Upvote 0
James006, Sorry, this works exactly as you wrote, but, but, but removing 9 numbers from the game creates a big problem. Removing 9 numbers causes the game level to go to the next highest level. and it wasn't designed to do that, because I have Macro Buttons for each level name on them and the level name is written in a cell on the worksheet. So, I will have to temporarily remove it until I can figure out what to do next.
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,318
Members
453,032
Latest member
Pauh

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