Relative addressing from named cell in VBA

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,691
Office Version
  1. 365
Platform
  1. Windows
I have a little macro that I have assigned to a command button. It's basically working except for one detail. There are several named cells in the worksheet. Each one is a column header. I need to perform arithmetic on the cell immediately below each header. If the header is in B4, I need to do arithmetic on B5.

I need something like this:

Public Sub Tally()
Dim row as integer, col as integer
row=activesheet.cell("HdrA").row+1
col =activesheet.cell("HdrA").column
activesheet.cell(row, col) = activesheet.cell(row, col) + 1
End Sub

The above doesn't work. Can anyone help me with the correct syntax?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this

Code:
Public Sub Tally()
With Range("HdrA").Offset(1)
    .Value = .Value + 1
End With
End Sub
 
Upvote 0
Wow!!! That was fast and it works perfectly. Amazing. Thanks so much.

Can I ask one unrelated question?

The actual math I need to do depends on the results of an experiment. If I get one result, I do math of one set of cells. If I get another result, I do math on another set of cells.

I need a way to ask the user to specify the result, such as "High" or "Low". I can use MsgBox and ask "Was the result 'high'". The suer can select yes or no. But I would prefer to put up a message that has my choices ("High"/"Low") and allow the user to select one. It would be great if I could assign a keyboard character to each choice.

Thanks
 
Upvote 0
That's what I was afraid of. I couldn't find anything about defining my own buttons.

I defined a second command button. I now have one labeled "High" and one labeled "Low". Is there a way to have the command button pass a parameter to the macro?

I tried changing the command button assignment statement to:

Code:
Test.xlsm!Sheet1.Tally(1)

but that got an error message about the formula being too complex.

I rewrote the macro to:

Code:
Public Sub Tally(Result As Boolean)
 
If Result Then
  With Range("TestHigh").Offset(1)
    .Value = .Value + 1
  End With
Else
  With Range("TestLow").Offset(1)
    .Value = .Value + 1
  End With
End If
 
End Sub

Is there a way to pass a parameter?

If not, I'll have each one call a different macro and have that one call the common macro with a parameter.

Thanks
 
Upvote 0
Try like this. In a regular Module

Code:
Public Result As Boolean
Public Sub Tally()
 
If Result Then
  With Range("TestHigh").Offset(1)
    .Value = .Value + 1
  End With
Else
  With Range("TestLow").Offset(1)
    .Value = .Value + 1
  End With
End If
 
End Sub
And in the form's code module use code like

Code:
Private Sub CommandButton1_Click()
Result = True
End Sub

Private Sub CommandButton2_Click()
Result = False
End Sub
 
Upvote 0
In fact it should be more like

Code:
Private Sub CommandButton1_Click()
Result = True
Unload Me
Call Tally
End Sub

Private Sub CommandButton2_Click()
Result = False
Unload Me
Call Tally
End Sub
 
Upvote 0
Do you like that better than passing a parameter to Tally:

Code:
Private Sub CommandButton1_Click()
Unload Me
Call Tally True
End Sub
 
Private Sub CommandButton2_Click()
Unload Me
Call Tally False
End Sub

What does the Unload command do?
 
Upvote 0
That would need to be

Code:
Call Tally(False)

Which way you do it is largely down to personal preference.

Unload Me removes the userform from memory. If you wanted to reuse it then you could use Me.Hide instead.
 
Upvote 0
Is there a way within the macro to get the name of the button that was clicked?
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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