desperately put validation if statement in VBA

Flicker

New Member
Joined
Feb 19, 2009
Messages
39
Hi sorry about this question. It seems very easy but I've spent several hour to bang my head and I can't figure it out.

I want to insert multiple worksheet with VBA by refer range of cell and use text from those range as a sheet name. I did search and found VBA code that I can use.

However, I want to add a validation -- if the sheet name is exist, do not add any sheet. just skip it but I can't insert the simple IF statement in VBA due to lack of VBA knowledge. Moreover, I try to move On Error Resume Next above Sheets.Add and hope On Error GoTo 0 would negate the Sheets.Add step. However, VBA still create sheet with default name (e.g. Sheet15) which is not what I expect.

Code:
Sub AddSheets()
'Updateby Extendoffice 20161215
    Dim xRg As Excel.Range
    Dim wSh As Excel.Worksheet
    Dim wBk As Excel.Workbook
    Set wSh = ActiveSheet
    Set wBk = ActiveWorkbook
    Application.ScreenUpdating = False
    For Each xRg In wSh.Range("E2:E9")
        With wBk
        'if the sheet with that name exist, should not add sheet.
        
        
        'else add sheet.
        
            .Sheets.Add after:=.Sheets(.Sheets.Count)
            On Error Resume Next
            ActiveSheet.Name = xRg.Value
            If Err.Number = 1004 Then
              Debug.Print xRg.Value & " already used as a sheet name"
            End If
            On Error GoTo 0
        End With
    Next xRg
    Application.ScreenUpdating = True
End Sub
could someone point me what I misunderstand here?

Thank you very much
 
Last edited:

Some videos you may like

Excel Facts

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

Flicker

New Member
Joined
Feb 19, 2009
Messages
39
It's seems working now. but any suggestions or improvements always welcome.

Code:
Sub AddSheets()
'Updateby Extendoffice 20161215
    Dim xRg As Excel.Range
    Dim wSh As Excel.Worksheet
    Dim wBk As Excel.Workbook
    
    Set wSh = ActiveSheet
    Set wBk = ActiveWorkbook
    Application.ScreenUpdating = False
    
    For Each xRg In wSh.Range("E2:E9")
        
        For i = 1 To Worksheets.Count
            If Worksheets(i).Name = xRg.Value Then
                exists = True
            End If
        Next i
            
        If Not exists Then
            
            With wBk
                .Sheets.Add after:=.Sheets(.Sheets.Count)
                On Error Resume Next
                ActiveSheet.Name = xRg.Value
                If Err.Number = 1004 Then
                  Debug.Print xRg.Value & " already used as a sheet name"
                End If
                On Error GoTo 0
            End With
            
        End If
            
    exists = False
    Next xRg
    Application.ScreenUpdating = True
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,423
Office Version
365
Platform
Windows
This assumes that the values in E are valid sheet names
Code:
Sub Flicker()
   Dim Cl As Range
   
   For Each Cl In Range("E2:E9")
      If Cl <> "" Then
         If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
         End If
      End If
   Next Cl
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,101,904
Messages
5,483,646
Members
407,399
Latest member
Rakeforms

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top