ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating Sheets in VBA (https://www.excelbanter.com/excel-programming/334188-creating-sheets-vba.html)

cmk18[_7_]

Creating Sheets in VBA
 

I want to create a set of sheets that are basically the same, except for
a few changes. But I may need to create new ones on the fly, and I was
curious if there was a way to check if the sheet exists and if it does
go to that sheet, and if it doesn't create a new one. I was planning
on using a naming configuration of ScoringSheet_x and the x would be
selected by the user.


--
cmk18
------------------------------------------------------------------------
cmk18's Profile: http://www.excelforum.com/member.php...fo&userid=6047
View this thread: http://www.excelforum.com/showthread...hreadid=386196


dominicb[_59_]

Creating Sheets in VBA
 

Good evening Cmk18

If you try to navigate to a non existent sheet in VBA an error will be
thrown up. Why not trap the error (such as On Error Goto NewSheet) and
then at the NewSheet: label, create the new sheet. If an error doesn't
occur, the sheet exists and you can Exit Sub.

HTH

DominicB


--
dominicb
------------------------------------------------------------------------
dominicb's Profile: http://www.excelforum.com/member.php...o&userid=18932
View this thread: http://www.excelforum.com/showthread...hreadid=386196


Dave Peterson

Creating Sheets in VBA
 
This is from Chip Pearson:

Function WorksheetExists(SheetName As Variant, _
Optional WhichBook As Workbook) As Boolean
'from Chip Pearson
Dim WB As Workbook
Set WB = IIf(WhichBook Is Nothing, ThisWorkbook, WhichBook)
On Error Resume Next
WorksheetExists = CBool(Len(WB.Worksheets(SheetName).Name) 0)
End Function

And you could use that function in your code like:

if worksheetexists("mysheetname",activeworkbook) then
'sheet exists
else
'add the sheet and name it
end if



cmk18 wrote:

I want to create a set of sheets that are basically the same, except for
a few changes. But I may need to create new ones on the fly, and I was
curious if there was a way to check if the sheet exists and if it does
go to that sheet, and if it doesn't create a new one. I was planning
on using a naming configuration of ScoringSheet_x and the x would be
selected by the user.

--
cmk18
------------------------------------------------------------------------
cmk18's Profile: http://www.excelforum.com/member.php...fo&userid=6047
View this thread: http://www.excelforum.com/showthread...hreadid=386196


--

Dave Peterson


All times are GMT +1. The time now is 01:41 PM.

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