Conditonal Statements for Worksheets

ngkf7

New Member
Joined
Jul 23, 2007
Messages
11
Hi,

I am fairly new to Macros and VBA and have been learning a lot over the past 2 days...I was just wondering is there a way I can check to see if a specific worksheet exists in my workbook, and if it does, select that one, and if it doesnt, to create one?


Thanks
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,456
is there a way I can check to see if a specific worksheet exists in my workbook, and if it does, select that one, and if it doesnt, to create one?
Hello ngkf7, welcome to the board.
Quick little example:
Code:
Sub SelectOrCreateSheet()
On Error Resume Next
Sheets("YourSheetName").Activate
If Err.Number <> 0 Then Sheets.Add.Name = "YourSheetName": Err.Clear
End Sub

Hope it helps.
 

ngkf7

New Member
Joined
Jul 23, 2007
Messages
11
Thanks

Thanks, I hope that solves my problem. I'll be able to find out in a bit. On another note though, I'm trying to elaborate on that condition now, so if the page exists, then there is probably already data on it, so I need to find the last row that doesnt have data, then paste a selection onto the next row.
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,456
OK, this will select (or create & select) a sheet named "YourSheetName" and then determine
the first unused row in column A, then copy a range from the source sheet to that row.
Code:
Sub SelectOrCreateSheet()
Dim NxtRw&
On Error Resume Next
Sheets("YourSheetName").Activate
If Err.Number <> 0 Then Sheets.Add.Name = "YourSheetName": Err.Clear

NxtRw = Sheets("YourSheetName").Cells(Rows.Count, "A").End(xlUp)(2).Row

Sheets("YourCopySourceSheet").Range("YourCopySourceRange").Copy _
  Sheets("YourSheetName").Cells(NxtRw, "A")

End Sub

My question though is, will that selected/created sheet always be named the same, or will
you want the user to be able to input the name of the sheet they want to select/create?
 

Forum statistics

Threads
1,181,647
Messages
5,931,208
Members
436,784
Latest member
amuljono

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
Top