Code to save in multiple spots then close (only close doesn't work)

dsrt16

Board Regular
Joined
Jun 18, 2005
Messages
208
I have a code to save the worksheet in multiple spots---that was the code that took me a while to get right since the folder name is referenced in cells, and it works like a charm.

But the easier part of the code--the simple closing the workbook when done--won't work. It's always sad when the little, easy things don't run. :P

It will save the workbook in all those locations, but it will not clise the workbook.

Code:
strName = Sheet1.Range("F1")

    ActiveWorkbook.SaveAs Filename:="C:\Users\Katie\Desktop\" & Sheet1.Range("E6") & "\ " & strName
    
    ActiveWorkbook.SaveAs Filename:="C:\Users\Katie\Desktop\AI Teachers\" & Sheet1.Range("E10") & "\" & Sheet2.Range("B2") & "\" & strName
                                                                  

    
    If Sheet1.Range("E12") = "" Then
            Exit Sub
    
        Else
           ActiveWorkbook.SaveAs Filename:="C:\Users\Katie\Desktop\AI Program\AI Teachers\" & Sheet1.Range("E12") & "\" & Sheet2.Range("B2") & "\" & strName
    
           If Sheet1.Range("E14") = "" Then
              Exit Sub
    
            Else
               ActiveWorkbook.SaveAs Filename:="C:\Users\Katie\Desktop\AI Program\AI Teachers\" & Sheet1.Range("E14") & "\" & Sheet2.Range("B2") & "\" & strName
    
                If Sheet1.Range("E16") = "" Then
                    Exit Sub
        
                Else
                  ActiveWorkbook.SaveAs Filename:="C:\Users\Katie\Desktop\AI Program\AI Teachers\" & Sheet1.Range("E16") & "\" & Sheet2.Range("B2") & "\" & strName
                End If
            End If
    End If
            
    ActiveWorkbook.Close
    'I also tried ThisWorkbook.Close with no luck either
    
Exit Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How many workbooks are involved?

Does any workbook get closed?

If this code is only for the workbook it's in you should use ThisWorkbook right from the start.
 
Upvote 0
It is only one workbook. The workbook currently open.

I have tried
Code:
ThisWorkbook.Close

But that didn't work either. It goes through all the saving placing multiple copies in the various locations, but it doesn't close when it is done.

Are you saying to use ThisWorkbook on all the line of codes instead of ActiveWorkbook? I will try that.
 
Upvote 0
I changed all the ActiveWorkbook lines to ThisWorkbook.

It did the same thing---saved the workbook in the multiple locations, but did not close.
 
Upvote 0
Are you saying to use ThisWorkbook on all the line of codes instead of ActiveWorkbook?
Yes, that's what Norie meant, and while it should be used that way, he did not mean that to fix the problem of closing. It's just a better coding practice.
What is happening when you run it? (I mean just the close part- not the rest that seems to be working)
Does it bring up the "Do you want to save this file?" window - or does it just do nothing in the way of closing the file?
 
Upvote 0
Does the Close actually get executed?
 
Upvote 0
It just doesn't do anything. It gets done saving to all the spots, and then stops.

I wanted to add in the close line because if I don't, and the teacher just manually closes out, it will tell them to save the changes, but they already saved the workbook and sent it to all the teacher's folders that needed it.

So I just need it to close out.
 
Upvote 0
So you stepped through the code with F8, or set breakpoints, and it never even reached the Close line?
 
Upvote 0
I found the problem. Duh! So simple.

If there is nothing in cell E12 or E14 or E16 then it exits the sub, so it doesn't even get to the close workbook line.

I just need to add the close workbook line before each exit sub in the nested if statements! :D
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

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