Consulting

Results 1 to 3 of 3

Thread: Creating Sheets from Cell Value IF Only!

  1. #1

    Creating Sheets from Cell Value IF Only!

    I'm trying to create sheets from cell values only if they are not blank or contain "#N/A".

    For example, I have Q3:Q38 that contain values I want to turn into sheets automatically. However, some of "#N/A" and others are blank. I do not wan tot create sheets for those ones.


    Column Q
    31114A
    36516A



    #N/A
    22492A

    22334A
    #N/A

    So the end result would be 4 sheets created, "31114A", "36516A", "22492A" and "22334A".

    Here is some code I found online that does this except it creates the blanks and #N/A sheets as well.


    Sub AddSheets()
    Dim cell As Excel.Range
    Dim wsWithSheetNames As Excel.Worksheet
    Dim wbToAddSheetsTo As Excel.Workbook
    Set wsWithSheetNames = ActiveSheet
    Set wbToAddSheetsTo = ActiveWorkbook
    For Each cell In wsWithSheetNames.Range("Q3:Q38")
        With wbToAddSheetsTo
            .Sheets.Add after:=.Sheets(.Sheets.Count)
            On Error Resume Next
            ActiveSheet.Name = cell.Value
            If Err.Number = 1004 Then
              Debug.Print cell.Value & " already used as a sheet name"
            End If
            On Error GoTo 0
        End With
    Next cell
    End Sub
    Any help would be appreciated.

    Thanks.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    try:
    Sub AddSheets()
    Dim cll As Excel.Range
    Dim wsWithSheetNames As Excel.Worksheet
    Dim wbToAddSheetsTo As Excel.Workbook
    Set wsWithSheetNames = ActiveSheet
    Set wbToAddSheetsTo = ActiveWorkbook
    For Each cll In wsWithSheetNames.Range("Q3:Q38")
    If Not (Application.IsNA(cll) Or Len(cll.Text) = 0) Then
      With wbToAddSheetsTo
        .Sheets.Add after:=.Sheets(.Sheets.Count)
        On Error Resume Next
        ActiveSheet.Name = cll.Value
        If Err.Number = 1004 Then
          Debug.Print cll.Value & " already used as a sheet name"
        End If
        On Error GoTo 0
      End With
      End If
    Next cll
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Worked flawlessly! Thank you so much. I'm sure I will have some more questions soon. I am pretty familiar with C++ programming concepts (academic version anyways) so I hope I can pick this stuff up fast. The problem I have is I was trying to get this implemented ASAP. That worked great.

    Thank you again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •