Question

I have inherited this awfulness, and been asked to make updates.

Currently this workbook is built with one sheet that has a bunch of checkboxes, all associated with other sheets in the workbook. The user selects checkboxes, and then hits a button to print the sheets associated with each checkbox that is checked.

The way it's built now, it runs a print job for each sheet selected. I have been asked to run one print job for all sheets selected (to avoid having a hundred cover sheets).

I have written a little VBA function that produces a string containing the name of every sheet for which the checkbox is checked, in quotes, comma separated.

I need to figure out a way to use this information to select all the sheets and then print once each sheet I need is selected.

Or even hell, I'd take being able to spit this string back into the macro that was originally written for the print ability. Right now the macro is another sheet in the workbook, and has 102 different print commands, controlled by a bunch of if statements. So I'd take being about to spit the string into that sheet so I could run one print command.

Either way, someone please help.

Here is my code:

Public Function sheetString()
    Dim c As Integer
    Dim r As Integer
    Dim sConcat As String

    Dim ws As Worksheet

    For c = 2 To 6 Step 2
        For r = 1 To 46
            If Sheet94.Cells(r, c) = True Then
               sConcat = sConcat & Sheet94.Cells(r, c - 1) & ", "
            End If
        Next r
    Next c

    sConcat = Left(sConcat, Len(sConcat) - 2)

    Debug.Print sConcat
End Function

The output of the code is like this (with varying names depending on which boxes are checked):

"PR015", "PR018", "PR019", "PR026", "PR029A"

EDIT: Thanks to simoco, I'm closer than I've been so far. Here is the code as it stands now.

Public Function sheetString()
    Dim c As Integer
    Dim r As Integer
    Dim sConcat As String
    Dim ws As Worksheet

    Set ws = Sheet94

    For c = 2 To 6 Step 2
        For r = 1 To 46
            If ws.Cells(r, c) = True Then
               sConcat = sConcat & ws.Cells(r, c - 1) & ","
            End If
        Next r
    Next c
    sConcat = Left(sConcat, Len(sConcat) - 1)
    Debug.Print sConcat
    sheetString = sConcat
End Function

Sub test()
    'if cells with sheet names contains quotes
    'Sheets(Split(Replace(sheetString, """", ""), ",")).Select
    'if cells with sheet names doesn't contain quotes
    Sheets(Split(sheetString, ",")).Select
    ActiveSheet.PrintOut Copies:=1
End Sub

It's running without bombing out, but now it's only selecting the first sheet that has it's box checked.

Was it helpful?

Solution

Here is slightly modified function sheetString:

Public Function sheetString()
    Dim c As Integer, r As Integer
    Dim sConcat As String
    Dim ws As Worksheet

    Set ws = Sheet94

    For c = 2 To 6 Step 2
        For r = 1 To 46
            If ws.Cells(r, c) Then
               sConcat = sConcat & ws.Cells(r, c - 1) & ","
            End If
        Next r
    Next c

    sConcat = Left(sConcat, Len(sConcat) - 1)

    Debug.Print sConcat
    sheetString = sConcat
End Function

and then call use it like this:

Sub test()
    'if cells with sheet names contains quotes
    'Sheets(Split(Replace(sheetString, """", ""), ",")).PrintOut Copies:=1
    'if cells with sheet names doesn't contain quotes
    Sheets(Split(sheetString, ",")).PrintOut Copies:=1
End Sub

OTHER TIPS

Sub Macro1()
 Application.Dialogs(xlDialogPrint).Show
End Sub
Sub Macro()
  Worksheets.PrintOut
End Sub

Use this code to print all sheets in xl. Also select your specific pri

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top