adding new worksheets

MAMIBUSSOL

Board Regular
Joined
Jun 2, 2011
Messages
95
I would like the results of my script to be transferred to a new worksheet

firstly I need to check whether the worksheet exist, if the sheet exists then I need to delete it and recreate.

however I also need to add data further on into the code, so I also need to check whether it exist or not
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
For instance:

Code:
Sub test()

    Dim ws As Worksheet
    
    On Error Resume Next
    Set ws = ThisWorkbook.Worksheets("YOUR SHEET")
    If Err.Number Then
        Set ws = ThisWorkbook.Worksheets.Add
        ws.Name = "YOUR SHEET"
    End If
    On Error GoTo 0

End Sub

ws is a variable declared as Worksheet. It holds a reference to either the existing worksheet, either the newly created worksheet.

Do not forget to add the name of the worksheet in the code above. If you want, use a constant to hold this name.
 
Upvote 0
Please have a look at the Add method in the VBE help files.

For instance:

Code:
Set ws = ThisWorkbook.Worksheets.Add(After:= ThisWorkbook.Worksheets.Count)
 
Upvote 0
I must be going mad, but I just can't see whats wrong with this code, and as it is not creating the required worksheet, I am not getting any errors.

according to my understanding it should do the following

1. Open TEST 2010.XLSM - file does exist and does open
2. creates worksheet 1, LASTNAME, FIRSTNAME - worksheet not created
3. moves the worksheet to the last worksheet - not sure if this works as the worksheet hasn't been created to move

Filename = TEST 2010.XLSM
SHEETNAME = 1, LASTNAME, FIRSTNAME

Code:
DIM WS AS WORKSHEET
 
Workbooks.Open Filename:= _
        "C:\Documents and Settings\Admin\Desktop\" & PAYDIR & "\" & Filename
Windows(Filename).Activate 
On Error Resume Next
Set WS = Filename.Worksheets(SHEETNAME)
If Err.Number Then
     Set WS = Filename.Worksheets.Add(AFTER:=Filename.Worksheets.Count)
      WS.Name = SHEETNAME
End If
On Error GoTo 0
 
Upvote 0

Forum statistics

Threads
1,224,888
Messages
6,181,602
Members
453,055
Latest member
cope7895

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top