Pregunta

My workbook has sheet1 = Overview (contains button to launch UserForm1). My Userform has 24 CheckBox and 2 CommandButton (OK and Cancel).

Each CheckBox is associated with a sheet in the workbook. Each of the subsequent sheets is labelled PQC 1001, PQC 1002, etc., that contain data (images pushed to the upper left in A1 for each sheet).

My goal is to have the user check the CheckBox for each of the items they want to use, then select "OK" (CommandButton1) to have it copy the data to a new workbook for them to print out.

Later, I intend to define printing parameters to have the items paginated, but I need to get the basics before I go for the gold.

Currently I have the following code (working with first and second CheckBox to make sure they work, then I intended to expand by copying the code and modifying for the correct sheet/CheckBox):

Sub CommandButton1_Click()

 Dim WB As Workbook

 If CheckBox1.Value = True Then
    sheets("PQC 1001").Copy
    Set NewBook = Workbooks.Add
        With NewBook
        End With
    Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    Else: CheckBox1.Value = False
  End If

 If CheckBox2.Value = True Then
 sheets("PQC 1002").Copy
     Selection.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, _
            SkipBlanks:=False, Transpose:=False
    Else: CheckBox2.Value = False
  End If

End Sub

Sub CommandButton2_Click()
 Unload Me
 End
End Sub

An error is triggered when I run this code (having checked CheckBox1 only): PasteSpecial method of range class failed. Beyond the code issue, I have 2 workbooks spawned; the first has just the PQC 1001 sheet in it ans the second workbook is blank (unless I copied anything else, e.g. when I copy/pasted the code before modifying, the code I copied pasted into cell A1 of the second workbook).

I honestly don't know where to go from here. I've spent a bit of time searching on Google for possible resolution and have tried looking for a few terms/phrases in Stack Overflow's search bar. I think I might be looking up the incorrect term names, so that could make a difference ("commandbutton to paste into newbook" being one).

As a general note, this is essentially the third thing I have ever attempted to code; my educational background contains zero coding. The extent of my coding skill comes from some reading and feedback on the code I've written from persons more knowledgeable than I.

¿Fue útil?

Solución

For your first issue, please avoid relying on Selection. Instead, you should explicitly state the destination range, like:

NewBook.Sheets(1).Range("A1").PasteSpecial...

Now, you are getting multiple workbooks open because the Sheets(_name_).Copy method will always return a new workbook if you have not specified a destination explicitly in the Copy statement.

http://msdn.microsoft.com/en-us/library/office/ff837784(v=office.15).aspx

Instead, specify the destination for the copied sheet using either the Before or After arguments of the Copy method:

Try this:

Dim NewBook as Workbook
Set wb = ActiveWorkbook 'Or ThisWorkbook, or Workbooks("workbookname.xlsx")
'#Create the new book outside of the If blocks.
Set NewBook = Workbooks.Add
'# Get rid of excess sheets
Do Until NewBook.Sheets.Count = 1
    NewBook.Sheets(1).Delete
Loop
If CheckBox1.Value = True Then
'Creates exact copy of the sheet PQC 1001 in the NEW workbook.
    wb.sheets("PQC 1001").Copy After:=NewBook.Sheets(1)
Else: CheckBox1.Value = False
End If
If CheckBox2.Value = True Then
     wb.sheets("PQC 1002").Copy After:=NewBook.Sheets(NewBook.Sheets.Count)
Else: CheckBox2.Value = False
End If
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top