• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA to create and program a command button

wcso105

New Member
I have an excel spreadsheet that works just as I'd like it to. I am trying to create the same sheet using VBA so I can have multiple sheets in the same workbook. Copy/paste does not work as it does not copy/paste the 3 command buttons. My command button code is basic and I can create the button but not add the code via VBA.

One code is:
>>> use code - tags <<<
Code:
Private Sub CommandButton2_Click()
Worksheets("Sheet1").PageSetup.PrintArea = Range("L2").Value & ":" & Range("L3").Value
PrintPreview
End Sub
Any help adding this to a spreadsheet via VBA is appreciated. My current spreadsheet is attached and what I have tried is at the bottom of the code. When I create a new sheet and run the macro, it prints everything instead of what is requested. Using the sheet created without VBA, it works perfectly.
 

Attachments

  • Ed Sawyer Builders.xlsm
    725.6 KB · Views: 5
Last edited by a moderator:
Still won't work - clicking the button has no result. Still trying to find other ways. Thank you for your suggestion.
 
Try this sample.
I modified Your code too.
Note: I cannot verify Your PrintPreview
I tested it with PrintOut
 

Attachments

  • Ed Sawyer Builders.xlsb
    713.4 KB · Views: 3
No change from previous versions. I'm going to reprogram my print button to print as .pdf and save each estimate in Word.
Thank you for your attempts to make this work. If I eventually find the answer I will post it for all.
 

wcso105

Interesting ...
I tested it myself and for me it works (except as I wrote Preview).
Can You send Your used file for me?
 

Attachments

  • Ed Sawyer Builders.xlsb
    713.4 KB · Views: 2
Just use a template sheet and copy the whole sheet. The buttons and code will be part of the copy. Having said that, you are better off not using Activex buttons.
 
Back
Top