Change the text in a button with conditions - excel

I have a small piece of VBA code that allows me to expand and collapse rows by clicking a button (by assigning the Macro to the button):
Sub Expand()
Range("4:4").EntireRow.Hidden = Not (Range("4:4").EntireRow.Hidden)
Range("9:9").EntireRow.Hidden = Not (Range("9:9").EntireRow.Hidden)
End Sub
Currently, I have just manually typed in the text "EXPAND" onto the button, however I would like the text top dynamically change to say "COLLAPSE" when the rows are visible and "EXPAND" when the rows are hidden.
Thank you.

If you are using Form Controls Button then assign this macro (in regular/standard module) to the button
Edited as suggested by #KekuSemau in the comments on the question
Sub Macro1()
With ActiveSheet.Shapes("Button 1")
If .TextFrame.Characters.Text = "Expand" Then
.TextFrame.Characters.Text = "Collapse"
ActiveSheet.Range("4:4,9:9").EntireRow.Hidden = False
Else
.TextFrame.Characters.Text = "Expand"
ActiveSheet.Range("4:4,9:9").EntireRow.Hidden = True
End If
End With
End Sub
If you are using ActiveX Controls Command Button then Paste this macro in the relevant sheet VBA module. Make sure the button name is correct.
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "Expand" Then
CommandButton1.Caption = "Collapse"
ActiveSheet.Range("4:4,9:9").EntireRow.Hidden = False
Else
CommandButton1.Caption = "Expand"
ActiveSheet.Range("4:4,9:9").EntireRow.Hidden = True
End If
End Sub

Related

Adding a tooltip to a ComboBox in Excel with VBA

I've added two Dropdown (aka ComboBox) to a Sheet
Using this piece of code I can access the Dropdown but how can I add a tooltip on the Dropdown?
The best solution would be to show a different text for every item but if there is only an unique tooltip for the whole dropdown I can change it after selecting every item.
Sub DropDown1_Change()
Dim s As Object
Set s = ActiveSheet.Shapes(Application.Caller)
s.ToolTip = "Example"
Debug.Print s.ControlFormat.Value
End Sub
This is a forms combobox, it would not have a tooltip capability, but you can make it look like it has a tool tip.
Place a hyperlink with a screen tip underneath the combobox, when you mouse over the combobox the screen tip will pop up.
You can place the hyperlink on many cells if you intend on stretching the combobox over many cells.
Like this
Here is a 20 second clip
http://www.screencast.com/t/ZbkEOyXntItk
You can get the range of the combobox with application.caller.
Assign each combobox to this macro, then you would only need one macro.
Sub DoIt()
Dim r As Range
r = ActiveSheet.Shapes(Application.Caller).TopLeftCell
ActiveSheet.Hyperlinks.Add Anchor:=r, Address:=r, ScreenTip:="5435435345", TextToDisplay:="ddddddddddddddddddd"
End Sub
Following is my code:
Private Sub ComboBox1_Click()
' Adding new items
ComboBox1.AddItem ("S")
ComboBox1.AddItem ("M")
If ComboBox1.Text = "S" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Strong" ' Add your text here
End With
End If
If ComboBox1.Text = "M" Then 'Add your dropdown item here
With Me.ComboBox1
.ControlTipText = "Moderate" ' Add your text here
End With
End If
End Sub

Disable button using Excel VBA

I want to disable a button with VBA code like this:
ActiveSheet.Shapes("Button 1").ControlFormat.Enabled = False
I tried:
Set b1 = ActiveSheet.Buttons("Button 1")
b1.Enabled = False
And:
Me.Shapes("Button 1").ControlFormat.Enabled = False
My button name is correct, because it doesn't give me an error message, so the code is completely run through.
After this script I can click on that button and the assigned macro runs. Nothing should happen when I click on it.
Disabling a Form button (not talking ActiveX here) does not prevent the assigned macro to run and does not gray out the button. The code below does exactly that based on the version got from Excel. If you did not assign a name to your Form button, you can also use (Buttons(1).
If Excel version = 16 or higher the button is "enabled" by making it black and assigning my macro, else the button is "disabled" by making it gray and assigning no action to it.
Code can e.g. reside in Private Sub Worksheet_Activate() within sheet "Test Sheet"
If Application.Version < 16 Then
Worksheets("Test Sheet").Buttons("button_name").Font.Color = 8421504
Worksheets("Test Sheet").Buttons("button_name").OnAction = ""
Else
Worksheets("Test Sheet").Buttons("button_name").Font.Color = 0
Worksheets("Test Sheet").Buttons("button_name").OnAction = "'Name of the workbook.xlsm'!my_macro_name"
End If
Probably you are using ActiveX Button. Try this:
Sheets("Sheet1").CommandButton1.Enabled = False '--->change sheet name as required
EDIT:
______________________________________________________________________________
For a Form control Button the following line
ActiveSheet.Shapes("Button 1").ControlFormat.Enabled = False
disables the button i.e. click event will no longer work but the appearance of the button does not change which gives an impression that the button is still active. So work around for that is to change the color of the text of the button as follows:
Sub disable_button_2()
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = False '---> Disable the button
.TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With
End Sub
And to bring back button to its original state write:
Sub activate_button_2()
Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
With myshape
.ControlFormat.Enabled = True '---> Enable the button
.TextFrame.Characters.Font.ColorIndex = 1 '---> Highlight button label
End With
End Sub
I suggest to create a shadow button/shape with exactly same size/position, but different color (fill and/or text to your liking) and no macro/action attached. Then just change the .visible property of your primary shape. Visible = button is active; not visible button is e.g. grayed out and has no action/is passive.
Only tested on Excel 2016 x86
I continued to receive errors utilizing .ControlFormat. solutions.
After much searching I found another solution that worked great for my needs of disabling Shapes/Buttons.
To mimic the .Enabled property of a uf control, you might toggle the .OnAction property of a shape.
Function ShapeIsEnabled(aShape As Shape) As Boolean
ShapeIsEnabled = (aShape.OnAction <> "")
End Function
Sub EnableShapeMacro(aShape As Shape)
aShape.OnAction = aShape.AlternativeText
End Sub
Sub DisableShapeMacro(aShape As Shape)
aShape.AlternativeText = aShape.OnAction
aShape.OnAction = vbNullString
End Sub
Note the use of the .AlternativeText property to store the macro name.
source: mikerickson
https://www.excelforum.com/excel-programming-vba-macros/1267897-disable-action-of-macro-enabled-shape.html#post5080833

Limit to only 1 selected checkbox

I have imported a table with check-boxes from Access to Excel. Is it possible to set the check-boxes where only one check-box can be selected from that imported table when using Excel?
In the comments Jeeped made an excellent point that radio buttons already have the functionality that you are looking for. On the other hand -- if you prefer the aesthetics of checkboxes then you can certainly use them. I created a userform with two checkboxes in a frame (and no other controls in the frame) and also included a label for displaying the chosen option. The following code deselects all other checkboxes in the frame when one is selected. I used a non-local Boolean variable to circumvent the other checkbox's event handlers while they were being changed to avoid a sort of echo effect I ran into where the events were firing when I didn't want them to (perhaps there is a less kludgy way to do that). The code easily extends to any number of checkboxes in a grouping frame.
Dim selecting As Boolean 'module level variable
Private Sub SelectOne(i As Long)
Dim c As Control
selecting = True
For Each c In Frame1.Controls
If c.Name <> "CheckBox" & i Then c.Value = False
Next c
DoEvents
Label1.Caption = i & " selected"
selecting = False
End Sub
Private Sub CheckBox1_Click()
If Not selecting Then SelectOne 1
End Sub
Private Sub CheckBox2_Click()
If Not selecting Then SelectOne 2
End Sub
I think this works best and its much easier - at least for a few boxes - for more you could write some formulas in excel and drag down then copy as values and copy paste text from excel into vba. Anyway, here it's how I did it:
I went and created code under each button - quite basic
Private Sub DateCheckBox1_Click()
If DateCheckBox1.Value = True Then
DateCheckBox2.Value = False
DateCheckBox3.Value = False
End If
End Sub
Private Sub DateCheckBox2_Click()
If DateCheckBox2.Value = True Then
DateCheckBox3.Value = False
DateCheckBox1.Value = False
End If
End Sub
Private Sub DateCheckBox3_Click()
If DateCheckBox3.Value = True Then
DateCheckBox2.Value = False
DateCheckBox1.Value = False
End If
End Sub

where will I put the codes when button is clicked?

i've created a macro to create a button when the sheet is activated. then i called it using the codes below: But everytime i go to different worksheet then back again to the sheet containing this button the macro does its job. i just want the macro to work when i clicked the button
Private Sub Worksheet_Activate()
Call sortData
End Sub
here's the code for macro that i've created:
Sub sortData()
'
'
'
'
ActiveSheet.Buttons.Add(689.25, 59.25, 133.5, 30).Select
Selection.OnAction = "sortData"
Selection.Characters.Text = "Sort Data"
With Selection.Characters(Start:=1, Length:=28).Font
.Name = "Times New Roman"
.FontStyle = "Bold"
.Size = 12
End With
Range("A1").Select
End Sub
now my problem is where will i put the codes shown below when this button is click? or simply how can i make this button worked?? i tried to put the codes in the same sheet where i called the macro but it is not working.
Sub sortData_Click() 'did i call the button right? but it is not working when i us it
'codes here
End Sub
in the VBA Editor, click insert new module (or you can just put this in the same module where your sortData is) then write this code into your new module:
Sub procedureName() 'procedure name is exactly the same as what you named in Selection.OnAction = "sortData"
'codes here
End Sub
what you actually done is making the sortData() call itself because you have set the Selection.OnAction = "sortData" ->>> change this name to whatever procedure name you want to perform

VBA expand/collapse rows with same macro button

We have a trivial problem regarding how to run a simple macro button. The purpose of this button is two-fold: expanding a row and collapsing a row.
1 on pressing the button this VBA command is initiated:
Sub Macro7()
Rows(7).ShowDetail = True
End Sub
This command expands row 7.
2 on pressing the button again (whilst the row is expanded), this VBA is initiated:
Sub Macro7()
Rows(7).ShowDetail = False
End Sub
This collapses the row.
Is there a way to link a button to two macros?
Thanks in advance!!!
M
Sub Macro7()
With Rows(7)
.ShowDetail = Not .ShowDetail
End With
End Sub
No need to. Just adjust your macro to check the current state of your row (collapsed or expanded) and act accordingly:
Sub ExpandOrCollapse()
Rows(7).ShowDetail=IIF(Rows(7).ShowDetail,False,true)
End Sub
I tried above answers and it didn't work for me. Below is the code that works:
Sub rowExpanded()
Rows("7:7").Select
Selection.EntireRow.Hidden = IIf(Selection.EntireRow.Hidden, False, True)
End Sub
Try this
Dim rowExpanded As Boolean
rowExpanded = Rows(7).ShowDetail
If rowExpanded = True Then
Rows(7).ShowDetail = False
Else
Rows(7).ShowDetail = True
End If
Try using a Command Button (ActiveX Control). Use the button caption to identify the toggle state.
Private Sub CommandButton1_Click()
If CommandButton1.Caption = "-" Then
ActiveSheet.Outline.ShowLevels Rowlevels:=1, ColumnLevels:=1
JobDescriptionToggleButton.Caption = "+"
Else
ActiveSheet.Outline.ShowLevels Rowlevels:=8, ColumnLevels:=8
JobDescriptionToggleButton.Caption = "-"
End If
End Sub

Resources