Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Selecting Last Worksheet



How would I select the last worksheet from a workbook?
I'm wanting to delete the last worksheet from a workbook.


--
Jack of all trades... master of none..
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Selecting Last Worksheet

Hi Alex

Try this

Sub test()
With ThisWorkbook
If .Sheets.Count 1 Then
Application.DisplayAlerts = False
.Sheets(.Sheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Alex" wrote in message ...


How would I select the last worksheet from a workbook?
I'm wanting to delete the last worksheet from a workbook.


--
Jack of all trades... master of none..



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Selecting Last Worksheet

Hmmmm...nothing happened.



"Ron de Bruin" wrote:

Hi Alex

Try this

Sub test()
With ThisWorkbook
If .Sheets.Count 1 Then
Application.DisplayAlerts = False
.Sheets(.Sheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Alex" wrote in message ...


How would I select the last worksheet from a workbook?
I'm wanting to delete the last worksheet from a workbook.


--
Jack of all trades... master of none..




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 493
Default Selecting Last Worksheet

Ok I sorta figured it out. Though if anyone can tell me how to select the
last worksheet, I'd appreciate it.

Dim WS As Byte

WS = ActiveWorkbook.Worksheets.Count
Application.DisplayAlerts = False
ActiveWorkbook.Worksheets(WS).Delete
Application.DisplayAlerts = True

This works for the macro that it runs on, as the last sheet is always the
one that needs to be deleted, even if it's sort of a round about way to do
(or so I think) and this part of the code only runs once.

And to think I only spent all day trying to figure this out....

"Alex" wrote:



How would I select the last worksheet from a workbook?
I'm wanting to delete the last worksheet from a workbook.


--
Jack of all trades... master of none..

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Selecting Last Worksheet

Sub test()
With ActiveWorkbook
If .WorkSheets.Count 1 Then
Application.DisplayAlerts = False
.WorkSheets(.WorkSheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

--
Regards,
Tom Ogilvy


"Alex" wrote in message
...
Hmmmm...nothing happened.



"Ron de Bruin" wrote:

Hi Alex

Try this

Sub test()
With ThisWorkbook
If .Sheets.Count 1 Then
Application.DisplayAlerts = False
.Sheets(.Sheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Alex" wrote in message

...


How would I select the last worksheet from a workbook?
I'm wanting to delete the last worksheet from a workbook.


--
Jack of all trades... master of none..








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Selecting Last Worksheet

Hello folks,

It seems that there is a tangible difference between these 2 macros
that have been suggested:-

===============================
Sub test()
With ActiveWorkbook
If .WorkSheets.Count 1 Then
Application.DisplayAlerts = False
.WorkSheets(.WorkSheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

================================

Sub test()
With ThisWorkbook
If .Sheets.Count 1 Then
Application.DisplayAlerts = False
.Sheets(.Sheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

================================

What's the difference between ThisWorkbook and ActiveWorkbook please?

And between Worksheets and Sheets?

Many thanks
Rob
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Selecting Last Worksheet

ThisWorkbook refers to the workbook that contains the code.

ActiveWorkbook is the workbook that has the focus in Excel's window.

They may be the same or not.

--
Regards,
Tom Ogilvy


"Chuck Elsham" wrote in message
...
Hello folks,

It seems that there is a tangible difference between these 2 macros
that have been suggested:-

===============================
Sub test()
With ActiveWorkbook
If .WorkSheets.Count 1 Then
Application.DisplayAlerts = False
.WorkSheets(.WorkSheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

================================

Sub test()
With ThisWorkbook
If .Sheets.Count 1 Then
Application.DisplayAlerts = False
.Sheets(.Sheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

================================

What's the difference between ThisWorkbook and ActiveWorkbook please?

And between Worksheets and Sheets?

Many thanks
Rob



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Selecting Last Worksheet

And between Worksheets and Sheets?

And Sheets in my example will also delete a ChartSheet
Tom Change it to worksheets so it will only delete the last WorkSheet

If you only have worksheets in your workbook you can use both (Sheets or worksheets)


--
Regards Ron de Bruin
http://www.rondebruin.nl


"Tom Ogilvy" wrote in message ...
ThisWorkbook refers to the workbook that contains the code.

ActiveWorkbook is the workbook that has the focus in Excel's window.

They may be the same or not.

--
Regards,
Tom Ogilvy


"Chuck Elsham" wrote in message
...
Hello folks,

It seems that there is a tangible difference between these 2 macros
that have been suggested:-

===============================
Sub test()
With ActiveWorkbook
If .WorkSheets.Count 1 Then
Application.DisplayAlerts = False
.WorkSheets(.WorkSheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

================================

Sub test()
With ThisWorkbook
If .Sheets.Count 1 Then
Application.DisplayAlerts = False
.Sheets(.Sheets.Count).Delete
Application.DisplayAlerts = True
End If
End With
End Sub

================================

What's the difference between ThisWorkbook and ActiveWorkbook please?

And between Worksheets and Sheets?

Many thanks
Rob





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting a row in a worksheet Rochelle in Melbourne Excel Worksheet Functions 1 April 9th 09 11:34 AM
selecting a worksheet rocket0612 Excel Worksheet Functions 2 March 1st 06 09:47 AM
Selecting Worksheet by it's value originally given [email protected] Excel Worksheet Functions 2 October 28th 05 07:48 PM
Selecting All AutoShapes on a Worksheet Alan Excel Programming 1 May 28th 04 12:51 AM
Selecting previous worksheet Carolyne[_2_] Excel Programming 2 May 20th 04 12:05 AM


All times are GMT +1. The time now is 05:14 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"