A Print Button macro that is based on Checkbox macros
If you're careful with the setup, it should be fairly straight forward.
I created 3 checkboxes (also from the forms toolbar) and called them:
check box 1
check box 2
check box 3
(the naming convention will be important--start at 1 and don't skip any
numbers.)
Then I assigned this code to the button (not checkboxes):
Option Explicit
Option Base 1
Sub testme()
Dim myAddresses As Variant
Dim myCBXCount As Long
Dim iCtr As Long
Dim myCBX As CheckBox
Dim histVisible
Dim RangesPrinted As Long
myAddresses = Array("a1:b9", "c2:d8", "e3:f9")
myCBXCount = ActiveSheet.CheckBoxes.Count
If myCBXCount < UBound(myAddresses) - LBound(myAddresses) + 1 Then
MsgBox "Design error #1!" & vbLf & "Please contact GaryM at xxxx."
Exit Sub
End If
Application.ScreenUpdating = False
RangesPrinted = 0
For iCtr = 1 To myCBXCount
Set myCBX = Nothing
On Error Resume Next
Set myCBX = ActiveSheet.CheckBoxes("check box " & iCtr)
On Error GoTo 0
If myCBX Is Nothing Then
MsgBox "Design error #2 with " & iCtr & " !" & vbLf _
& "Please contact GaryM at xxxx."
Else
If myCBX.Value = xlOn Then
With Worksheets("sheet2")
histVisible = .Visible
.Visible = xlSheetVisible
.Range(myAddresses(iCtr)).PrintOut
.Visible = histVisible
RangesPrinted = RangesPrinted + 1
End With
myCBX.Value = xlOff 'turn it off for next time?
End If
End If
Next iCtr
If RangesPrinted 0 Then
MsgBox "Printed " & RangesPrinted & " ranges!"
Else
MsgBox "Nothing printed--check a box or two!"
End If
Application.ScreenUpdating = True
End Sub
You'll want to have as many addresses in this line:
myAddresses = Array("a1:b9", "c2:d8", "e3:f9")
as you have checkboxes.
And go in that same order: 1, 2, 3,...
Mstr G wrote:
I created an Excel spreadsheet (Windows XP) which contains a macro
Print Button from the forms toolbar that prints a hidden worksheet.
However, I was wondering if it's possible to incorporate checkbox
macros in the active window that will dictate which range of cells to
be printed when the Print Button macro pressed. This will allow the
user to print various sections (range of cells) of the hidden
worksheet depending on which checkbox is checked.
Thank you,
Mstr G.
--
Dave Peterson
|