Home |
Search |
Today's Posts |
|
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
I've combined 2 macros that I got help writing here.
Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
|
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
Hi,
If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If ...... Missing here HTh "davegb" wrote: I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
No, he has a line continuation, so no End If needed.
-- HTH Bob Phillips "Toppers" wrote in message ... Hi, If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If ...... Missing here HTh "davegb" wrote: I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
But it could be End If to If Not TopCell Is Nothing which admittedly could
come before If .name as per Don's reply. Both are valid I think. "Bob Phillips" wrote: No, he has a line continuation, so no End If needed. -- HTH Bob Phillips "Toppers" wrote in message ... Hi, If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If ...... Missing here HTh "davegb" wrote: I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
I see your point. As I say, you should indent Ifs, and don't use single line
Ifs, it all gets too confusing. Bob "Toppers" wrote in message ... But it could be End If to If Not TopCell Is Nothing which admittedly could come before If .name as per Don's reply. Both are valid I think. "Bob Phillips" wrote: No, he has a line continuation, so no End If needed. -- HTH Bob Phillips "Toppers" wrote in message ... Hi, If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If ...... Missing here HTh "davegb" wrote: I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
Agreed!
"Bob Phillips" wrote: I see your point. As I say, you should indent Ifs, and don't use single line Ifs, it all gets too confusing. Bob "Toppers" wrote in message ... But it could be End If to If Not TopCell Is Nothing which admittedly could come before If .name as per Don's reply. Both are valid I think. "Bob Phillips" wrote: No, he has a line continuation, so no End If needed. -- HTH Bob Phillips "Toppers" wrote in message ... Hi, If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If ...... Missing here HTh "davegb" wrote: I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
Right, properly indenting the lines after
If Not TopCell Is Nothing Then ' if it found "top" would've helped. P.S. One line If statements are OK with me if they fit nicely on one line. "Toppers" wrote: But it could be End If to If Not TopCell Is Nothing which admittedly could come before If .name as per Don's reply. Both are valid I think. "Bob Phillips" wrote: No, he has a line continuation, so no End If needed. -- HTH Bob Phillips "Toppers" wrote in message ... Hi, If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If ...... Missing here HTh "davegb" wrote: I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
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! |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
Hi Dave
check my comments in line ######### -- Cheers JulieD check out www.hcts.net.au/tipsandtricks.htm ....well i'm working on it anyway "davegb" wrote in message ups.com... I've combined 2 macros that I got help writing here. Sub AllSheetsToggleProtectWInd() 'for all sheets in currently active workbook, assigned to button Dim TopCell As Range Dim TopCol As Range Dim Cols2Hide As Range Dim wkSht As Worksheet For Each wkSht In ActiveWorkbook.Worksheets With wkSht If .ProtectContents Then .Unprotect Password:=PWORD .Name = .Name & "##" .Columns.Hidden = False Else Set TopCell = .Rows(3).Find(What:="top", LookIn:=xlValues) If Not TopCell Is Nothing Then ' if it found "top" ########where is this IF's END IF???############ Set TopCol = .Columns(TopCell.Column) Set Cols2Hide = .Range(TopCol, .Columns("AC")) Cols2Hide.Hidden = True .Protect Password:=PWORD If .Name Like "*[##]" Then _ .Name = Left(.Name, Len(.Name) - 2) End If End With<-----[end with without with error] Next wkSht End Sub But when I run it, I get an error of "End with without if", even though there's very clearly a "with wksht" statement at the top. If I remark out the End with, I get a "Next without For" error, even though there's a "For" statement. So any ideas on why VBA can't see my with or For each statements? Thanks in advance. |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
Boy, is my face red! Somehow, as I wrote the message, I put down that
there was an "End If w/o an If" error. It's an "End with w/o with" error, which is what I indicated in the code I posted. Does that make more sense? Sorry for the confusion, working on too many things at once. |
#12
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
Hi Dave
but as far as i can tell you do need this extra END IF and if you put it in does your code then run correctly ... error messages aren't always 100% accurate :) -- Cheers JulieD check out www.hcts.net.au/tipsandtricks.htm ....well i'm working on it anyway "davegb" wrote in message ups.com... Boy, is my face red! Somehow, as I wrote the message, I put down that there was an "End If w/o an If" error. It's an "End with w/o with" error, which is what I indicated in the code I posted. Does that make more sense? Sorry for the confusion, working on too many things at once. |
#13
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
True, the compiler/interpreter can't see the "missing" With statement. The
End With appears to be inside the unclosed If statement. "JulieD" wrote: Hi Dave but as far as i can tell you do need this extra END IF and if you put it in does your code then run correctly ... error messages aren't always 100% accurate :) -- Cheers JulieD check out www.hcts.net.au/tipsandtricks.htm ....well i'm working on it anyway "davegb" wrote in message ups.com... Boy, is my face red! Somehow, as I wrote the message, I put down that there was an "End If w/o an If" error. It's an "End with w/o with" error, which is what I indicated in the code I posted. Does that make more sense? Sorry for the confusion, working on too many things at once. |
#14
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
It worked! Thanks a lot, Julie!
|
#15
Posted to microsoft.public.excel.programming
|
|||
|
|||
End if w/o if?
you're welcome
-- Cheers JulieD "davegb" wrote in message oups.com... It worked! Thanks a lot, Julie! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|