Assign a different macro per click of VBA Button - vba

I have an excel button that hides columns "S-U" when I click it. I want to click the button a 2nd time and it hides columns "P-R" and etc. Can you manipulate an excel button per click?

If there are just a few things you want it to do (it sounds like it), I would have my macro evaluate the current state and act accordingly.
In your example, you indicate that you first want it to hide S-U, then P-R on a second click. This will do that:
Sub HideColumns()
If Sheets("Sheet1").Range("S:S").EntireColumn.Hidden = False Then
Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True
Else
Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True
End If
End Sub
Of course, you can extend this with additional conditions and actions.. like changing the text of the button to represent what it will do next:
Assume you have it labeled "Hide Rows S:U" to start, you can change it inside VBA to indicate what it would do on the next click:
Sub HideColumnsUpdateText()
If Sheets("Sheet1").Range("S:S").EntireColumn.Hidden = False Then
Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True
Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows P:R"
Else
Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True
End If
End Sub
There's really no end to what you can do once you start evaluating the current state (extend using ElseIf, or even use Case). You just have to keep the logic straight.
Edit to further expand:
If your situation is linear - that is, if want to hide more and more columns in the same order, you just need to do else ifs to evaluate the situation, and step through the order that you want to hide them.
Personal note: I find that "IF whatever =true" statements are a little easier to follow than "if whatever = false" (and technically, you don't even have to have type the "=True"). But it means you need to start at the last possibility, and work backwards. Otherwise you need to evaluate by "= False" (like I first demonstrated), but I find it a little harder to follow. Your results may vary.
You indicated that you want these hidden in order: "S:U","P:R","M:O","J:L","G:I" . Here is a script that, once all of those rows are hidden, the button would then show them all. So I start by evaluating if the last possibility is true -that is- are Rows G:I hidden already? If so, then show them all. I've also included updating the button text, but that's optional.
Sub hideSetsOfColumnsProgressively()
' progressive order of button function: "S:U","P:R","M:O","J:L","G:I","Unhide Rows"
If Sheets("Sheet1").Range("G:I").EntireColumn.Hidden = True Then
Sheets("Sheet1").Range("G:U").EntireColumn.Hidden = False 'shows all rows
'optionally change the text of the button to indicate the next function:
Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows S:U"
ElseIf Sheets("Sheet1").Range("J:L").EntireColumn.Hidden = True Then
'then we've already hidden all of the other columns, so do the last set
Sheets("Sheet1").Range("G:I").EntireColumn.Hidden = True
Sheets("Sheet1").Buttons("Button 1").Text = "Unhide Rows"
ElseIf Sheets("Sheet1").Range("M:O").EntireColumn.Hidden = True Then
Sheets("Sheet1").Range("J:L").EntireColumn.Hidden = True
Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows G:I"
ElseIf Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True Then
Sheets("Sheet1").Range("M:O").EntireColumn.Hidden = True
Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows J:L"
ElseIf Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True Then
Sheets("Sheet1").Range("P:R").EntireColumn.Hidden = True
Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows M:O"
Else
Sheets("Sheet1").Range("S:U").EntireColumn.Hidden = True
Sheets("Sheet1").Buttons("Button 1").Text = "Hide Rows P:R"
End If
End Sub
The nice part about this script is that it loops. You can just keep clicking it and it will hide progressively more and more rows, then show them all.
I hope this answers your question. Keep in mind that the logic has to be solid, or you'll get unexpected results.

One solution to this problem is be declaring a global variable, and assigning a counter to it:
Private i As Integer
Sub Button1_Click()
If i = 0 Then
Do Stuff 'This is the first click
i = i + 1
Else
Do Stuff the Second Time 'This is the second time and beyond.
i = i + 1
End If
End Sub
i will automatically be assigned a value of 0. The first time it checks i = 0 it will operate however you want the first time. Once it sets i = i + 1 then it will do whatever else you want it to do for the second time. If you want to make it a third time, you can always do Else If i = 1 Then, etc.
Under the Visual Basic Editor, create a new module if you don't already have one and place this in there. Create a button and assign the Button1_Click Macro to the Button.

If you only need to alternate between two options, e.g. like a toggle on off, then use a boolean:
Public x As Boolean
Private Sub CommandButton1_Click()
If x Then MsgBox ("ON") Else MsgBox ("OFF")
x = Not x
End Sub

Related

Set a sub-form hidden field to visible, based on a check box status

C, Thank you for your input and encouragement! I have changed my form and script slightly, I am afraid I kept the if then statement as I am comfortable with the formatting. The script now works when the 'On Open'event runs.
Private Sub Form_Open(Cancel As Integer)
Me.ChkAlbumNotes.SetFocus
If Me.ChkAlbumNotes.Value = False Then
Me.lblAlbumNotes.Visible = False
Me.txtAlbumNotes.Visible = False
Me.btnAlbumNotes.Visible = True
Else
Me.lblAlbumNotes.Visible = True
Me.txtAlbumNotes.Visible = True
Me.btnAlbumNotes.Visible = False
End If
Me.TrackName.SetFocus
If Me.TrackName = " " Then
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
This is fine when the form opens for the first time but I have a set of navigation buttons that are installed by the application as Macros. I cannot add my script to the On_Click event when the button is clicked, as On_Click is linked to the Macro. Is there a way to incorporate the script from the On_Load process to the pre-defined macro? Or can you suggest a neater way to achieve my requirements which are;
When the form opens,a check is made for the existence of a false value in the checkbox
if the check box is set to false, then the Notes Text Box and label are hidden and the notes button is visible.
If the check box has a true value, then the Notes text box and label are made visible and the button is hidden.
On completion of the test I check the field Track Name
if this is empty, I assume I am at the last record and give the Add New Record button the focus
If Track Name is not empty, then focus is set to Next Record button
when this button is clicked, the next record page opens and the process starts again.
Many Thanks
Mike
You should use the Form_Current event instead of Form_Open . This fires on starting the form (2 times) and everytime you move to another record.
Private Sub Form_Current()
Me.lblAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.txtAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.btnAlbumNotes.Visible = Not Me.ChkAlbumNotes.Value
If Me.TrackName = "" Then ' I suggest If Me.TrackName = " " being a typo and you want to check if empty ( that's why you should use vbNullString instead of "")
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub

VBA Expand/Collapse rows

I have a report in which I am asking the users to click buttons to reveal where they need to add their commentary. I have it working but wanted to put in an If statement in case they have already expanded the row.
I have two macros, the first relates to the button they push and sends to the main macro the name of the button and a row number which is part of the section that is either expanded or collapsed
Sub ROccupancy()
'
Dim RecName As String
RecName = "ROccupancy"
Dim RowNum As Integer
RowNum = 27
Call ToogleRec(RecName, RowNum)
End Sub
The next macro is where I am having the trouble
Sub ToogleRec(RecName, RowNum)
'
Dim Toogle As String
Dim MyObj As Object
Set MyObj = ActiveSheet.Shapes.Range(Array(RecName))
Toogle = Left(MyObj.TextFrame2.TextRange.Characters.Text, 4)
TextName = Mid(MyObj.TextFrame2.TextRange.Characters.Text, 5, 100)
If Toogle = "Show" Then
MyObj.ShapeStyle = msoShapeStylePreset9
MyObj.TextFrame2.TextRange.Characters.Text = _
"Hide" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = False Then
Rows(RowNum).ShowDetail = True
End If
Else
MyObj.ShapeStyle = msoShapeStylePreset11
MyObj.TextFrame2.TextRange.Characters.Text = _
"Show" & TextName
MsgBox Rows(RowNum).ShowDetail
If Rows(RowNum).ShowDetail = True Then
Rows(RowNum).ShowDetail = False
End If
End If
Range("C" & RowNum).Select
End Sub
The issue is the Rows(RowNum).ShowDetail is always TRUE, no matter if it's expanded or collapsed. I can remove the If section and set it to TRUE or FALSE using "Rows(RowNum).ShowDetail = False" or "Rows(RowNum).ShowDetail = TRUE". However, if the user has manually expanded or collapsed the row it causes an error (which freaks them out)
This question and answer seemed promising but Rows(RowNum).ShowDetail always seems to be TRUE
I put the MsgBox in there for error checking. I'll remove it in the final version.
Have you tried using Hidden property? Something like:
With Sheet1.Rows(5)
.ShowDetail = .Hidden
End With
Take note though that for you to use .ShowDetail method, you'll need to Group the rows first (needs to be in outline form).
True if the outline is expanded for the specified range (so that the detail of the column or row is visible). The specified range must be a single summary column or row in an outline.
Above code toggles hiding/unhiding a grouped row 5. You don't even need an If statement for the toggling. HTH.

Call a sub after this one has finished

I have a userform looping through a range with 2 settings; manual and automatic.
When I have an option button on my form set to manual, and click a next command button, I check the next cell in the range, change the contents of the form accordingly, then wait for the next button press.
However if I find the option button is set to automatic then instead of finishing up my code and waiting for the next button press, I have to call the next button press programmatically. That means that the previous subs each calling the next code slowly build up in the call stack, and I worry this will have some memory implications if I'm looping over a large range.
In code:
Private Sub nextItem()
Dim willShow As Boolean
returnResults 'return details from the form to the sheet
clearMemory 'clear out previous items on form
itemNo = itemNo + 1 'iterate over the range
SetupParts 'place new items on form
'do what next
Select Case displaySetting 'this variable holds the result from my option button in a custom enum "dispMode"
Case dispMode.Manual 'always show form
willShow = True
Case dispMode.SemiAutomatic 'show form based on condition
willShow = (Data.Count = 0) 'if SetupParts returns no data, display form, otherwise keep hidden
Case dispMode.Automatic 'don't show form
willShow = False
End Select 'there are actually a few more options here, but I've simplified
If willShow = False Then
If Me.Visible = True Then 'if needs to hide, and currently visible, then hide the form
Me.Hide
nextItem 'this is the problem, I call this code again, so the call stack grows
Else
'form is already hidden, do nothing
End If
ElseIf Me.Visible = False Then 'willShow must be True
Me.Show 'then wait for button click
End If
End Sub
Private Sub commandBtnNext_Click()
nextItem
End Sub
To avoid this problem, is there any way of getting nextItem to run immediately after the previous call of nextItem has run; ie. to tell a sub to run immediately after this one has finished (without introducing time delays). Or maybe this isn't an issue; if so, please explain why.
UPDATE
There is also a SemiAutomatic check to see which mode to use based on the contents of the userform. This is fine when calling recursively, but I can't see how to incorporate it into a looping approach.

Advancing two slides in powerpoint VBA

I am trying to advance slides based on a response. I want to go forward 2 slides if the user selects easy and one if they select hard. This is my current code. The Nextpage script isn't working AND I would prefer for it to be usable for multiple questions--I can't seem to get it to work with something like slide +1 or slide +2 (or ++).
Sub Start()
ActivePresentation.Slides(2).Shapes("selection_hard").Visible = False
ActivePresentation.Slides(2).Shapes("selection_easy").Visible = False
ActivePresentation.SlideShowWindow.View.Next
End Sub
Sub Shoe_Easy()
ShoeAnswer = "Easy"
ActivePresentation.Slides(2).Shapes("selection_hard").Visible = False
ActivePresentation.Slides(2).Shapes("selection_easy").Visible = True
'ActivePresentation.SlideShowWindow.View.GotoSlide (11)
End Sub
Sub Shoe_Hard()
ShoeAnswer = "Hard"
ActivePresentation.Slides(2).Shapes("selection_hard").Visible = True
ActivePresentation.Slides(2).Shapes("selection_easy").Visible = False
'ActivePresentation.SlideShowWindow.View.GotoSlide (12)
End Sub
Sub Nextpage()
If ActivePresentation.Slides(2).Shapes("selection_hard").Visisble = True Then
ActivePresentation.SlideShowWindow.View.GotoSlide (3)
ElseIf ActivePresenation.Slides(2).Shapes("selection_easy").Visible = True Then
ActivePresenation.SlideShowWindow.View.GotoSlide (4)
End If
End Sub
Assuming that "response" means clicking on one of two shapes (Easy or Hard), this will do it. You just need to make sure that the text in the shape and the code below match up and that you assign the HandleClick macro as a RunMacro action setting to each of the shapes (assign them to two of them then copy/paste the shapes elsewhere as needed).
There are a few extra hoops to jump through to get this working on a Mac; shout if you need it to work there too.
Sub HandleClick(oSh As Shape)
' Did they click the Easy or Hard button?
' oSh contains a reference to the shape they clicked
' Look a the text in oSh to decide where to go next:
Select Case UCase(oSh.TextFrame.TextRange.Text)
Case Is = "EASY"
SlideShowWindows(1).View.GotoSlide (oSh.Parent.SlideIndex + 2)
Case Is = "HARD"
SlideShowWindows(1).View.GotoSlide (oSh.Parent.SlideIndex + 1)
Case Else
' Do nothing
End Select
End Sub
This immediately advances the slide as soon as it's clicked. If you want the user to be able to choose an answer and then advance, you'd need a different approach.
Instead of advancing immediately as above, you'd set the value of a global variable to, say, "EASY" or "HARD", depending on the user's selection.
Then in a separate macro assigned to your forward button, you'd advance one or two slides depending on the value of the global variable.
I think something like this might help:
Nextpage()
Dim currentSlide as Slide
Set currentSlide = ActivePresentation.SlideshowWindow.View.Slide
If ActivePresentation.Slides(2).Shapes("selection_hard").Visisble = True Then
ActivePresentation.SlideShowWindow.View.GotoSlide currentSlide.SlideIndex + 1
ElseIf ActivePresenation.Slides(2).Shapes("selection_easy").Visible = True Then
ActivePresenation.SlideShowWindow.View.GotoSlide currentSlide.SlideIndex + 2
End If
End Sub

Multiple inputboxes one test for null input

Ok, so I have a series of input boxes pop up for the user and I know I want to check if the user hits cancel or "X" then I will have to check its return value.
EX answer = inputbox("lelele") if answer = "" then end else end if.
My problem is I have 4 input boxes in a row and I don't want to have to do a separate if statement for every input box so is there a way I can check all three in some sort of try catch block or while loop?
Below is the code I am actually using. Keep in mind that I am trying to catch a cancel every step along the way, so any time anyone clicks cancel the program immediately stops running.
'column you want to first select for copying
ColSelect = InputBox("which column do you want to select ColCopyFrom")
'the column you are comparing it to
ColCompare = InputBox("which column do you want to compare to ")
'where you are copying data from
ColCopyFrom = InputBox("which column do you want to copy data ColCopyFrom")
'where you are copying data to
ColCopyTo = InputBox("which column do you want to copy data to")
<
This is what I want to do for every box
if ColSelect = "" then
exit
else
'do nothing
end if
if ColCompare = "" then
exit
else
end if
You could wrap the inputbox into a function passing the prompt as a parameter like this and use END to break:
Function myInputBox(prompt As String) As String
Dim ib As String
ib = InputBox(prompt)
If ...
'... do some checking here
Else
End 'stop dead here
End if
myInputBox = ib
End Function
On the other hand, you might think about using a UserForm instead for enhanced usability.
Could you create a function and call it to check for each input? By creating a function once you can call upon it as many times as necessary, in your case 4 times. When you call the function you can then substitute the variable to check against
'first inputbox
Call checker(ColSelect)
'second input box
Call checker(ColCompare)
'etc
This is the function to check and you will see the variable name is cancel. This will take the variable entered (in the example ColSelect) and then give it a new name. This can be anything you like
sub checker(cancel)
if cancel = "" then
exit
else
'do nothing
end if
end Sub