Help with VBA code to increment a cell in a range

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,687
Office Version
  1. 365
Platform
  1. Windows
I have a workbook that I use to track wins and losses in a game of solitaire. On the main sheet, I have a Score Button control that calls a sub that tracks the results and updates a number of statistics on that sheet.

I'd like to add a second sheet with columns for keeping track of the number of moves used for each win and loss. This minisheet shows an example of the ranges in question. The Moves column lists the number of moves it took to complete the game. In the real sheet, it goes to 400. The Wins and Losses columns record how many games took that many moves to complete. For example, of the games that took 9 moves to complete, there were 4 wins and 11 losses.

Temp.xlsx
BCD
5MovesWinsLosses
6101
7213
8308
9437
105511
116916
127613
138714
149411
151039
161103
171225
Sheet1


I want to add code to my Score sub that will look up the number of moves in the Moves column and increment the tally in either the Wins or Losses. I need help with the code that, given the number of moves and whether it was a win or a loss, will increment the correct number. For example, if I get a win in 10 moves, the sub will increment C15 from 3 to 4.

To make these ranges easy for the sub to access, I have given them each a global name. The minisheet doesn't show the range names, so here's a screen shot of the Name Manager.

1651955615936.png


Thanks
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
After some fiddling around, I came up with this code which seems to work.

VBA Code:
Sub Macro1()

Const rnWins As String = "Wins"
Const rnLosses As String = "Losses"

Dim reply As Variant
 
reply = Split(UCase(InputBox("Enter the results (W/L nn)")))
If UBound(reply) < 1 Then: MsgBox "Cancelled": Exit Sub: End

If reply(0) = "W" Then
  Range(rnWins).Cells(reply(1), 1) = Range(rnWins).Cells(reply(1), 1) + 1
Else
  Range(rnLosses).Cells(reply(1), 1) = Range(rnLosses).Cells(reply(1), 1) + 1
End If

End Sub

I need to add some error checking and other things, but I wanted to check if the basic code has any flaws or could be better.

Thanks
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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