Using Macro to add 1 to cell that is looked up

curtryan45

New Member
Joined
Jun 6, 2008
Messages
17
I have a table that will be frequently sorted. I would like to add a macro that will add 1 to a certain row. For example, say I had a macro button that was labeled "apple". When I click that macro, I want it to find the row in my table called apple and add 1 to it.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You didn't say what to do with the row number + 1 so this just shows the value in a msgbox if it finds it. This is set to look in Column J, change as necessary:

Code:
Private Sub CommandButton1_Click()
Dim c As Range
Set c = Range("J:J").Find("apple")
If Not c Is Nothing Then
    MsgBox c.Row + 1
Else
    MsgBox "Apple not found"
End If
End Sub
 
Upvote 0
Oh I meant add 1 to a certain cell, not row. Let me make an easier example.
I am doing a basketball standings. My relevant columns are Team, W, and L. Say I have a macro button labeled "Houston Rockets". When I click that button, I want it to add one to the Rockets wins, no matter where in the table it is.
 
Upvote 0
Oh I meant add 1 to a certain cell, not row. Let me make an easier example.
I am doing a basketball standings. My relevant columns are Team, W, and L. Say I have a macro button labeled "Houston Rockets". When I click that button, I want it to add one to the Rockets wins, no matter where in the table it is.
Assuming the team name will appear no more than once in the column, try this modification to HOTPEPPER's code. Adjust the column in the code to match your Team Name column.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> c = Range("J:J").Find(CommandButton1.Caption)<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        c.Offset(, 1).Value = c.Offset(, 1).Value + 1<br>    <SPAN style="color:#00007F">Else</SPAN><br>        MsgBox CommandButton1.Caption & " not found"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Assuming the team name will appear no more than once in the column, try this modification to HOTPEPPER's code. Adjust the column in the code to match your Team Name column.


<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br> <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br> <SPAN style="color:#00007F">Set</SPAN> c = Range("J:J").Find(CommandButton1.Caption)<br> <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br> c.Offset(, 1).Value = c.Offset(, 1).Value + 1<br> <SPAN style="color:#00007F">Else</SPAN><br> MsgBox CommandButton1.Caption & " not found"<br> <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>


Thank you for this good posting as I am counting different events and the code seems to be perfect. Anyway when I pasted the code, adjusted the column and started adding buttons assigning the same code to each button (luckily only two for testing :)) I get Error 424 Object needed. Can anyone point out whats the problem.

TIA
Priit
 
Upvote 0
Thank you for this good posting as I am counting different events and the code seems to be perfect. Anyway when I pasted the code, adjusted the column and started adding buttons assigning the same code to each button (luckily only two for testing :)) I get Error 424 Object needed. Can anyone point out whats the problem.

TIA
Priit
I tried the code with up to three buttons and it seems to work fine. Perhaps you should post your modified code for the two buttons you have tried and also tell us about your sheet layout (which columns are used for what? What rows are used? etc). Also, are you just working on the one worksheet?

If you have a lot of buttons, maybe something like this would be better:

In a standard Module:

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> IncreaseNumber(<SPAN style="color:#00007F">ByVal</SPAN> Team <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>)<br>    <SPAN style="color:#00007F">Dim</SPAN> c <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Set</SPAN> c = Range("J:J").Find(Team)<br>    <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> c <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>        c.Offset(, 1).Value = c.Offset(, 1).Value + 1<br>    <SPAN style="color:#00007F">Else</SPAN><br>        MsgBox Team & " not found"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Then in the Sheet Module:

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton1_Click()<br>    IncreaseNumber (CommandButton1.Caption)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton2_Click()<br>    IncreaseNumber (CommandButton2.Caption)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton3_Click()<br>    IncreaseNumber (CommandButton3.Caption)<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thanks Peter,

for your answer. The general idea is that I have to count different events happening. So I want to click the button corresponding to the event and when I have counted all of them I will have the Finish button which will add counting results into the next sheet and clear the previous results on the first sheet for the next counting.

Your answer clarified to me the parameter passing. I thought naively that the language can recognize which button was pressed and passes the parameter itself.

But now I am still stuck with modules. I put the IncreaseNumber function into Module1 and CommandButton1_Click() into the Sheet1 under the Excel Objects. Apparently thats wrong as I get the Object needed message.

As I did my last programming in Foxpro 20 years ago I dont get that object thing clearly :(

TIA
Priit
 
Upvote 0
But now I am still stuck with modules. I put the IncreaseNumber function into Module1 and CommandButton1_Click() into the Sheet1 under the Excel Objects. Apparently thats wrong as I get the Object needed message.
Assuming CommandButton1 etc is on Sheet1, then it sounds like you have put everything in the right place. So...

1. What version of Excel are you using?

2. I suggest you try setting up a new file with just one sheet with...
a) Team names (say 3 of them) in column J of that sheet
b) Three command buttons with captions matching those 3 team names
c) Code as I suggested
... and see if that works (it should add 1 to the value in column K next to the correct team each time the corresponding button is clicked)
Depending on that outcome, we might be able to work out how to proceed.

Here is my sheet after the button with caption "Houston Rockets" has been clicked 7 times, "LA Lakers" button clicked once and "Utah Jazz" button clicked 13 times.

Excel Workbook
IJKLM
1TeamWL
2Houston Rockets7
3LA Lakers1
4Phoenix Suns
5Utah Jazz13
6
Teams
 
Upvote 0
Thanks Peter again,

My problem seems to lie not in the code but somewhere with the objects.
I am using Excel 2000. Now I tried this and got similar results.
I create a button and assign a new macro to it which writes a number to the cell. It works fine, I press the button and the number appears. Now I replace writing the number with writing the button caption. I press the button and get "Object needed" message. I move the macro to the Sheet module and get "Macro not found" message.

Regards
Priit
 
Upvote 0
I create a button and assign a new macro to it ...
Does this mean you are using buttons from the 'Forms' toolbar? I was using buttons from the 'Control Tolbox' toolbar and providing code relating to that type of Command Button.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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