Print data in sheet by userform based on selected item from combobox

Abdo

Board Regular
Joined
May 16, 2022
Messages
183
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hi
I have uerform contains combobox , the combobox contains "bigger than zero", " equal zero" , "all"
the data will be in Sheet1 from A1:E
the numeric will be in column E , if I select bigger than zero from combobox1 and click commandbutton1 then should print out the paper for data doesn't contain zero in column E and if I select equal zero from combobox1 and click commandbutton1 then should print out the paper for data contain zero in column E ,and if I select all from combobox1 and click commandbutton1 then should print out the paper for all of data contain zero or not in column E .
so I search for the code does it.
thanks
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this:

VBA Code:
Private Sub CommandButton1_Click()
  With ComboBox1
    If .ListIndex = -1 Then
      MsgBox "Select combo"
      .SetFocus
      Exit Sub
    End If
  
    Select Case LCase(.Value)
      Case LCase("bigger than zero")
        Range("A:E").AutoFilter Field:=5, Criteria1:=">0"
      Case LCase("equal zero")
        Range("A:E").AutoFilter Field:=5, Criteria1:="0"
      Case LCase("all")
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    End Select
    ActiveSheet.PrintOut
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  End With
End Sub

🧙‍♂️
 
Upvote 0
Hi
the code works prfectly , but there is case shouldn't do that
it supposes just print from A:E , but also print a second paper when threre is data after column E , how can I fix it,please
 
Upvote 0
What do you mean by a second paper? The macro prints the entire sheet. If you have delimited the printing area, you must remove that area so that the entire sheet is printed.


Or change "E" for the letter of last column.
Range("A:E").Autofilter

Or change Range("A:E").Autofilter by Rows(1).Autofilter

🧙‍♂️
 
Upvote 0
I mean when prin the sheet should print the range from A:E , not print whole sheet. if there data are after E should not print . I see when there data are after column H then will be second page after column H . the first page will be A:H based on margin for every page in the sheet .

my goal ignore Any data after column E when print . so will just printing from A:E
I' surprised from the code doesn't do what I want despite of you specify whta exactly the columns should filter in your code! .

thanks again
 
Upvote 0
One thing is the filter and another thing is the impression.

To print only up to column E you must establish the printing area.

Or try this:

VBA Code:
Private Sub CommandButton1_Click()
  Dim lr As Long
  
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilter.Mode = False
  lr = Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  With ComboBox1
    If .ListIndex = -1 Then
      MsgBox "Select combo"
      .SetFocus
      Exit Sub
    End If
  
    Select Case LCase(.Value)
      Case LCase("bigger than zero")
        Range("A:E").AutoFilter Field:=5, Criteria1:=">0"
      Case LCase("equal zero")
        Range("A:E").AutoFilter Field:=5, Criteria1:="0"
      Case LCase("all")
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    End Select
    ActiveSheet.PageSetup.PrintArea = "A1:E" & lr
    ActiveSheet.PrintOut
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  End With
End Sub
 
Upvote 0
thanks again.
just I've found the problem for zero . the code doesn't deal with format number like this 0.00 ,but will deal with other numbers like 14,222.00 ,how I fix with zero,please?
 
Upvote 0
Try

VBA Code:
Private Sub CommandButton1_Click()
  Dim lr As Long
 
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  lr = Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  With ComboBox1
    If .ListIndex = -1 Then
      MsgBox "Select combo"
      .SetFocus
      Exit Sub
    End If
 
    Select Case LCase(.Value)
      Case LCase("bigger than zero")
        Range("A:E").AutoFilter 5, ">0"
      Case LCase("equal zero")
        Range("A:E").AutoFilter 5, 0, xlOr, "0.00"
      Case LCase("all")
        If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
    End Select
    ActiveSheet.PageSetup.PrintArea = "A1:E" & lr
    ActiveSheet.PrintOut
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  End With
End Sub

😇
 
Upvote 0
Solution
everything is great !(y)
I appreciate for your time & assistance.;)
thank you so much .:)
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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