+ Reply to Thread
Results 1 to 4 of 4

Modify code to skip creation of a sheet and move on to the next instead of making another

Hybrid View

Lmao Modify code to skip creation... 04-19-2019, 03:43 AM
BadlySpelledBuoy Re: Modify code to skip... 04-19-2019, 04:39 AM
Lmao Re: Modify code to skip... 04-19-2019, 06:41 AM
BadlySpelledBuoy Re: Modify code to skip... 04-19-2019, 08:34 AM
  1. #1
    Registered User
    Join Date
    08-26-2011
    Location
    California, United States
    MS-Off Ver
    Excel 2016
    Posts
    45

    Post Modify code to skip creation of a sheet and move on to the next instead of making another

    So I have a code I am using that makes sheets and based on the values in a column. I need to modify it to check the same names of sheets if it finds one that doesn't already exist it makes a new one. If it does find that the sheet exists it skips it instead of making another. I tried modifying it but I can only get it to duplicate the entire document to add just one new sheet.

    
    
    Sub ExportDataCreateDatabase()
       Dim Cl As Range
       Dim Ws As Worksheet
       
       
       
       Application.ScreenUpdating = False
       
       Set Ws = Worksheets("DATABASE")
     
       For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
          Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
          With Sheets(CStr(Cl.Value))
             Ws.Hyperlinks.Add Cl, "", .Range("A1").Address(0, 0, , 1)
             Ws.Range("A1:K1").Copy .Range("A1:g1")
             .Hyperlinks.Add .Range("M1"), "", "MAINPAGE!A1", , "MAINPAGE"
             .Hyperlinks.Add .Range("N1"), "", "DATABASE!A1", , "DATABASE"
             .Range("A2:K2").Formula = "=" & Cl.Offset(, 0).Address(0, 0, , 1)
             .Range("A4:F4").Value = Array("Date", "Disposition", "Added", "Removed", "Issued", "Surplus")
              .Range("A1:F1").Copy
              .Range("A4:F4").Select
               Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
               SkipBlanks:=False, Transpose:=False
              Application.CutCopyMode = False
              Columns("A:O").EntireColumn.AutoFit
              ActiveWindow.DisplayGridlines = False
               
               
               
              Range("A1:K2").Select
        Range("K2").Activate
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        
        
        Range("A4:F29").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlEdgeRight)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideVertical)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With Selection.Borders(xlInsideHorizontal)
            .LineStyle = xlContinuous
            .ColorIndex = 0
            .TintAndShade = 0
            .Weight = xlThin
        End With
        Rows("5:5").Select
        ActiveWindow.FreezePanes = True
          End With
           
           
        Range("A5:F125").Select
       Selection.Locked = False
        Range("M1:N1").Select
        Selection.Locked = False
        Selection.FormulaHidden = False
        ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
        ActiveSheet.EnableSelection = xlUnlockedCells
        ActiveWindow.DisplayGridlines = False
        ActiveWindow.DisplayHeadings = False
        
        
        
       Next Cl
       
    End Sub
    Last edited by Lmao; 04-19-2019 at 06:42 AM.

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    8,058

    Re: Modify code to skip creation of a sheet and move on to the next instead of making anot

    Your code could be tidied up a lot, but I'm about to go out so don't have time to do that right now for you. I may come back to it later though.

    In the mean time, try the code below. I think it does what you mean.
    The bits in red I've added to your existing code.

    Sub ExportDataCreateDatabase()
        Dim Cl As Range
        Dim Ws As Worksheet
        Dim WSTest As Worksheet
        Dim shtname As String
        
        Application.ScreenUpdating = False
    
        Set Ws = Worksheets("DATABASE")
    
        For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            Set WSTest = Nothing
            shtname = Cl.Value
            On Error Resume Next
            Set WSTest = ThisWorkbook.Sheets(shtname)
            On Error GoTo 0
            If WSTest Is Nothing Then
                Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
                With Sheets(CStr(Cl.Value))
                    Ws.Hyperlinks.Add Cl, "", .Range("A1").Address(0, 0, , 1)
                    Ws.Range("A1:K1").Copy .Range("A1:g1")
                    .Hyperlinks.Add .Range("M1"), "", "MAINPAGE!A1", , "MAINPAGE"
                    .Hyperlinks.Add .Range("N1"), "", "DATABASE!A1", , "DATABASE"
                    .Range("A2:K2").Formula = "=" & Cl.Offset(, 0).Address(0, 0, , 1)
                    .Range("A4:F4").Value = Array("Date", "Disposition", "Added", "Removed", "Issued", "Surplus")
                    .Range("A1:F1").Copy
                    .Range("A4:F4").Select
                    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
                    SkipBlanks:=False, Transpose:=False
                    Application.CutCopyMode = False
                    Columns("A:O").EntireColumn.AutoFit
                    ActiveWindow.DisplayGridlines = False
                 
                    Range("A1:K2").Select
                    Range("K2").Activate
                    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
                    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
                    With Selection.Borders(xlEdgeLeft)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlEdgeTop)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlEdgeBottom)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlEdgeRight)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlInsideVertical)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlInsideHorizontal)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                
                    Range("A4:F29").Select
                    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
                    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
                    With Selection.Borders(xlEdgeLeft)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlEdgeTop)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlEdgeBottom)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlEdgeRight)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlInsideVertical)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    With Selection.Borders(xlInsideHorizontal)
                        .LineStyle = xlContinuous
                        .ColorIndex = 0
                        .TintAndShade = 0
                        .Weight = xlThin
                    End With
                    Rows("5:5").Select
                    ActiveWindow.FreezePanes = True
                End With
                
                Range("A5:F125").Select
                Selection.Locked = False
                Range("M1:N1").Select
                Selection.Locked = False
                Selection.FormulaHidden = False
                ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
                ActiveSheet.EnableSelection = xlUnlockedCells
                ActiveWindow.DisplayGridlines = False
                ActiveWindow.DisplayHeadings = False
            End If
        Next Cl
    End Sub
    BSB

  3. #3
    Registered User
    Join Date
    08-26-2011
    Location
    California, United States
    MS-Off Ver
    Excel 2016
    Posts
    45

    Re: Modify code to skip creation of a sheet and move on to the next instead of making anot

    This does what I need. Thank you. Im just learning code I'm below beginners level so excuse the ugliness of the code lol.

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    8,058

    Re: Modify code to skip creation of a sheet and move on to the next instead of making anot

    Happy to help.

    Scruffy/ugly code is fine so long as YOU understand it. I will say that properly indented code, although makes no difference to the running of it, does make it much easier to read when it comes to debugging.

    It would appear that some of your code was put together with the macro recorder. Whilst it's a great tool it often produces unnecessarily lengthy code.
    For example, you have something like 80 lines of code to add borders, inside and out, to two ranges. The result of those 80 lines can be accomplished by using just one line:
    Range("A1:K2,A4:F29").Borders.LineStyle = xlContinuous
    Below is a slightly rewritten version of your code to make things a little shorter. It could possibly be shortened further but difficult to do anymore without seeing your workbook to tell what some of the lines are doing.
    Sub ExportDataCreateDatabase()
        Dim Cl As Range
        Dim Ws As Worksheet
        Dim WSTest As Worksheet
        
        Application.ScreenUpdating = False
    
        Set Ws = Worksheets("DATABASE")
    
        For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
            Set WSTest = Nothing
            On Error Resume Next
            Set WSTest = ThisWorkbook.Sheets(Cl.Text)
            On Error GoTo 0
            If WSTest Is Nothing Then
                Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Text
                With Sheets(Cl.Text)
                    Ws.Hyperlinks.Add Cl, "", .Range("A1").Address(0, 0, , 1)
                    Ws.Range("A1:K1").Copy .Range("A1:G1")
                    .Hyperlinks.Add .Range("M1"), "", "MAINPAGE!A1", , "MAINPAGE"
                    .Hyperlinks.Add .Range("N1"), "", "DATABASE!A1", , "DATABASE"
                    .Range("A2:K2").Formula = "=" & Cl.Offset(, 0).Address(0, 0, , 1)
                    .Range("A4:F4").Value = Array("Date", "Disposition", "Added", "Removed", "Issued", "Surplus")
                    .Range("A1:F1").Copy
                    .Range("A4:F4").PasteSpecial Paste:=xlPasteFormats
                    Application.CutCopyMode = False
                    Columns("A:O").EntireColumn.AutoFit
                 
                    Range("A1:K2,A4:F29").Borders.LineStyle = xlContinuous
                    
                    Rows("5:5").Select
                    ActiveWindow.FreezePanes = True
                End With
                
                With Range("A5:F125,M1:N1")
                    .Locked = False
                    .FormulaHidden = False
                End With
                
                With ActiveSheet
                    .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
                    .EnableSelection = xlUnlockedCells
                End With
                
                With ActiveWindow
                    .DisplayGridlines = False
                    .DisplayHeadings = False
                End With
            End If
        Next Cl
    End Sub
    Good luck with learning to code. It's an interesting journey and there's a whole forum of people here to help you along if/when you get stuck.

    BSB

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] assistance to modify vba code existing code sheets name to sheet tab names in filtering
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2018, 03:58 AM
  2. Making the sheet move, not the cell
    By Ady2673 in forum Excel General
    Replies: 2
    Last Post: 04-05-2017, 02:00 PM
  3. [SOLVED] Need to modify VBA code to move Entries between Worksheets based on Drop-down Entry
    By loydancer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-02-2015, 01:56 AM
  4. Need to modify code to skip blank rows and headings
    By missit in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-05-2015, 08:18 PM
  5. [SOLVED] Help needed making portions of a drag down/multi sheet data pull formula skip cells
    By vmackie in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 02-21-2014, 05:14 PM
  6. Move rows up in the sheet and skip printing
    By flavio in forum Excel General
    Replies: 1
    Last Post: 07-17-2013, 09:30 PM
  7. [SOLVED] Move or copy sheet causing the creation of an htm file
    By drlogarithm in forum Excel General
    Replies: 0
    Last Post: 11-07-2005, 01:10 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1