Simple if statement for listbox printing

erutherford

Active Member
Joined
Dec 19, 2016
Messages
453
I borrowed this code and it works, except for the If statement. I have seen this example numerous times and no ones had an issue. When I run it (with an empty LBIssues) it will print preview a blank page. Shouldn't it say "there is nothing to print" and not display a blank page?

Code:
Private Sub PrintBtn_Click()
    Dim rng As range
    Dim wks As Worksheet, ListData()
    'Creates a new ws.sheet for the printout
    Set wks = ThisWorkbook.Worksheets("Printout")
    'Define range start for the list output
    Set rng = wks.range("B2")

    If LBIssues.ListCount = 0 Then
        MsgBox "There is nothing in the list to print!"
        Exit Sub
    End If

    ReDim ListData(LBIssues.ListCount - 1, LBIssues.ColumnCount - 1)
    ListData() = LBIssues.List()
    wks.Cells.Clear
    wks.range(rng, rng.Offset(UBound(ListData), 6)).Value = ListData()
    Application.ScreenUpdating = False

    'Keeps ws.Printout visible
    wks.Visible = xlSheetVisible
    Unload ufEditIssues
    wks.PrintPreview
    'wks.Visible = xlSheetHidden
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How are you loading the LBIssues list, maybe there are cells with blank spaces and you are loading one or more blank records in the LBIssues and therefore the counter is greater than 0.
 
Upvote 0
It does load headers when I look at the ws.sheet it creates, but I don't see where the code loads them. Properties, Column heads =False Everything else is blank.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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