Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I have some code that I thought would delete every sheet in a workbook except
the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Edit this to read:
If (wks.Name < "Product Names Data") Or (wks.Name < "Master Product Sheet") Then wks.Delete and you should be OK. Kruncher |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If you use an OR, then every sheet will result in a true value
Master1 < Master1 or Master1 < Master2 False or True = True with OR, Master1 < Master2 would be true. Use AND instead: false And True = False Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" AND _ "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- Regards, Tom Ogilvy "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
At least when I make a mistake first thing on a Monday, I was corrected
by the best. Thanks Tom. |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Don't you need to include the sheet reference in your second condition?
If wks.Name < "Product Names Data" AND _ wks.Name < "Master Product Sheet" Then -- HTH... Jim Thomlinson "Tom Ogilvy" wrote: If you use an OR, then every sheet will result in a true value Master1 < Master1 or Master1 < Master2 False or True = True with OR, Master1 < Master2 would be true. Use AND instead: false And True = False Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" AND _ "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- Regards, Tom Ogilvy "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Yes, you're correct. I did show it but missed the AND/OR problem. Tom
missed the second reference, so I don't feel so bad now ;-) |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Tom,
I'm getting a next without for error if I copy your code. "Tom Ogilvy" wrote: If you use an OR, then every sheet will result in a true value Master1 < Master1 or Master1 < Master2 False or True = True with OR, Master1 < Master2 would be true. Use AND instead: false And True = False Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" AND _ "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- Regards, Tom Ogilvy "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Put END IF before NEXT WKS statement
"Master Product Sheet" Then wks.Delete End If Next wks "hshayh0rn" wrote: Hi Tom, I'm getting a next without for error if I copy your code. "Tom Ogilvy" wrote: If you use an OR, then every sheet will result in a true value Master1 < Master1 or Master1 < Master2 False or True = True with OR, Master1 < Master2 would be true. Use AND instead: false And True = False Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" AND _ "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- Regards, Tom Ogilvy "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
It is hard to tell when code is posted but I think that the end if is not
required because the if statement was intended to be all on line. If the delete is a new line then I would agree that the end if is necessary. Hard to tell. -- HTH... Jim Thomlinson "Toppers" wrote: Put END IF before NEXT WKS statement "Master Product Sheet" Then wks.Delete End If Next wks "hshayh0rn" wrote: Hi Tom, I'm getting a next without for error if I copy your code. "Tom Ogilvy" wrote: If you use an OR, then every sheet will result in a true value Master1 < Master1 or Master1 < Master2 False or True = True with OR, Master1 < Master2 would be true. Use AND instead: false And True = False Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" AND _ "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- Regards, Tom Ogilvy "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#10
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() Jim, You are right .... I made the judgement that wks. delete was a new line and therefore the END IF was required: but I did also recognise that it could be one line. As Tom rarely makes a mistake, the one line view is most likely! "Jim Thomlinson" wrote: It is hard to tell when code is posted but I think that the end if is not required because the if statement was intended to be all on line. If the delete is a new line then I would agree that the end if is necessary. Hard to tell. -- HTH... Jim Thomlinson "Toppers" wrote: Put END IF before NEXT WKS statement "Master Product Sheet" Then wks.Delete End If Next wks "hshayh0rn" wrote: Hi Tom, I'm getting a next without for error if I copy your code. "Tom Ogilvy" wrote: If you use an OR, then every sheet will result in a true value Master1 < Master1 or Master1 < Master2 False or True = True with OR, Master1 < Master2 would be true. Use AND instead: false And True = False Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" AND _ "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- Regards, Tom Ogilvy "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#11
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I think you need an And condition so that if it is not this sheet and it is
not that sheet then delete the sheet. Boolean is a little funny when you use not = Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" and wks.Name < "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- HTH... Jim Thomlinson "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Thanks Jim and everyone else for your responses. I got it to work so thank you!
"Jim Thomlinson" wrote: I think you need an And condition so that if it is not this sheet and it is not that sheet then delete the sheet. Boolean is a little funny when you use not = Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" and wks.Name < "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub -- HTH... Jim Thomlinson "hshayh0rn" wrote: I have some code that I thought would delete every sheet in a workbook except the two master sheets that can't be deleted but it's not working. I'm thinking the problem is with the OR part of the code. If I remove the OR then the code runs but the OR is pretty important so I'm look for some insight. Sub DeleteSheets() Dim wks As Worksheet Application.DisplayAlerts = False For Each wks In Worksheets If wks.Name < "Product Names Data" Or "Master Product Sheet" Then wks.Delete Next wks ErrorHandler: Application.DisplayAlerts = False End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Deleting Duplicate Records across multiple sheets | Excel Worksheet Functions | |||
deleting columns on multiple sheets | Excel Discussion (Misc queries) | |||
Deleting sheets | Excel Programming | |||
Deleting multiple Chart Tabs/sheets | Excel Programming | |||
Deleting Hyphens or Dashes from multiple cells without deleting the remaining content | Excel Programming |