Hi - I have this CD but have a problem with the code presented in Lesson 31.
I get a compile error "Invalid use of property" on this line of code:
The code works in the video and I don't think the RowCount variable is necessary...? Full code here:
I get a compile error "Invalid use of property" on this line of code:
Code:
RowCount = Lastrow - StartRow+1
The code works in the video and I don't think the RowCount variable is necessary...? Full code here:
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
FinalRow = WSO.Cells(Rows.Count, 1).End(xlUp).Row + 1
LastDept = Cells(2, 1)
StartRow = 2
For i = 2 To FinalRow
ThisDept = WSO.Cells(i, 1)
If ThisDept = LastDept Then
' do nothing
Else
' We have a new department starting
' 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 = "Budget Report"
WSN.Cells(1, 1).Font.Size = 14
WSN.Cells(2, 1).Value = LastDept & " - " & WSO.Cells(StartRow, 2)
WSO.Range("C1:Q1").Copy Destination:=WSN.Cells(4, 1)
' copy all of the records for this department
WSO.Range(WSO.Cells(StartRow, 3), WSO.Cells(LastRow, 17)).Copy Destination:=WSN.Cells(5, 1)
FN = LastDept & ".xlsx"
FP = WBO.Path & Application.PathSeparator & "Budget" & Application.PathSeparator
WBN.SaveAs Filename:=FP & FN
WBN.Close SaveChanges:=False
LastDept = ThisDept
StartRow = i
End If
Next i
End Sub