Two ActiveX Command Buttons, but Only One Works

reberryjr

Well-known Member
Joined
Mar 16, 2017
Messages
714
Office Version
  1. 365
Platform
  1. Windows
I have 2 ActiveX command button on a worksheet. The cmd_AddNewProj button works as expected, but the cmd_Refresh button does not work. The code works when I hit F5, but when I click the actual button, a 2nd image of the button pops up. I'm not in design mode. Thoughts?

VBA Code:
Private Sub cmd_AddNewProj_Click()

frm_NewJob.Show

End Sub
Private Sub cmd_Refresh_Click()

Dim m As Workbook
Dim mS As Worksheet, mI As Worksheet
Dim mILR As Long

Set m = ThisWorkbook
Set mS = m.Sheets("Summaries")
Set mI = m.Sheets("Inventory")

mILR = Sheets("Inventory").Range("D" & Rows.Count).End(xlUp).Row

mS.Activate

With ActiveSheet
    .PivotTables("InvSummary").ChangePivotCache ActiveWorkbook.PivotCaches.Create(xlDatabase, SourceData:=m.Sheets("Inventory").Range("A1:R" & mILR))
End With

End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
If you set a breakpoint at the first line of code in Sub cmd_Refresh_Click() will it execute to that point if you press the button? It it will, are you able to then single-step (F8) through the code to see what is happening?
 
Upvote 0
ActiveX controls are flaky at best but I suspect that activating a different sheet at the same time as clicking the button doesn't help. Try changing this:

VBA Code:
mS.Activate

With ActiveSheet
    .PivotTables("InvSummary").ChangePivotCache ActiveWorkbook.PivotCaches.Create(xlDatabase, SourceData:=m.Sheets("Inventory").Range("A1:R" & mILR))
End With

to this:

VBA Code:
mS.PivotTables("InvSummary").ChangePivotCache ActiveWorkbook.PivotCaches.Create(xlDatabase, SourceData:=m.Sheets("Inventory").Range("A1:R" & mILR))
 
Upvote 0
Solution
ActiveX controls are flaky at best but I suspect that activating a different sheet at the same time as clicking the button doesn't help. Try changing this:

VBA Code:
mS.Activate

With ActiveSheet
    .PivotTables("InvSummary").ChangePivotCache ActiveWorkbook.PivotCaches.Create(xlDatabase, SourceData:=m.Sheets("Inventory").Range("A1:R" & mILR))
End With

to this:

VBA Code:
mS.PivotTables("InvSummary").ChangePivotCache ActiveWorkbook.PivotCaches.Create(xlDatabase, SourceData:=m.Sheets("Inventory").Range("A1:R" & mILR))
@RoryA simple change, but it appears effective. Many thanks!!
 
Upvote 0

Forum statistics

Threads
1,223,157
Messages
6,170,418
Members
452,325
Latest member
BlahQz

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