Trying to run a Macro from another sheet using ActiveX button

ShannonP

New Member
Joined
Jan 5, 2023
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hello.

A sheet called “MemberData” is where all information is input and it is also where my graph displays. Information input into the Memberdata sheet is fed to a table in a sheet called “CoverageandRatescalculation”. The only sheet that will be visible to the end user is the MemberData sheet.

Each time updated information is input into the MemberData sheet I need to go to the CoverageandRatescalculation sheet to press an ActiveX control button that runs two macros. One macro resets the table and the other updates the table. The macros work perfectly when run from the CoverageandRatescalculation sheet.

Try as I might I can’t find a way to run the macros that update the CoverageandRatescalculation sheet directly from the MemberData sheet. Any assistance would be greatly appreciated.

Here are the macros that will only work when run from the CoverageandRatescalculation sheet.

First macro

VBA Code:
Sub HideRows()

StartRow = 32

EndRow = 114

ColNum = 1

For i = StartRow To EndRow

If Cells(i, ColNum).Value = "0" Then

Cells(i, ColNum).EntireRow.Hidden = True

Else

Cells(i, ColNum).EntireRow.Hidden = False

End If

Next i

End Sub




And the second Macro is

VBA Code:
Sub HideRows_based_On_Values()


For Each cell In Range("A32:A120, H32:H120")

If cell.Value = 0 Then cell.EntireRow.Hidden = True

Next cell


End Sub




And here is the Macro for the Command Button located in the CoverageandRatescalculation sheet. All macros only work when run from the CoverageandRatescalculation sheet.

VBA Code:
Private Sub CommandButton1_Click()

HideRows

HideRows_based_On_Values

End Sub
 
Last edited by a moderator:

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 and welcome to MrExcel. And Happy New Year!

For the macro to work from any other sheet, you must reference the sheet you need, I show you one way to do it in macro1 and another way in macro2.

Try this:
VBA Code:
Sub HideRows()
  Dim StartRow As Long, EndRow As Long, ColNum As Long
  Dim i As Long
  
  Application.ScreenUpdating = False
  With Sheets("CoverageandRatescalculation")
  
    StartRow = 32
    EndRow = 114
    ColNum = 1
    For i = StartRow To EndRow
      If .Cells(i, ColNum).Value = "0" Then
        .Cells(i, ColNum).EntireRow.Hidden = True
      Else
        .Cells(i, ColNum).EntireRow.Hidden = False
      End If
    Next i
    
  End With
  Application.ScreenUpdating = True
End Sub

VBA Code:
Sub HideRows_based_On_Values()
  Dim cell As Range
  Application.ScreenUpdating = False
  For Each cell In Sheets("CoverageandRatescalculation").Range("A32:A120, H32:H120")
    If cell.Value = 0 Then cell.EntireRow.Hidden = True
  Next cell
  Application.ScreenUpdating = True
End Sub

VBA Code:
Private Sub CommandButton1_Click()
  HideRows
  HideRows_based_On_Values
End Sub


Note Code Tag:
In future please use code tags when posting code.
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.
------
 
Upvote 0
Thanks so much Dante and happy new year to you as well!
When I input the macros into the MemberData sheet and run them I am getting a run time error 9 when the code reaches the line containing the sheet name "CoverageandRatescalculation". This also happens at the same point in the second macro. Any thoughts?
Thanks again!

1673010856866.png
 

Attachments

  • 1673010822469.png
    1673010822469.png
    9.1 KB · Views: 5
Upvote 0
That means that the sheet name is incorrect.
 
Upvote 0
I am getting a run time error 9
As @RoryA commented, the error is because the sheet does not exist.
I put the name of the sheet "CoverageandRatescalculation", check in your file that the sheet is actually called "CoverageandRatescalculation", check that it does not have blank spaces at the beginning or end of the name.
 
Upvote 0
Thanks so much @DanteAmor and @RoryA. I figures it out, there was a space after "CoverageandRatescalculation" so it was actually "CoverageandRatescalculation ". I updated it and it works perfectly. Many many thanks Dante, your help is much appreciated!!!
 
Upvote 0

Forum statistics

Threads
1,224,872
Messages
6,181,500
Members
453,047
Latest member
charlie_odd

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