This Macro halts any ideas what could be wrong

P

pano

Hi all
Running this macro from a button on another sheet.
Now comes up with Runtime error 1004
Unable to set the hidden property of the range class?

This code is supposed to check a petrol sheet down one column and if
there is no entry hide that row when it comes to print the monthly
sheet.
Sub Hide_Print_Unhide()

'Petrol Sheet Macro
Dim rw As Long
Application.ScreenUpdating = False
With Sheets("carlog")
.Visible = -1
For rw = 10 To 44
If .Cells(rw, 10).Value = "" Then _
.Rows(rw).Hidden = True ' Here is where it halts
on
Next rw
.PrintPreview ' for testing use .PrintPreview
.Range("j10:j44").EntireRow.Hidden = False
.Visible = -1
End With
Application.ScreenUpdating = True
Worksheets("printMenu").Select

End Sub


Thanks for looking Stephen
 
D

Dave Peterson

Any chance that the worksheet is protected?
Hi all
Running this macro from a button on another sheet.
Now comes up with Runtime error 1004
Unable to set the hidden property of the range class?

This code is supposed to check a petrol sheet down one column and if
there is no entry hide that row when it comes to print the monthly
sheet.
Sub Hide_Print_Unhide()

'Petrol Sheet Macro
Dim rw As Long
Application.ScreenUpdating = False
With Sheets("carlog")
.Visible = -1
For rw = 10 To 44
If .Cells(rw, 10).Value = "" Then _
.Rows(rw).Hidden = True ' Here is where it halts
on
Next rw
.PrintPreview ' for testing use .PrintPreview
.Range("j10:j44").EntireRow.Hidden = False
.Visible = -1
End With
Application.ScreenUpdating = True
Worksheets("printMenu").Select

End Sub

Thanks for looking Stephen
 
P

pano

Any chance that the worksheet is protected?










--

Dave Peterson- Hide quoted text -

- Show quoted text -

Yes it is protected , I was reading the old stuff and saw that could
be a possible reason but it still comes up with the amended code??
Sub Hide_Print_Unhide()
'Petrol Sheet Macro
Dim rw As Long
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:="123"
With Sheets("carlog")
.Visible = -1
For rw = 10 To 44
If .Cells(rw, 10).Value = "" Then _
.Rows(rw).Hidden = True
Next rw
.PrintOut ' for testing use .PrintPreview
.Range("j10:j44").EntireRow.Hidden = False
.Visible = -1
End With
Application.ScreenUpdating = True
ActiveSheet.Protect Password:="123"
Worksheets("printMenu").Select
End Sub
 
D

Dave Peterson

My first guess is that since you're unprotecting the activesheet, then maybe
carlog isn't the
activesheet (especially if it's hidden???).

So move the .unprotect and .protect into the With/End with structure:

Sub Hide_Print_Unhide()
'Petrol Sheet Macro
Dim rw As Long
Application.ScreenUpdating = False
With Sheets("carlog")
.unprotect password:="123"
.Visible = -1
For rw = 10 To 44
If .Cells(rw, 10).Value = "" Then _
.Rows(rw).Hidden = True
Next rw
.PrintOut ' for testing use .PrintPreview
.Range("j10:j44").EntireRow.Hidden = False
.Visible = -1
.protect password:="123"
End With
Application.ScreenUpdating = True
Worksheets("printMenu").Select
End Sub
 
P

pano

My first guess is that since you're unprotecting the activesheet, then maybe
carlog isn't the
activesheet (especially if it's hidden???).

So move the .unprotect and .protect into the With/End with structure:

Sub Hide_Print_Unhide()
'Petrol Sheet Macro
Dim rw As Long
Application.ScreenUpdating = False
With Sheets("carlog")
.unprotect password:="123"
.Visible = -1
For rw = 10 To 44
If .Cells(rw, 10).Value = "" Then _
.Rows(rw).Hidden = True
Next rw
.PrintOut ' for testing use .PrintPreview
.Range("j10:j44").EntireRow.Hidden = False
.Visible = -1
.protect password:="123"
End With
Application.ScreenUpdating = True
Worksheets("printMenu").Select
End Sub





panowrote:



--

Dave Peterson- Hide quoted text -

- Show quoted text -

Thanks Dave you got it I had put the unprotect and protect in the
wrong spot thanks for that
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top