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

earthworm

Active Member
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
 

DanteAmor

Well-known Member
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
[COLOR=#0000ff]    End If[/COLOR]
    If Range("c3").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\02.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
[COLOR=#0000ff]    End If[/COLOR]
    If Range("c4").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\03.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
[COLOR=#0000ff]    End If[/COLOR]
    If Range("c5").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\04.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
[COLOR=#0000ff]    End If[/COLOR]
End Sub
 

earthworm

Active Member
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
[COLOR=#0000ff]    End If[/COLOR]
    If Range("c3").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\02.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
[COLOR=#0000ff]    End If[/COLOR]
    If Range("c4").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\03.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
[COLOR=#0000ff]    End If[/COLOR]
    If Range("c5").Value > 0 Then
        Workbooks.Add
        ChDir "C:\Users\_\Desktop"
        ActiveWorkbook.SaveAs Filename:="C:\Users\_\Desktop\04.xlsx", FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
[COLOR=#0000ff]    End If[/COLOR]
End Sub
Still not working its saving only 1 sheet

A1
B0
C12
D3

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>

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

DanteAmor

Well-known Member
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
 

Some videos you may like

This Week's Hot Topics

  • Get External Data (long shot question!)
    This is likely a long shot but I am wondering if it is at all possible for Excel to somehow 'change' the contents of a URL that is being linked to...
  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • Cell Formatting
    Good Morning, I need to format a few different cells in the following manners: A1 has to always add a colon (:) after whatever is typed in by a...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • Workbook_Change stopped working !
    I am working on an app to speed up & automate processing of Credit Cards statements. After data is input from a CSV file, it is presented to the...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
Top