Help with VBA 400 error

Toastis

Board Regular
Joined
Apr 20, 2011
Messages
65
i get a 400 error with this VBA code. Basically I want a control that they have to complete a field before they can print or close the form. If everything is complete and they hit the "send" button (macro) then it should either deny them from doing so or allow it to print as a pdf. It works fine as laid out if everything is filled in - it's when it's missing something, it gives the error and then ends on the VB screen. That would confuse them terribly.

Help!

Sub Make_PDF()
' Create and save .pdf

pdfName = Range("A9").Text

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
pdfName + " " + Format$(Date, "mm-dd-yyyy") + ".pdf" _
, Quality:=xlQualityMedium, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If WorksheetFunction.CountBlank(Worksheets("sheet1").Range("A9:A11")) >= 1 Or WorksheetFunction.CountBlank(Worksheets("sheet1").Range("F9:F15")) >= 1 Then
MsgBox "Print disabled because at least one cell is left blank in Range A9:A11 or Range F9:F15"
Cancel = True
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If WorksheetFunction.CountBlank(Worksheets("sheet1").Range("A9:A11")) >= 1 Or WorksheetFunction.CountBlank(Worksheets("sheet1").Range("F9:F15")) >= 1 Then
MsgBox "Close disabled because at least one cell is left blank in Range A9:A11 or Range F9:F15"
Cancel = True
End If
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If I take out the Before Print controls (ensuring the cells are completed) it works fine. But I need to ensure they completed the fields before they send it.

Not to mention I need to add a disable code to prevent using file -> send and I don't know if you can create a vba code that does that.
 
Upvote 0
The reason you don't get a compile error is because there is no such constant as xlQualityMedium; I changed it to xlQualityMinimum, which VBA recognizes. That's why I wanted you to use Option Explicit.

What is the value of pdfName when you step through the code?

When you step through the code, do you get to the BeforePrint sub?
 
Upvote 0
See http://www.cpearson.com/excel/debug.htm.

In themeanwhile, try coding it like this.

In a standard module,

Code:
Sub PrintPDF()
    With Worksheets("sheet1").Range("A9:A11, F9:F15")
        If NoBlanks(.Cells) Then
            Make_PDF
        Else
            Application.Goto .Cells
            MsgBox "Print disabled because one or more of these cells is blank"
        End If
    End With
End Sub
 
Sub Make_PDF()
    Dim pdfName As String
 
    pdfName = Range("A9").Text & Format$(Date, " mm-dd-yyyy") & ".pdf"
    ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            Filename:=pdfName, _
            Quality:=xlQualityMinimum, _
            IncludeDocProperties:=False, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=True
End Sub
 
Function NoBlanks(r As Range) As Boolean
    Dim rArea As Range
 
    For Each rArea In r.Areas
        If WorksheetFunction.CountBlank(rArea) Then Exit Function
    Next rArea
    NoBlanks = True
End Function

In ThisWorkbook,
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    With Worksheets("sheet1").Range("A9:A11, F9:F15")
        If Not NoBlanks(.Cells) Then
            Cancel = True
            Application.Goto .Cells
            MsgBox "Close disabled because one or more of these cells is blank"
        End If
    End With
End Sub
 
Upvote 0
Genius!!! :bow:

Thank you! It works - got a brilliant code to restrict them from file send without completing those fields?
 
Upvote 0
You could adapt the PrintPDF routine.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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