ziad alsayed
Well-known Member
- Joined
- Jul 17, 2010
- Messages
- 665
dear all
i am facing a problem with the below code when reaching the line of saving the workbook . Appreciate if you assist in sorting out the problem
i am facing a problem with the below code when reaching the line of saving the workbook . Appreciate if you assist in sorting out the problem
Code:
Sub CreateWorkbooks()
Dim WBO As Workbook ' original workbook
Dim WBN As Workbook ' new workbook
Dim WSO As Worksheet ' original worksheet
Dim WSN As Worksheet ' new worksheet
Set WBO = ActiveWorkbook
Set WSO = ActiveSheet
' filter to get all the None
Range("B1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$F$500000").AutoFilter Field:=2, Criteria1:="None"
'find finalrow
finalrow = WSO.Cells(Rows.Count, 1).End(xlUp).Row + 1
LastBranch = Cells(2, 3)
StartRow = 2
For i = 2 To finalrow
ThisBranch = WSO.Cells(i, 3)
If ThisBranch = LastBranch Then
' do nothing
Else
' We have a new branch
' Copy all of the previous rows to a new workbook
LastRow = i - 1
RowCount = LastRow - StartRow + 1
' Create a new workbook.
Set WBN = Workbooks.Add(Template:=xlWBATWorksheet)
Set WSN = WBN.Worksheets(1)
' Set up the headings for the report
WSN.Cells(1, 1).Value = "Code"
WSN.Cells(1, 2).Value = "Profile"
WSN.Cells(1, 3).Value = "Branch"
WSN.Cells(1, 4).Value = "Description"
' copy all of the records for this branch
WSO.Range(WSO.Cells(StartRow, 1), WSO.Cells(LastRow, 4)).Copy Destination:=WSN.Cells(2, 1)
[COLOR=red] ' save WBN
' All WBN should be Saved In G:\Nigeria Files\Top ten\No Profile
FN = LastBranch & ".xls"
FP = WBO.Path & Application.PathSeparator & "No Profile" & Application.PathSeparator
WBN.SaveAs Filename:=FP & FN
[/COLOR] WBN.Close SaveChanges:=False
LastBranch = ThisBranch
StartRow = i
End If
Next i
End Sub