PC Review


Reply
Thread Tools Rate Thread

Delete Sheets to the left

 
 
ian bartlett
Guest
Posts: n/a
 
      7th Sep 2008
Hi All

I add sheets through code, and when I have printed what I want I want to
delete all sheets except the two on the left in this case Sheet1(Sheet2) and
Sheet5(Sheet1) doing it with the recorder I got the following, but this will
not work after rebuilding the sheets.

Any ideas ??

Thanks Bart


Sheets("D3").Select
ActiveWindow.ScrollWorkbookTabs Position:=xlLast
Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
"DVG", "ISA", "3", "2", _
"ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
"DVK")).Select
Sheets("D3").Activate
Sheets(Array("10", "12", "3M", "ADM", "180", "PX3", "HOME",
"UAL")).Select Replace:= _
False
Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
"DVG", "ISA", "3", "2", _
"ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
"UAL")).Select
Sheets("UAL").Activate
Sheets(Array("DVK", "10", "12", "3M", "ADM", "180", "PX3",
"HOME")).Select Replace:= _
False
ActiveWindow.SelectedSheets.Delete
Range("F6").Select


 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      7th Sep 2008
Hi

Look at this:

Sub DeleteSheeets()
Application.DisplayAlerts = False
For Each sh In ThisWorkbook.Sheets
If sh.Name <> "Sheet1" And sh.Name <> "Sheet2" Then
sh.Delete
End If
Next
Application.DisplayAlerts = True
End Sub

Regards,
Per

"ian bartlett" <(E-Mail Removed)> skrev i meddelelsen
news:uSVwk.152886$nD.140587@pd7urf1no...
> Hi All
>
> I add sheets through code, and when I have printed what I want I want to
> delete all sheets except the two on the left in this case Sheet1(Sheet2)
> and Sheet5(Sheet1) doing it with the recorder I got the following, but
> this will not work after rebuilding the sheets.
>
> Any ideas ??
>
> Thanks Bart
>
>
> Sheets("D3").Select
> ActiveWindow.ScrollWorkbookTabs Position:=xlLast
> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
> "DVG", "ISA", "3", "2", _
> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
> "DVK")).Select
> Sheets("D3").Activate
> Sheets(Array("10", "12", "3M", "ADM", "180", "PX3", "HOME",
> "UAL")).Select Replace:= _
> False
> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
> "DVG", "ISA", "3", "2", _
> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
> "UAL")).Select
> Sheets("UAL").Activate
> Sheets(Array("DVK", "10", "12", "3M", "ADM", "180", "PX3",
> "HOME")).Select Replace:= _
> False
> ActiveWindow.SelectedSheets.Delete
> Range("F6").Select
>


 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      7th Sep 2008
This should delete sheets to the right of the active sheet.

Sub delshtsbyindex()
sc = Sheets.Count
'MsgBox sc
ms = ActiveSheet.Index
'MsgBox ms
For i = ms To sc
Sheets(i).Delete
'MsgBox Sheets(i).Name
Next i
End Sub


--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"ian bartlett" <(E-Mail Removed)> wrote in message
news:uSVwk.152886$nD.140587@pd7urf1no...
> Hi All
>
> I add sheets through code, and when I have printed what I want I want to
> delete all sheets except the two on the left in this case Sheet1(Sheet2)
> and Sheet5(Sheet1) doing it with the recorder I got the following, but
> this will not work after rebuilding the sheets.
>
> Any ideas ??
>
> Thanks Bart
>
>
> Sheets("D3").Select
> ActiveWindow.ScrollWorkbookTabs Position:=xlLast
> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
> "DVG", "ISA", "3", "2", _
> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
> "DVK")).Select
> Sheets("D3").Activate
> Sheets(Array("10", "12", "3M", "ADM", "180", "PX3", "HOME",
> "UAL")).Select Replace:= _
> False
> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
> "DVG", "ISA", "3", "2", _
> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
> "UAL")).Select
> Sheets("UAL").Activate
> Sheets(Array("DVK", "10", "12", "3M", "ADM", "180", "PX3",
> "HOME")).Select Replace:= _
> False
> ActiveWindow.SelectedSheets.Delete
> Range("F6").Select
>


 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      7th Sep 2008
> For i = ms To sc
> Sheets(i).Delete
> Next i


Did you test that code? It doesn't work because 'i' will go past Sheet.Count
midway through the loop.


--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"Don Guillett" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> This should delete sheets to the right of the active sheet.
>
> Sub delshtsbyindex()
> sc = Sheets.Count
> 'MsgBox sc
> ms = ActiveSheet.Index
> 'MsgBox ms
> For i = ms To sc
> Sheets(i).Delete
> 'MsgBox Sheets(i).Name
> Next i
> End Sub
>
>
> --
> Don Guillett
> Microsoft MVP Excel
> SalesAid Software
> (E-Mail Removed)
> "ian bartlett" <(E-Mail Removed)> wrote in message
> news:uSVwk.152886$nD.140587@pd7urf1no...
>> Hi All
>>
>> I add sheets through code, and when I have printed what I want I want to
>> delete all sheets except the two on the left in this case Sheet1(Sheet2)
>> and Sheet5(Sheet1) doing it with the recorder I got the following, but
>> this will not work after rebuilding the sheets.
>>
>> Any ideas ??
>>
>> Thanks Bart
>>
>>
>> Sheets("D3").Select
>> ActiveWindow.ScrollWorkbookTabs Position:=xlLast
>> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
>> "DVG", "ISA", "3", "2", _
>> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
>> "DVK")).Select
>> Sheets("D3").Activate
>> Sheets(Array("10", "12", "3M", "ADM", "180", "PX3", "HOME",
>> "UAL")).Select Replace:= _
>> False
>> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
>> "DVG", "ISA", "3", "2", _
>> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
>> "UAL")).Select
>> Sheets("UAL").Activate
>> Sheets(Array("DVK", "10", "12", "3M", "ADM", "180", "PX3",
>> "HOME")).Select Replace:= _
>> False
>> ActiveWindow.SelectedSheets.Delete
>> Range("F6").Select
>>

>


 
Reply With Quote
 
ian bartlett
Guest
Posts: n/a
 
      7th Sep 2008
Don

Chip is right it seems when I run your code I get the error
Run time error 9
Subscript out of range.

Any other ideas ??


 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      7th Sep 2008
Try code like the following.

Sub AAA()
On Error GoTo ErrH:
Application.DisplayAlerts = False
With ThisWorkbook.Worksheets
Do Until .Count <= 2
.Item(.Count).Delete
Loop
End With
ErrH:
Application.DisplayAlerts = True
End Sub



--
Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




"ian bartlett" <(E-Mail Removed)> wrote in message
news:HAWwk.153184$nD.64076@pd7urf1no...
> Don
>
> Chip is right it seems when I run your code I get the error
> Run time error 9
> Subscript out of range.
>
> Any other ideas ??
>
>


 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      7th Sep 2008
Give this a try (I'd put it in a Module)...

Sub DeleteWorksheets()
Dim WS As Worksheet
Worksheets(1).Select
On Error GoTo Whoops
Application.DisplayAlerts = False
For Each WS In Worksheets
If WS.Index > 2 Then WS.Delete
Next
Whoops:
Application.DisplayAlerts = True
End Sub

--
Rick (MVP - Excel)


"ian bartlett" <(E-Mail Removed)> wrote in message
news:HAWwk.153184$nD.64076@pd7urf1no...
> Don
>
> Chip is right it seems when I run your code I get the error
> Run time error 9
> Subscript out of range.
>
> Any other ideas ??
>
>


 
Reply With Quote
 
john
Guest
Posts: n/a
 
      7th Sep 2008
another way maybe

Sub DeleteSheets()
Dim Arr() As String
'Create an Array
'of all Sheets
Dim N As Long
Application.DisplayAlerts = False
With ThisWorkbook
With .Worksheets
If .Count < 3 Then Exit Sub
ReDim Arr(3 To .Count)
For N = 3 To .Count
Arr(N) = .Item(N).Name
Next N
End With
.Worksheets(Arr).Delete
End With
Application.DisplayAlerts = True
End Sub
--
jb


"ian bartlett" wrote:

> Hi All
>
> I add sheets through code, and when I have printed what I want I want to
> delete all sheets except the two on the left in this case Sheet1(Sheet2) and
> Sheet5(Sheet1) doing it with the recorder I got the following, but this will
> not work after rebuilding the sheets.
>
> Any ideas ??
>
> Thanks Bart
>
>
> Sheets("D3").Select
> ActiveWindow.ScrollWorkbookTabs Position:=xlLast
> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
> "DVG", "ISA", "3", "2", _
> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
> "DVK")).Select
> Sheets("D3").Activate
> Sheets(Array("10", "12", "3M", "ADM", "180", "PX3", "HOME",
> "UAL")).Select Replace:= _
> False
> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
> "DVG", "ISA", "3", "2", _
> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB", "6",
> "UAL")).Select
> Sheets("UAL").Activate
> Sheets(Array("DVK", "10", "12", "3M", "ADM", "180", "PX3",
> "HOME")).Select Replace:= _
> False
> ActiveWindow.SelectedSheets.Delete
> Range("F6").Select
>
>
>

 
Reply With Quote
 
ian bartlett
Guest
Posts: n/a
 
      7th Sep 2008
Chip, Rick & John

Thanks very much I tried all three and all performed as stated

Thanks again.

Bart


 
Reply With Quote
 
Don Guillett
Guest
Posts: n/a
 
      7th Sep 2008
Since I didn't really want to delete sheets I only tested with
sheets(i).select
MY BAAD!

--
Don Guillett
Microsoft MVP Excel
SalesAid Software
(E-Mail Removed)
"Chip Pearson" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>> For i = ms To sc
>> Sheets(i).Delete
>> Next i

>
> Did you test that code? It doesn't work because 'i' will go past
> Sheet.Count midway through the loop.
>
>
> --
> Cordially,
> Chip Pearson
> Microsoft Most Valuable Professional
> Excel Product Group
> Pearson Software Consulting, LLC
> www.cpearson.com
> (email on web site)
>
>
>
>
> "Don Guillett" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> This should delete sheets to the right of the active sheet.
>>
>> Sub delshtsbyindex()
>> sc = Sheets.Count
>> 'MsgBox sc
>> ms = ActiveSheet.Index
>> 'MsgBox ms
>> For i = ms To sc
>> Sheets(i).Delete
>> 'MsgBox Sheets(i).Name
>> Next i
>> End Sub
>>
>>
>> --
>> Don Guillett
>> Microsoft MVP Excel
>> SalesAid Software
>> (E-Mail Removed)
>> "ian bartlett" <(E-Mail Removed)> wrote in message
>> news:uSVwk.152886$nD.140587@pd7urf1no...
>>> Hi All
>>>
>>> I add sheets through code, and when I have printed what I want I want to
>>> delete all sheets except the two on the left in this case Sheet1(Sheet2)
>>> and Sheet5(Sheet1) doing it with the recorder I got the following, but
>>> this will not work after rebuilding the sheets.
>>>
>>> Any ideas ??
>>>
>>> Thanks Bart
>>>
>>>
>>> Sheets("D3").Select
>>> ActiveWindow.ScrollWorkbookTabs Position:=xlLast
>>> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
>>> "DVG", "ISA", "3", "2", _
>>> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB",
>>> "6", "DVK")).Select
>>> Sheets("D3").Activate
>>> Sheets(Array("10", "12", "3M", "ADM", "180", "PX3", "HOME",
>>> "UAL")).Select Replace:= _
>>> False
>>> Sheets(Array("D3", "5", "ATC", "CRU", "AA", "8", "DVJ", "DVH", "1",
>>> "DVG", "ISA", "3", "2", _
>>> "ISC", "7", "ISD", "D4", "9", "D2", "DVF", "4", "ISE", "ISB",
>>> "6", "UAL")).Select
>>> Sheets("UAL").Activate
>>> Sheets(Array("DVK", "10", "12", "3M", "ADM", "180", "PX3",
>>> "HOME")).Select Replace:= _
>>> False
>>> ActiveWindow.SelectedSheets.Delete
>>> Range("F6").Select
>>>

>>

>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hide Sheets to Left Q Seanie Microsoft Excel Programming 2 4th Mar 2011 12:38 PM
Set all sheets to see A1 as left top cell Diddy Microsoft Excel Programming 4 23rd Mar 2009 04:51 PM
re-setting view of sheets so A1 is top left Diddy Microsoft Excel Programming 2 23rd Mar 2009 04:50 PM
How do I unhide all sheets on the left of Excel Saucer Man Microsoft Excel Programming 6 10th Mar 2009 02:48 PM
can i put sheets on the left and right of my screen Steve Microsoft Excel Misc 1 15th Dec 2008 07:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:15 PM.