VBA expand/collapse rows with same macro button - excel

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

Related

Checkbox to Hide/Unhide Row Not Unhiding

I have a form control checkbox and the following code to hide and unhide row 10.
It hides but does not unhide.
Sub CheckBox1_Click()
If Range("C84").Value = True Then
Rows("10:10").EntireRow.Hidden = False
Else
Rows("10:10").EntireRow.Hidden = True
End If
End Sub
The code works perfectly.
Furthermore, if the value of cell C84 is not used elsewhere, the same can be obtained without using LinkedCell:
Sub CheckBox1_Click()
Rows("10:10").EntireRow.Hidden = Not CheckBox1
End Sub
For it to unhide c84 value should be false as long as it is true it will be hidden only

How can I assign a dynamic range (of rows) to a checkbox in vba?

I'm creating a workbook in excel and using activeX checkboxes to hide/unhide some rows each time as a way to enable or disable them. For that I use VBA code. However in that code I specify a static range of rows. The problem is that if I insert a new row, everything is now offset and i have to manually rewrite all the intervals. Is there a way to do this dynamically.
Here is the code I use for two consecutive checkboxes:
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
[24:41].EntireRow.Hidden = False
Else: [24:41].EntireRow.Hidden = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2 = True Then
[42:49].EntireRow.Hidden = False
Else: [42:49].EntireRow.Hidden = True
End If
End Sub
I have searched for a while before asking the question, and I apologise if there is an answer somewhere, I'm a total newbie to excel and VBA and I don't think I can adapt a solution that is remotely similar to the problem I'm facing.
Thanks for your precious help in advance.
Have a Column in which you write an X in each row you want to hide / unhide.
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = False
End If
Next c
Else
For each c in Range("A1:A50")
If c.Value = "x" Then
c.EntireRow.Hidden = True
End If
Next c
End If
End Sub
You could try naming your range
thisworkbook.names.add name:="mydinamicrange" ,ReferstoR1C1:="Sheet1!R24c1:R49C10"
Then , you may use it with something like this :
Private Sub CheckBox1_Click()
If CheckBox1 = True Then
thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden = False
Else: thisworkbook.names("mydinamicrange").referstorange.EntireRow.Hidden= True
End If

Excel vba - unprotect and protect while executing macro's

My spreadsheet hides and shows multiple rows by clicking buttons. As the spreadsheet is password protected any macro should apply password, then runs hide/show and finally set password again. This is how its look like.
Sub Macro1()
ActiveSheet.Unprotect Password:="abc"
Rows("12:16").EntireRow.Hidden = True
ActiveSheet.Protect Password:="abc"
End Sub
Sub Macro2()
ActiveSheet.Unprotect Password:="abc"
Rows("12:16").EntireRow.Hidden = False
ActiveSheet.Protect Password:="abc"
End Sub
Sub Macro3()
ActiveSheet.Unprotect Password:="abc"
Rows("20:24").EntireRow.Hidden = True
ActiveSheet.Protect Password:="abc"
End Sub
Sub Macro4()
ActiveSheet.Unprotect Password:="abc"
Rows("20:24").EntireRow.Hidden = False
ActiveSheet.Protect Password:="abc"
End Sub
Script works fine, but I have 16 sections which require 32 macro's. It still works fine but I wonder if there would be an easier way, requiring only 1 line for applying and 1x for setting the password.
Thank you for your comments.
Dennis
The Netherlands
Sub Macro1
HideIt Rows("12:16"), True
End Sub
Sub Macro2()
HideIt Rows("12:16"), False
End Sub
Sub HideRows(rng As Range, HideIt as Boolean)
ActiveSheet.Unprotect Password:="abc"
rng.EntireRow.Hidden = HideIt
ActiveSheet.Protect Password:="abc"
End Sub
If you could name your buttons with something which would enable to to translate the names to a range and true/False, you could link them all to a single Sub and use Application.Caller to get the name of the calling button and extract the parameters from that.
EDIT:
OK here's a very simple example: add two "forms" buttons to your worksheet and name one "btn_12_5_H" and the other "btn_12_5_S".
Here's how you name each button:
Select the button via a right-click
Enter the name in the "name" box in the formula bar and press Enter
Link both buttons to the Sub below (right-click button >> Assign macro):
Sub ShowHideRows()
Dim arr
'split the calling button name into an array
' (array will be zero-based)
arr = Split(Application.Caller, "_")
'**EDIT** check array is expected size...
If UBound(arr) <> 3 Then Exit Sub
If IsNumeric(arr(1)) and IsNumeric(arr(2)) Then
With Me 'if the code is in the sheet module, else "ActiveSheet"
.Unprotect Password:="abc"
'arr(1) determines start row
'arr(2) determines # of rows
'arr(3) determines if rows are hidden or not
.Cells(arr(1), 1).Resize(arr(2), 1).EntireRow.Hidden = (arr(3) = "H")
.Protect Password:="abc"
End With
End If
End Sub
EDIT#2:
Just for completeness, note that you can also add arguments directly to the OnAction (i.e. when you right-click the button an select "Assign macro")
For example you can use something like:
Book1!'ShowHideRows2 12,TRUE'
Note use of single quotes around the whole thing. The called sub might look something like (very basic example to demonstrate that the arguments were properly passed):
Sub ShowHideRows2(rownum, HideIt)
Debug.Print rownum, HideIt
End Sub
Note that because the Sub has parameters it won't show up in the "Assign macro" list and you have to type it in.

Making shapes invisible/visible in excel through VBA

I'm having this problem for the last few hours and I would really appreciate some help with it.
Basically, I want to be able to hide/unhide shapes depending on selections a user makes on a userform. I've broken the problem down into a very simple example. If I insert a shape called "oval 1" in a sheet and run the code:
Sub hideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = False
End With
End With
End Sub
the shape disappears but when I run this code
Sub unhideshape()
With ActiveSheet
.Shapes("Oval 1").Select
With Selection
.Visible = True
End With
End With
End Sub
I get an error "Requested Shapes are locked for Selection"
The workbook is not protected and I have tried un-ticking locked and locked text on the shape properties.
Any ideas what's causing this.
You cannot Select a hidden object. However, you dont need to use Select at all, and it is usually not recommended. Try simply:
Sub HideShape()
ActiveSheet.Shapes("Oval 1").Visible = False
End Sub
Sub UnhideShape()
ActiveSheet.Shapes("Oval 1").Visible = True
End Sub
I hide shapes based on their name since some shapes I don't want to hide. I use this format:
Sheet1.Shapes.Range(Array("COtxtBox1")).Visible = msoTrue
name of your shape or shapes goes into the array
if it only 1 shape you could just use:
Sheet1.Shapes.range("COtxtBox1").Visible = True
I found that the "mso" part is not necessary for the True or False statement
Sub HideEachShape()
Dim sObject As Shape
For Each sObject In ActiveSheet.Shapes
sObject.Visible = False
Next
End Sub
from: extendoffice.com
I solved problem with this code(Oval = Type 9, from MsoAutoShapeType Enumeration (Office)):
Sub hide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = False
Next i
End Sub
Sub unhide()
s = ActiveSheet.Shapes.Count
For i = 1 To s
If ActiveSheet.Shapes(i).Type = 9 Then ActiveSheet.Shapes(i).Visible = True
Next i
End Sub
If "Type = 9" is wrong, you can find out type of your shape with code in Immediate window (ctrl+G in VBA):
?ActiveSheet.Shapes("Oval 1").Type
Public HIDE As Boolean
Sub T_BUTTON ()
ActiveSheet.Shapes("T 1").Visible = HIDE
If ActiveSheet.Shapes("T 1").Visible = False Then
HIDE = True
Else
HIDE = False
End If
END SUB

excel macro pass data between two forms where focus is set

I have two forms.
form1 has four text boxes and a button for each textbox.
The button would setfocus on the textbox and bring up form2.
form2 is basicly a number keypad that also has a text box so that the user can select a number. That number will go in form2.textbox, which when changed will put the data in form1.textbox1.
The problem I'm having is how to tell form2.textbox to put data in form1.textbox2.
This is what my code looks like:
Public Sub textbox1_Click()
Me.textbox1.SetFocus
numbfrm.Show
End Sub
Private Sub textbox2_Click()
Me.textbox2.SetFocus
numbfrm.Show
End Sub
Private Sub textbox3_Click()
Me.txtactual.SetFocus
numbfrm.Show
End Sub
This is what is in the number form. It contains all of the numbers 1 to 10, but I just put the first three numbers here.
Private Sub Cmd1_Click()
TxtNumber.Value = TxtNumber.Value & "1"
End Sub
Private Sub Cmd2_Click()
TxtNumber.Value = TxtNumber.Value & "2"
End Sub
Public Sub TxtNumber_Change()
passnumber
End Sub
This is in a module:
Sub passnumber()
form1.textbox1.Value = numbfrm.TxtNumber
End Sub
I've been looking through the web to find an easy way to do that.
I tried puting in the module
Sub passnumber()
If form1.texbox1.foucs =true then
form1.textbox1.Value = numbfrm.TxtNumber
Else If form1.textbox2.foucs = true then
form1.texbox2.value =numbfrm.txtnumber
End sub
I have made a workaround for it, I put toggle buttons next to each box and when button is pressed it would mark it as true, and then I told it if that toggle is true it would use certain text box
Sub passnumber()
If form1.option1.value =true then
form1.textbox1.Value = numbfrm.TxtNumber
else
If form1.option2.value =true then
form1.textbox2.Value = numbfrm.TxtNumber
end if
end if
End sub

Resources