I am using VBA for Excel 2016 and keep getting the following error:
Run-time error '1004':
Application-defined or object-defined error
Here is my code:
Sub Macro2()
Dim wbk As Workbook
Dim countermax As Integer
Dim ws As String
Dim i As Integer 'for counter
Dim x As Integer 'for serial number of month
Dim m As Long
Dim macro As Worksheet
Set wbk = ThisWorkbook
Set macro = wbk.Worksheets("Macro")
countermax = wbk.Worksheets("macro").Range("A1")
i = 2
x = wbk.Worksheets("macro").Range("D2")
y = 12 - x
z = y - 1
Do While i < countermax
ws = wbk.Worksheets("macro").Range("B" & i)
**m = wbk.Worksheets(ws).Range(Range("A9").End(xlDown), Range("A9").End(xlDown).End(xlDown)).Rows.Count**
Worksheets(ws).Range(Range("A9").End(xlDown), Range("A9").End(xlDown).Offset(m, 0)).EntireRow.Select
Selection.Rows.Hidden = False
On Error Resume Next
Selection.Rows.Ungroup
If y <> 0 Then
Range("A9").Select
Selection.End(xlDown).Select
Selection.End(xlDown).Select
Range(Range("A9").End(xlDown), Range("A9").End(xlDown).Offset(-z, 0)).EntireRow.Select
Selection.Rows.Group
Selection.Rows.Hidden = True
Else: End If
i = i + 1
Loop
End Sub
Debugging this code highlights the line in **.
Every month I have to manually go through each tab in a workbook and ungroup + unhide the next hidden row in a particular range that represents the months of the year. What I'd like to do is have a macro go through every worksheet that has its name listed in the macro worksheet and unhide the next month's row. Because not every worksheet has the same number of rows (e.g. the first month may be January for one worksheet, March for another), I'm trying to have Excel count the number of rows and store that as a data type so it knows how many to hide.
When I hover my cursor over the "m" variable, it tells me "m = 0". Can someone please explain to me why it's not giving me the row count?