End if w/o if?

D

davegb

Not sure I'm following some of your comments re indenting properly. I
imagine there are different approaches, just like naming variables, but
can someone give me some guidelines to help avoid this type of problem?
For instance, post how you would have indented my macro?
Thanks!
 
J

JulieD

Hi Dave

the way i teach it is, consider statements like SUB / END SUB; WITH / END
WITH, IF / ELSEIF / END IF etc as brackets ... everything inside the
brackets should be indented one tab each time ... this way, you can easily
read down a column and spot missing END IF or END WITH etc .. so your macro
(not knowing exactly where your IF statment finishes), i would write as

Sub AllSheetsToggleProtectWInd()
{BLANK}
{TAB}'for all sheets in currently active workbook, assigned to button
{TAB}Dim TopCell As Range
{TAB}Dim TopCol As Range
{TAB}Dim Cols2Hide As Range
{TAB}Dim wkSht As Worksheet
{BLANK}
{TAB}FOR EACH wkSht INActiveWorkbook.Worksheets
{TAB}{TAB}WITH wkSht
{TAB}{TAB}{TAB}IF.ProtectContents Then
{TAB}{TAB}{TAB}{TAB}.Unprotect Password:=PWORD
{TAB}{TAB}{TAB}{TAB}.Name = .Name & "##"
{TAB}{TAB}{TAB}{TAB}.Columns.Hidden = False
{TAB}{TAB}{TAB}ELSE
{TAB}{TAB}{TAB}{TAB}Set TopCell = .Rows(3).Find(What:="top",
LookIn:=xlValues)
{TAB}{TAB}{TAB}{TAB}If Not TopCell Is Nothing Then ' if it found "top"
{TAB}{TAB}{TAB}{TAB}{TAB}Set TopCol = .Columns(TopCell.Column)
{TAB}{TAB}{TAB}{TAB}{TAB}Set Cols2Hide = .Range(TopCol, .Columns("AC"))
{TAB}{TAB}{TAB}{TAB}{TAB}Cols2Hide.Hidden = True
{TAB}{TAB}{TAB}{TAB}{TAB}.Protect Password:=PWORD
{TAB}{TAB}{TAB}{TAB}END IF 'assuming it goes here
{TAB}{TAB}{TAB}{TAB}IF .Name Like "*[##]" Then
{TAB}{TAB}{TAB}{TAB}{TAB}.Name = Left(.Name, Len(.Name) - 2)
{TAB}{TAB}{TAB}{TAB}END IF 'i'm not a big fan of one line IF statements
either
{TAB}{TAB}{TAB}END IF
{TAB}{TAB}END WITH
{TAB}NEXT wkSht
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top