desperately put validation if statement in VBA

Flicker

New Member
Joined
Feb 19, 2009
Messages
40
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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Flicker

New Member
Joined
Feb 19, 2009
Messages
40
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
44,555
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
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,032
Messages
5,508,905
Members
408,699
Latest member
Danlask

This Week's Hot Topics

  • Turn fraction around
    Hello I need to turn a fraction around, for example I have 1/3 but I need to present as 3/1
  • TIme Clock record reformatting to ???
    Hello All, I'd like some help formatting this (Tbl-A)(Loaded via Power Query) [ATTACH type="full" width="511px" alt="PQdata.png"]22252[/ATTACH]...
  • TextBox Match
    hi, I am having a few issues with my code below, what I need it to do is when they enter a value in textbox8 (QTY) either 1,2 or 3 the 3 textboxes...
  • Using Large function based on Multiple Criteria
    Hello, I can't seem to get a Large formula to work based on two criteria's. I can easily get a oldest value based one value, but I'm struggling...
  • Can you check my code please
    Hi, Im going round in circles with a Compil Error End With Without With Here is the code [CODE=rich] Private Sub...
  • Combining 2 pivot tables into 1 chart
    Hello everyone, My question sounds simple but I do not know the answer. I have 2 pivot tables and 2 charts that go with this. However I want to...
Top