449,054 Members | 1,334 Online
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

# VBA Minesweeper - Take advantage of your Mouse in an Excel's Worksheet.

 Expert Mod 15k+ P: 31,662 Kad, You will find the Workbook & Worksheet specific events in the dropdown on the right (the event list) if you select the Workbook or Worksheet from the dropdown on the left (the object list). By default this is set to (General), which will show (Declarations). Apr 30 '08 #2

 P: 1 I am currently trying to program minesweeper for a class. I have one comment on how you generate the bombs. You are using a Do-Loop and loop until j = 10. The way you have it, If the random function tries to put a bomb in a cell that already has a bomb, it simply won't. But the program will still go through j = j + 1, hence the counter will go up. So technically when you run this program, you might not get 10 bombs every time, because if the random function lands a bomb on a bomb, no bomb will be placed and the counter will still go up. I hope this isn't too confusing- I'm very new at VBA and can not explain it well. But I did come up with a slightly different random bomb-placing program that will consistently place 10 bombs. Anyway, tell me what you think Expand|Select|Wrap|Line Numbers Randomize For n = 1 To 10 Do     i = Int(10 * Rnd) + 2     j = Int(10 * Rnd) + 2       If mine(i, j) <> "X" Then         mine(i, j) = "X"         Exit Do     End If Loop Next n Dec 13 '08 #3

 Expert Mod 15k+ P: 31,662 Interesting point (and it does make perfect sense by the way). I would only suggest that the Do loop should be indented as the For loop is. Welcome to Bytes! Dec 16 '08 #4

 Expert 100+ P: 1,295 Sorry i didnt answer before. Cmeier7, yes, it wouldn't make sense if the j = j+1 were outside the IF, but its in it, so only when the bomb is planted it'll add 1 to j. Just as you said it should be, it is. About your code. I think is another nice way to do it. ^.^ happy new year (a little bit late) Jan 16 '09 #5

 P: 61 I like this code, pretty cool. One small hickup I found, if you hit the X too many times in a row the array doesn't get cleared and all the cells end up being X's. this is what I did to remedy that.. Expand|Select|Wrap|Line Numbers If Mines(R1, R2) = "X" Then     Target.Interior.ColorIndex = 3     Erase Mines     MsgBox "game over"     Generate     Exit Sub End If   I added the erase mines to clear the array. Works perfecly now. Thanks for the awsome code:-) Oct 28 '10 #6