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

earthworm

Well-known Member
Joined
May 19, 2009
Messages
773
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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
 
Upvote 0
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

[TABLE="width: 128"]
<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]A[/TD]
[TD="width: 64, align: right"]1[/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD="align: right"]12[/TD]
[/TR]
[TR]
[TD]D[/TD]
[TD="align: right"]3[/TD]
[/TR]
</tbody>[/TABLE]

In above example three sheets should create example 1 ,3 & 4
 
Upvote 0
In which cells are your data?
C2, C3, C4 y C5?

Or the data is in A1, B1, C1 and D1?
 
Upvote 0
:confused: 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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top