ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Add worksheets at end (https://www.excelbanter.com/excel-programming/314578-add-worksheets-end.html)

FinChase

Add worksheets at end
 
I have some code that prompts users to enter the current month in an input
box, then it checks to see if a worksheet by that name already exists in the
workbook. If it does exist, the subroutine ends, and if it does not exist it
adds a worksheet with the name of the current month. My problem is that it
is adding the new worksheet as the first worksheet and I would like it add it
as the last worksheet. Can someone help me with this? Here is the code I
have so far:

Sub AddMonthWS()
Dim myMonth As String
Dim Sht As Worksheet
myMonth = Application.InputBox(Prompt:="Enter the current month name:",
Title:="Month", _
Type:=2)
On Error Resume Next
Set Sht = ActiveWorkbook.Worksheets(myMonth)
On Error GoTo 0
If Sht Is Nothing Then
Sheets.Add.Name = myMonth
End If
End Sub

Thanks, Lee

eRiC

Add worksheets at end
 
Pls Try as follows :-

Sub AddMonthWS()
Dim myMonth As String
Dim Sht As Worksheet
myMonth = Application.InputBox(Prompt:="Enter the current month name:",
Title:="Month", _
Type:=2)
On Error Resume Next
Set Sht = ActiveWorkbook.Worksheets(myMonth)
On Error GoTo 0
If Sht Is Nothing Then
Worksheets.Add after:=Worksheets(Worksheets.Count)
ActiveSheet.Name = myMonth
End If
End Sub




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

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com