Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #11  
Old 02-10-2015, 11:30 PM
excelledsoftware excelledsoftware is offline Insert Sheet via cell value Windows 7 64bit Insert Sheet via cell value Office 2003
IT Specialist
 
Join Date: Jan 2012
Location: Utah
Posts: 455
excelledsoftware will become famous soon enough
Default

Ok here we go. I put in some additional checks since worksheet names can not be over 31 characters or contain certain special characters. Please ensure that you 100% back up your work on all open excel workbooks when running this. It will delete all worksheets that are not in the range and it will not give a prompt before doing this.

Same instructions as above. Let me know if you have any questions.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim RangePass As Boolean
  If Target.Count > 1 Then
    CreateNewWorksheet ("")
    Exit Sub
  End If
  If Target.Row > 4 And Target.Row < 51 Then
    If Target.Column = 4 And Target.Value <> "" Then CreateNewWorksheet (Target.Value)
  End If
  
End Sub
Sub CreateNewWorksheet(UseValue As String)
  Dim wb As Workbook, nws As Worksheet, NewName As String
  Dim ws As Worksheet, cws As Worksheet, CheckString As String
  Dim CheckRow As Integer, CheckName As String, BadData As Variant
  Dim TempString As String, v As Variant
  
  Set wb = ThisWorkbook
  Set cws = wb.ActiveSheet
  BadData = Array("*", "[", "]", "/", "\", "?", "'", ":")
  'Add range names to a string to check
  CheckString = ""
  For CheckRow = 5 To 50
    TempString = Range("D" & CheckRow).Value
    For Each v In BadData
      TempString = Replace(TempString, v, "")
    Next v
    CheckString = CheckString & "[" & TempString & "]"
  Next CheckRow
  CheckString = "[" & cws.Name & "]" & CheckString
  'Delete not found worksheets
  For Each ws In wb.Worksheets
    CheckName = "[" & ws.Name & "]"
    Application.DisplayAlerts = False
    If InStr(1, CheckString, CheckName) = 0 Then
      ws.Delete
    End If
  Next ws
  Application.DisplayAlerts = True
  NewName = UseValue
  'Clear special characters
  For Each v In BadData
    NewName = Replace(NewName, v, "")
  Next v
  If NewName = "" Then End
  If Len(NewName) > 31 Then
    MsgBox "Name too long. no worksheet added."
    End
  End If
  

  If wb.Worksheets.Count > 49 Then
    MsgBox "This workbook can only contain 50 worksheets."
    'If you dont want the messagebox to come up just comment it out with a '
    End
  End If
  For Each ws In wb.Worksheets
    If ws.Name = NewName Then
      MsgBox ("The name " & NewName & " is already in use. No new worksheet added.")
      'You can comment out the message box if you just want code to end.
      End
    End If
  Next ws

  'All passed add the new worksheet
  Set nws = wb.Worksheets.Add(After:=cws)
  nws.Name = NewName
  cws.Activate  'Use a ' right before this line to select the new worksheet.
 
End Sub
Thanks
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Insert Sheet via cell value Help Coloring a cell in Sheet one if data is missing from another sheet Aeducan Excel 1 06-22-2014 04:49 PM
Insert Sheet via cell value How to set a hyperlink from a pp presentation into a CELL from an excel sheet Sabi PowerPoint 1 04-30-2013 06:36 AM
Insert Sheet via cell value Insert Excel sheet markg2 Word 1 12-15-2010 12:19 PM
copy cell from sheet 2 to sheet 3 macro slipperyjim Excel Programming 1 02-18-2010 01:31 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:52 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft