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
 
Ahah! I was wondering if one of those Exit Sub statements might be the problem. Just didn't want to ask something that seemed so obvious.
(Sorry, guess I should've asked.)
 
Upvote 0

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
That might work but you could try something like this:
Code:
  strPath = "C:\Users\Katie\Desktop\AI Teachers\"
 
    For I = 10 To 16
        If Sheet1.Range("E" & I) <> "" Then
            wb.SaveAs Filename:=strPath & Sheet1.Range("E" & I) & "\" & Sheet2.Range("B2") & "\" & strname
        End If
    Next I
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,852
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