LiveLessons Excel VBA DVD - code error

Giga_Me

New Member
Joined
Jun 14, 2011
Messages
15
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:

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
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
I wonder if you have other code in the workbook (or add-ins) that is causing the problem.
Try declaring the variables:
Code:
Dim RowCount as Long
Dim Lastrow as Long
Dim StartRow as Long
 
Derek I thought about declaring the values also. I copied the code from Bill's accompanying spreadsheet so there shouldn't have been extraneous code...I will review later. Also, because the RowCount variable only appears once and is not referenced again in the code, I'm wondering if it's even necessary...?
 
Yes, you are right about the variable only appearing once and my guess is that it may have been code 'left over' from some diagnostic code.
I mentioned Add-Ins because I have experienced problems in the past where, in order to function as intended, variables etc. have been declared as global/public.
The code compiled OK for me.
 

Forum statistics

Threads
1,222,729
Messages
6,167,883
Members
452,154
Latest member
lukmana_sam

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