Locating Command Buttons in Excel VBA - excel

I am maintaining a large spread sheet at work and there are ActiveX command buttons throughout the code. One sheet I am currently working on has almost 100 of these. I have made sure the button itself is visible as some of the buttons are hidden/unhidden depending on the flow of the code.
Is there a way I can find whereabouts on the sheet itself the button is located that the VBA code is pointing to? Here is a snippet:
enter code here
Private Sub CommandButton4_Click()
Application.Goto Range("Add_Trainees"), True
CommandButton3.Visible = True ' unhides menu button
CommandButton81.Visible = True ' hides SC1
CommandButton97.Visible = True ' hides SC2
End Sub
I am trying to find where on the sheet where command buttons 3, 81 and 97 are located.
TIA for any help/suggestions.

I think pnuts provides the correct way to find out where the buttons are.
I would also suggest to open the panel from "Developer" -> "Properties". In that panel, you can check out all the ActiveX Controls, including the command buttons, in the list.

Related

VBA Enable only worksheet scrolling while UserForm is active

Is there a way to enable only the worksheet scrolling while userform is active?
.show (vbmodeless)
Doesn't work for my needs as it enables all editing in worksheet, but I need to let the end user to only scroll the worksheet.
Could someone please share an answer to my question, please?
I suggest you put a vertical scroll bar on the form itself and use changes in the value of the scroll bar to scroll the worksheet using vba code. The form can then be modal and you retain control of the worksheet.
For example, I put a scrollbar on a modal form and ran this code:
Option Explicit
Private Sub ScrollBar1_Change()
Debug.Print ScrollBar1.Value
Sheet1.Rows(ScrollBar1.Value + 1).Select
End Sub
Which worked fine. You will need to adjust the settings of the scroll bar to be sensible in relation to the size of the worksheet and the way you want the user to interact, but if scrolling is the only function you want to allow, then perhaps this could work.

I want to create an add button

I am newbie in excel and I really need a button that if pressed adds a certain value to the cells I selected, is that possible?
You can add buttons to Excel by enabling developer mode:
On the File tab, go to Options > Customize Ribbon.
Under Customize the Ribbon and under Main Tabs, select the Developer check box.
Then on the developer ribbon you can click insert, then select a button to draw onto the sheet.
You will then be given the option to assign a macro to the button (Also later accessible by right clicking the button).
As for the VBA code of the macro, you would need to be more specific about what functionality you require, is the 'certain value' to be added to the selected cells always the same value?
Someone answered me in two minutes on another site with this perfect code for what I wanted, thanks a lot sir. It works like magic.
Sub Add_to_Selection()
If Not IsNumeric([E4]) Then
Exit Sub
Else
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value + [E4].Value
Next cell
End If
End Sub

How to Show/Hide all comments on an Excel sheet (but not impact other sheets)

I want to have a button Show/hide all comments on the active Excel sheet only. I implemented and am fairly happy with this solution (using Application.DisplayCommentIndicator):
Simple VBA Show/Hide Excel Comments problems
The only issue is that this solution shows/hides comments on every sheet open in the application. I need it to only operate on the active sheet (IE. the one with the button) This is especially inconvenient because the other sheets (in other workbooks) don't have the Show/Hide button.
I have a semi-fix for your issue. Let's say if the user pressed your button to hide the comments, which is located in Sheet1, but then decides to switch to another worksheet, all comments would be activated again.
Problem: if the user switches back to Sheet1, the comments will be hidden and the user would have to press unhide regardless of their previous selection if they would like to see the comments:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sh.Name = "Sheet1" Then
Application.DisplayCommentIndicator = xlCommentIndicatorOnly
Else
Application.DisplayCommentIndicator = xlCommentAndIndicator
End If
End Sub

Run Macro When ComboBox is Clicked

I need help with figuring out how to make a macro run when I click the ComboBox. For example, the ComboBox is pulling in a list from another column, if that column changes, I want to see the updated list when I click the ComboBox for this first time. Right now it is running the macro after I click the ComboBox and then click on an option that already exists. Once I reopen, then I see the updated list I want to see the first time a user clicks. Any help is appreciated, thank you so much!!!!
Sub costcenterdup()
'
' costcenterdup Macro
Application.ScreenUpdating = False
With Sheets("Dollars")
.Range("K9:K" & .Cells(9, "K").End(xlDown).Row).Copyy
Destination:=Sheets("LookUp").Range("E2")
End With
With Sheets("LookUp")
.Range("$E2:E" & .Cells(.Rows.Count, "E").End(xlUp).Row).RemoveDuplicates
Columns:=1, Header:=xlNo
End With
With Application.Worksheets("LookUp")
.Range("E2:E5000").Sort Key1:=.Range("E2")
End With
Range("C5").Select
Application.ScreenUpdating = True
End Sub
There are two types of combo boxes that you can add to a sheet.
One of them is a form combo box, which can be accessed from the "Forms" toolbar.
The other one is the ActiveX combo box, which can be accessed from the "Control Toolbox" toolbar.
A very good explanation of these is here (link is courtesy of #Ralph): http://peltiertech.com/forms-controls-and-activex-controls-in-excel/
To the first one you can only assign one macro when the combo box changes.
But to the ActiveX ComboBox you can assign several. If you add it to the GotFocus event then it will run the macro every time the box gets focus:
Private Sub ComboBox1_GotFocus()
'Add code here
End Sub
But if I correctly understand your question, that you want to have the combo box have the data in it from a column – then you need the first version of combo box (the form one) and simply right-click on it, select "Format control..." and on the Control tab set the cells you want the data filled with. It will automatically update the combo box for you and you will always see the values from the cells. There is no need for a macro in this case. See the below image:

Is there a Macro to hide all sheets in a workbook when a certain cell is equal to 100%?

I understand the logic behind this but I'm unsure how to right the macro. I import up to 63 sheets of data into excel.
ALL of the sheets have a status in Column B Row 9. I would like to make a macro to hide all sheets in the workbook when B9 = 100%
If Worksheet.Column.B, Row.9= 100%
Worksheet.hide
Open the VB Editor ALT+F11. Under Microsoft Excel Objects right click Insert --> Module. Paste in the following code.
Option Explicit
Public Sub HideSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Range("B9").Value = 1 Then
ws.Visible = xlSheetHidden
End If
Next ws
End Sub
The Option Explicit setting forces you to declare variables. I include this because, on top of good coding practice, it has saved me hours of debugging only to find I spelled a variable name wrong (such errors are captured before the code begins when adding this line).
The basic principle is that the code uses a For..Each loop to iterate through each worksheet in the workbook. IF cell B9 is 1 (corresponding to 100%) then the worksheet's Visible property is set to xlSheetHidden which hides the sheet. Sheets with this visible property can be unhidden if the user right-clicks along the worksheet tabs and selects Unhide.... If you don't want users to be able to unhide the sheets, you can set it to xlSheetVeryHidden which hides the sheet and disabled unhiding the sheet from the UI.
To run this macro you can click anywhere inside the code and click the button that looks like play (this is the Run Sub/Userform button) or you can press F5.
I would recommend setting the macro to a keyboard shortcut, or if you prefer to a button located somewhere on the worksheet.
To assign the macro a keyboard shortcut:
Under the Developer tab select Macros (or simply press ALT+F8) to display the Macro window
Under Macro name: select the name of your macro (HideSheets in this example)
Click Options...
Put the key in that you want to press to run the macro (in this case I chose CTRL+h for hide)
Select OK
Test by pressing the keyboard combination you specified
Additionally, you can assign a macro to run when a button on the worksheet is clicked, to do this:
Under Developer go to the Insert dropdown
Under ActiveX controls, select the command button
Draw the button anywhere on the page
Right click the button --> CommandButton Object --> Edit
Change the button text to whatever you want (like Hide Sheets for example)
Double click the button to open the code, you should see a Sub entitled CommandButton1_Click()
Type HideSheets into the subroutine like this (or whatever the name of your subroutine is)
Private Sub CommandButton1_Click()
HideSheets
End Sub
Exit design mode by clicking Design Mode under the Developer tab
Click the button to ensure the macro functions
Building on the answer from Soulfire, you can run the automatically anytime the value in the cell changes value. Just put the following code under the worksheet (not the module), which will run the macro 'HideSheets' whenever the value in cell C9 changes.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$9" Then
Call HideSheets
End If
End Sub

Resources