PDA

View Full Version : Solved: If/then statment where "then" does multiple actions?



Gingertrees
11-05-2009, 07:21 AM
The "Hub" (main page) of my workbook has multiple command buttons that basically just make other sheets visible and active. But I want to make sure people are prevented from messing around in a Read Only workbook (if they've ignored the error message when opening it in two places at once). So I'd like each button to look something like this:

Private Sub CommandButton1_Click()
'opens the 6mos. updates collection
If ActiveWorkbook.Readonly Then Msgbox("This file is already open & _
& vbNewLine "somewhere else. Find and close original file before proceeding.")
If ActiveWorkbook.Readonly = false Then
Sheet35.Visible = xlSheetVisible
Sheet20.Visible = xlSheetVisible
Sheet35.Activate
End Sub
However I do not know the right syntax to cause a "Then" statement to perform 3 actions. I tried tying them together with ampersans,
Sheet35.Visible = xlSheetVisible & Sheet20.Visible = xlSheetVisible & Sheet35.Activate but that didn't do anything.
?Help?

Gingertrees
11-05-2009, 08:51 AM
Nevermind, for anyone's reference I figured it out:
Private Sub CommandButton1_Click()
'6mos.updates collection
If ActiveWorkbook.ReadOnly Then MsgBox ("This file is already open " & _
"somewhere else." & vbNewLine & "You must locate and close file.")
Calculate
If ActiveWorkbook.ReadOnly = False Then
Sheet35.Visible = xlSheetVisible
Sheet20.Visible = xlSheetVisible
Sheet35.Activate
End If
End Sub

Put the code to catch files in Read Only mode FIRST.
Add in the line "Calculate"
Put in the second line to tell the computer what to do when things are happy and NOT in Read Only mode. It'll run all the code to the End of the Sub.

nst1107
11-05-2009, 08:57 AM
You can also put the actions in one line like you have in your second example, but each statement needs to be seperated by a colon.Sheet35.Visible = xlSheetVisible: Sheet20.Visible = xlSheetVisible: Sheet35.ActivateGlad you found the solution. :)

Gingertrees
11-05-2009, 09:07 AM
Hey Nate, that's neat too! Thanks. :-)

Bob Phillips
11-05-2009, 12:39 PM
You can also put the actions in one line like you have in your second example, but each statement needs to be seperated by a colon.Sheet35.Visible = xlSheetVisible: Sheet20.Visible = xlSheetVisible: Sheet35.ActivateGlad you found the solution. :)

You can, but you wouldn't, would you?

nst1107
11-05-2009, 04:01 PM
You can, but you wouldn't, would you?In this case, no I don't think I would. The line would get hard to read, and/or those statements would get lost in the shuffle if you were to come back looking for them.

I frequently will put two statements on one line like that, especially if they are short, coupled with an If statement, and the second one is Exit Sub. That, I don't think, is too hard to read or find, since the Exit Sub is blue. I really don't like block If statements with only one or two lines in them. It's like a With statement with only one or two lines in it.

What do you do?

Bob Phillips
11-06-2009, 04:45 AM
I try to think about maintainability always, and the problem with not having a block IF is that sometimes you can waste time looking for the End If. That sways it for me.

The only time I ever have multiple statements on a line is if I am initilaising some variables say, and they are connected. Otherwise, I avoid it.