The last workbook created is not closing....

raghuprabhu

New Member
Joined
Apr 8, 2017
Messages
7
Hi,

The following code goes through the records and saves the workbook as the cell value in the column N.

The only problem is the last workbook created is not saving and is not closing.

Code:
Sub SaveAsString()
Dim i As Integer
Dim lRow As Integer
Dim sPath As String
Dim sFileName As String
Dim wb As Workbook
sPath = ThisWorkbook.Path
lRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row


Application.DisplayAlerts = False
Application.ScreenUpdating = False
    For i = 2 To lRow             
            
            sFileName = Range("N" & i).Value
            ActiveWorkbook.SaveAs filename:=sPath & "\" & sFileName & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True


End Sub

Any suggestions would be helpful.

Thank you

Regards
Raghu
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this (untested):

Code:
Sub SaveAsString()
Dim i As Integer
Dim lRow As Integer
Dim sPath As String
Dim sFileName As String
Dim wb As Workbook
sPath = ThisWorkbook.Path
lRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row


Application.DisplayAlerts = False
Application.ScreenUpdating = False

    With ActiveWorkbook
        For i = 2 To lRow
                
                sFileName = Range("N" & i).Value
               .SaveAs Filename:=sPath & "\" & sFileName & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        Next i
   
Application.ScreenUpdating = True
Application.DisplayAlerts = True
                
                .Close (True)
    End With
End Sub
 
Upvote 0
.............actually, sorry - didn't read your post correctly, I thought you wanted to close the workbook running the code, after the other workbooks were created.
 
Upvote 0
HI Sykes,

I don't want to close the workbook running the code. I want to save and close the last workbook created. It is creating but not saving and closing.

Regards
Raghu
 
Upvote 0

Forum statistics

Threads
1,215,584
Messages
6,125,673
Members
449,248
Latest member
wayneho98

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