Results 1 to 9 of 9

Thread: Elseif macro to skip macro and execute another macro if conditions are not met
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    May 2009
    Posts
    301
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Elseif macro to skip macro and execute another macro if conditions are not met

    I want to create a macro that will create new sheet and save the same if the value in particular cells are >0

    I created one but its only saving one sheet at a time and not moving to second macro if the value in first cell is >0

    Please assist

    Code:
    Sub if_1()If Range("c2").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\01.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
            
    ElseIf Range("c3").Value > 0 Then
           Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\02.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
       
    ElseIf Range("c4").Value > 0 Then
           Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\03.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
       
          
    ElseIf Range("c5").Value > 0 Then
           Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\04.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
       
    End If
    End Sub

  2. #2
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,842
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    You need independent if, in this way each one is evaluated.

    Code:
    Sub if_1()
        If Range("c2").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\01.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If Range("c3").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\02.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If Range("c4").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\03.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If Range("c5").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\04.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
    End Sub
    Regards Dante Amor

  3. #3
    Board Regular
    Join Date
    May 2009
    Posts
    301
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    Quote Originally Posted by DanteAmor View Post
    You need independent if, in this way each one is evaluated.

    Code:
    Sub if_1()
        If Range("c2").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\01.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If Range("c3").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\02.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If Range("c4").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\03.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If Range("c5").Value > 0 Then
            Workbooks.Add
            ChDir "C:\Users\_\Desktop"
            ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\04.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
        End If
    End Sub
    Still not working its saving only 1 sheet

    A 1
    B 0
    C 12
    D 3

    In above example three sheets should create example 1 ,3 & 4

  4. #4
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,842
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    In which cells are your data?
    C2, C3, C4 y C5?

    Or the data is in A1, B1, C1 and D1?
    Regards Dante Amor

  5. #5
    Board Regular
    Join Date
    May 2009
    Posts
    301
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    Data is in cell c column

  6. #6
    Board Regular
    Join Date
    May 2009
    Posts
    301
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    Bump!

  7. #7
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,842
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    is it worked?
    Regards Dante Amor

  8. #8
    Board Regular
    Join Date
    May 2009
    Posts
    301
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    Quote Originally Posted by DanteAmor View Post
    is it worked?
    No . I search on internet and it said that if any one logic is true the macro will end instead of moving to next . Hence i think we need to apply or logic instead of end.

  9. #9
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    6,842
    Post Thanks / Like
    Mentioned
    83 Post(s)
    Tagged
    15 Thread(s)

    Default Re: Elseif macro to skip macro and execute another macro if conditions are not met

    If you need to save a sheet, this instruction: Workbooks.Add, does not create a sheet, it creates a book with a sheet and it is the book that you save with it:

    Code:
    ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\01.xlsx", FileFormat:= _
                xlOpenXMLWorkbook, CreateBackup:=False
    Then when creating a new book, the sheet is blank, the next validation is done on the new book If Range ("c3"). Value> 0. the new book is blank so you do not create the next book.


    Then you can:
    a) Close the new book
    b) Return to the book with the macro
    c) Make reference to the book and the sheet of the book with the macro


    I present the 3 options:

    Option a)

    Code:
    Sub if_1_option_a()
        Dim wPath As String
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        wPath = "C:\Users\_\Desktop\"
        
        If Range("c2").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "01.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWorkbook.Close
        End If
        If Range("c3").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "02.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWorkbook.Close
        End If
        If Range("c4").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "03.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWorkbook.Close
        End If
        If Range("c5").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "04.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWorkbook.Close
        End If
    End Sub
    Option b)

    Code:
    Sub if_1_option_b()
        Dim wPath As String
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        wPath = "C:\Users\_\Desktop\"
        
        If Range("c2").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "01.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ThisWorkbook.Activate
        End If
        If Range("c3").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "02.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ThisWorkbook.Activate
        End If
        If Range("c4").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "03.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ThisWorkbook.Activate
        End If
        If Range("c5").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "04.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ThisWorkbook.Activate
        End If
    End Sub

    Option c)

    Code:
    Sub if_1_option_c()
        Dim wPath As String
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        wPath = "C:\Users\_\Desktop\"
    
    
        If ThisWorkbook.ActiveSheet.Range("c2").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "01.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If ThisWorkbook.ActiveSheet.Range("c3").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "02.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If ThisWorkbook.ActiveSheet.Range("c4").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "03.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        End If
        If ThisWorkbook.ActiveSheet.Range("c5").Value > 0 Then
            Workbooks.Add
            ActiveWorkbook.SaveAs Filename:=wPath & "04.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        End If
    End Sub

    Choose the one that suits you
    Regards Dante Amor

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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