PDA

View Full Version : ELSE without IF error



Dibbley247
02-07-2013, 07:34 AM
I'm getting an error when testing saying "End without if"
Since I put a yes/no message box in.
Part of my code below:

Sub CleardownSheets()
Dim Response As Integer
Response = MsgBox(prompt:="This will clear the sheet for a new week, are you sure?", Buttons:=vbYesNo)
If Response = vbYes Then
'Will now clear ranges if YES was selected
Const PATH As String = "\\calfp03\Lynher Bakery\1AAMan Hours\current week\2013 - A1\1A WK"
With ActiveWorkbook
.SaveCopyAs Filename:=PATH & .Sheets("Reset Sheet").Range("A1").Value & ".xls"
Application.ScreenUpdating = False
' Will clear all sheets in workbook
Sheets("Sat Night (3)").Select
Range("B5,D5:K5,O5:P5,B7,D7:K7,O7:P7,B9,D9:K9,O9:P9,B11,D11:K11,O11:P11,D13:K13,O1 3:P13").Select
Selection.ClearContents
Application.ScreenUpdating = True
ActiveWorkbook.Save
Else
' The no button was selected.
MsgBox "Action Was Cancelled"
End If
End With
End Sub


Thanks

p45cal
02-07-2013, 07:46 AM
just move your end with statement so stuff is nested nicely:Sub CleardownSheets()
Dim Response As Integer
Response = MsgBox(prompt:="This will clear the sheet for a new week, are you sure?", Buttons:=vbYesNo)
If Response = vbYes Then
'Will now clear ranges if YES was selected
Const PATH As String = "\\calfp03\Lynher Bakery\1AAMan Hours\current week\2013 - A1\1A WK"
With ActiveWorkbook
.SaveCopyAs Filename:=PATH & .Sheets("Reset Sheet").Range("A1").Value & ".xls"
Application.ScreenUpdating = False
' Will clear all sheets in workbook
Sheets("Sat Night (3)").Select
Range("B5,D5:K5,O5:P5,B7,D7:K7,O7:P7,B9,D9:K9,O9:P9,B11,D11:K11,O11:P11,D13:K13,O1 3:P13").Select
Selection.ClearContents
Application.ScreenUpdating = True
ActiveWorkbook.Save
End With
Else
' The no button was selected.
MsgBox "Action Was Cancelled"
End If
End Sub

Dibbley247
02-07-2013, 07:52 AM
Thanks.
I didn't think that would have mattered where that END WITH would have been!
Another one I THINK I've learned

Thanks very much

snb
02-07-2013, 10:28 AM
there's more to learn....

Sub CleardownSheets()
If MsgBox("This will clear the sheet for a new week, are you sure?", 4) = 6 Then
With ActiveWorkbook
.SaveCopyAs "\\calfp03\Lynher Bakery\1AAMan Hours\current week\2013 - A1\1A WK" & .Sheets("Reset Sheet").Range("A1").Value & ".xls"
.Sheets("Sat Night (3)").Range("B5,D5:K5,O5:P5,B7,D7:K7,O7:P7,B9,D9:K9,O9:P9,B11,D11:K11,O11:P11,D13:K13,O1 3:P13").ClearContents
.Save
End With
End If
End Sub

Dibbley247
02-08-2013, 01:11 AM
Thats why I dont like the recorder sometimes!
So you don't need to select things to clear them!?

snb
02-08-2013, 03:05 AM
Preferably you avoid any 'select' or 'activate' in VBA.