VBA: Using Elseif to decide which folder to save the workbook

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
55
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,

Im hoping you can help with the following. I am attempting to use a reference in cell A4 to decide where to save the spreadsheet. I am getting an error on my first Elseif saying Compile error: Else without if

Thank you in advance

VBA Code:
Sub Save()

Dim FileName As String
Dim Folder1
Dim Folder2
Dim Folder3

FileName =  ThisWorkbook.Worksheets("Summary").Range("A1")
Folder1 = "C:\Users\Flow\Desktop\Test\Monday\" 
Folder2 = "C:\Users\Flow\Desktop\Test\Tuesday\" 
Folder3 = "C:\Users\Flow\Desktop\Test\Wednesday\" 

If ThisWorkbook.Worksheets("Summary").Range("A4") = "Monday" Then ActiveWorkbook.SaveAs Folder1 & FileName & ".xlsm", 52

ElseIf ThisWorkbook.Worksheets("Summary").Range("A4") = "Tuesday" Then ActiveWorkbook.SaveAs Folder2 & FileName & ".xlsm", 52

ElseIf ThisWorkbook.Worksheets("Summary").Range("A4") = "Wednesday" Then ActiveWorkbook.SaveAs Folder3 & FileName & ".xlsm", 52

Else: Call Manual_Save

End If

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
It should be like this
Excel Formula:
Sub Save()

Dim FileName As String
Dim Folder1
Dim Folder2
Dim Folder3

FileName = ThisWorkbook.Worksheets("Summary").Range("A1")
Folder1 = "C:\Users\Flow\Desktop\Test\Monday\"
Folder2 = "C:\Users\Flow\Desktop\Test\Tuesday\"
Folder3 = "C:\Users\Flow\Desktop\Test\Wednesday\"

If ThisWorkbook.Worksheets("Summary").Range("A4") = "Monday" Then
   ActiveWorkbook.SaveAs Folder1 & FileName & ".xlsm", 52

ElseIf ThisWorkbook.Worksheets("Summary").Range("A4") = "Tuesday" Then
   ActiveWorkbook.SaveAs Folder2 & FileName & ".xlsm", 52

ElseIf ThisWorkbook.Worksheets("Summary").Range("A4") = "Wednesday" Then
   ActiveWorkbook.SaveAs Folder3 & FileName & ".xlsm", 52

Else: Call Manual_Save

End If

End Sub
Although I would strongly suggest you change the name of the Sub, otherwise you could get problems elsewhere.
Never use VBA keywords as the names of variables or procedures.
 
Upvote 0
Solution
Hi Fluff,

Thank you for your help that works perfectly. I will keep in mind my naming conventions, thanks for the heads up!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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