Print Dialogue by Hiding Empty Rows

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Print Dialogue by Hiding Empty Rows

Post by adam »

Hi anyone,

I'm using the following code to hide rows if the column F is empty and open the print dialogue. The code works fine if the last row(column F37 is empty) but if F37 is filled with data I get the debug message.

How may I prevent this? I have formulas in column H,K,M & O

Any help on this would be kindly appreciated.

Code: Select all

Sub PrintDialogByHidingEmptyRows()
Application.ScreenUpdating = False
Worksheets("MySheet").Range("F18:F37").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
Application.Dialogs(xlDialogPrint).Show
Application.ScreenUpdating = True
End Sub
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78647
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Dialogue by Hiding Empty Rows

Post by HansV »

Create a loop:

Code: Select all

  Dim r As Long
  For r = 18 To 37
    With Worksheets("MySheet").Range("F" & r)
      If .Value = "" Then
        .EntireRow.Hidden = True
      End If
    End With
  Next r
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

Thanks for the help Hans. I do really appreciate it.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

At times when the user accidentally clicks the print macro without filling a single column between the rows F18 to F37; all the rows get hidden and the sheet gets printed.

How could a message that says "you must fill at least one field to print the sheet be created with such a scenario".

Any help on this would be kindly appreciated.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78647
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Dialogue by Hiding Empty Rows

Post by HansV »

Insert the following above the For ... loop:

Code: Select all

  If Application.WorksheetFunction.CountA(Worksheets("MySheet").Range("F18:F37")) = 0 Then
    MsgBox "you must fill at least one field to print the sheet be created with such a scenario", vbInformation
    Exit Sub
  End If
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

Thanks for the help Hans. The code works fine now. I do really appreciate your help.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

If I want the Worksheet to be the active sheet how should I change the line If Application.WorksheetFunction.CountA(Worksheets("MySheet").Range("F18:F37")) = 0 Then?

Any help would be kindly appreciated.

Thanks in advance.
Best Regards,
Adam

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

I've got it solved. the caption of the message box says Microsoft excel. Instead of this how could I make a message box where the caption appears as my workbook title?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78647
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Dialogue by Hiding Empty Rows

Post by HansV »

MsgBox "You must fill at least one field to print the sheet be created with such a scenario", vbInformation, ActiveWorkbook.Name
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

Thanks for the help Hans. But is there a way I could avoid the .xlsm part appearing in the message box caption.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78647
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Dialogue by Hiding Empty Rows

Post by HansV »

Yes. Use the InStr and Left functions to extract the part before the point from the name,
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

Thanks for the reply Hans. How could I change the caption of the of the message box that appears when I click the excel close button?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78647
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Print Dialogue by Hiding Empty Rows

Post by HansV »

That message box is a built-in Excel message box, not a VBA message box. You cannot change its caption.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Print Dialogue by Hiding Empty Rows

Post by adam »

Thanks for the reply Hans.
Best Regards,
Adam