how select specific range and print out based on selected item from combobox

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
Hello
I want print out specific range by using name from combobox1 (active x ) inside the sheet(not combobox on userform) ,so when select name from combobox1 then should search for the name in column J and select the range is relating with name based on combobox1 and when click command button1 ,then pop message " do you want print this range " if click ok ,then should print out , if click no ,then should not do any thing .
and if the combobox is empty and click command button1 ,then pop message " do you want print this range " if click ok ,then should print out the whole ranges contains data without any exception , if click no ,then should not do any thing .
example :
when select ALA from combobox1 then just print out H2:I8 and if the combobox1 is empty and click command button1 , then should print out H2:L37



Al (1) (1) (2).xlsm
HIJKL
1
2NAME
3ALA
4DATEDescribeDEBITCREDITBALANCE
505/01/2022NOT PAID1,233.001,233.00
606/01/2022NOT PAID1,233.002,466.00
707/01/2022PAID2,000.00466.00
808/01/2022PAID466.000.00
9
10NAME
11ALA1
12DATEDescribeDEBITCREDITBALANCE
1308/01/20220.00
1409/01/2022NOT PAID2,000.002,000.00
1511/01/2022NOT PAID2,000.001,000.003,000.00
1611/01/2022NOT PAID2,000.005,000.000.00
17
18NAME
19ALA2
20DATEDescribeDEBITCREDITBALANCE
2111/01/20220.00
2212/01/2022NOT PAID1,500.001,500.00
2313/01/2022NOT PAID500.002,000.00
2414/01/2022PAID2,000.001,000.003,000.00
2515/01/2022PAID1,010.001,990.00
2616/01/2022PAID1,000.00990.00
2717/01/2022PAID990.000.00
28
29NAME
30ALA
31DATEDescribeDEBITCREDITBALANCE
3217/01/20220.00
3318/01/2022NOT PAID5000050,000.00
3419/01/2022PAID100001,000.0059,000.00
3520/01/2022PAID2,000.0057,000.00
3621/01/2022PAID50,000.007,000.00
3722/01/2022PAID7,500.00-500.00
SS
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Put the following code in a module and run the macro "printname"


VBA Code:
Sub printname()
  Dim f As Range
  Dim n As Long
  
  If ActiveSheet.ComboBox1 = "" Then
    Range("H2:L" & Range("H" & Rows.Count).End(3).Row).Select
    Call printselection
  Else
    Set f = Range("J:J").Find(ActiveSheet.ComboBox1.Value, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      MsgBox "the name does not exist: " & ActiveSheet.ComboBox1.Value
    Else
      n = f.CurrentRegion.Rows.Count
      f.Offset(1, -2).Resize(n - 2, 5).Select
      Call printselection
    End If
  End If
End Sub

Sub printselection()
  Dim res As VbMsgBoxResult

  With ActiveSheet
    res = MsgBox(" do you want print this range ", vbYesNo + vbQuestion)
    If res = vbYes Then
      .PageSetup.PrintArea = Selection.Address
      .PrintOut
    End If
  End With
End Sub

:cool:
 
Upvote 1
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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