ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can I put buttons on a sheet without selecting? (https://www.excelbanter.com/excel-programming/454821-can-i-put-buttons-sheet-without-selecting.html)

Don Wiss

Can I put buttons on a sheet without selecting?
 
I'd prefer if this macro added the buttons without selecting the sheet. And
even work if another workbook is active.

Sub AddButtonsToPage()

Application.ScreenUpdating = False
ThisWorkbook.Activate
Sheets("BondCalc").Select

If ActiveSheet.Shapes.Count = 0 Then
ActiveSheet.Buttons.Add(400, 20, 170, 23).Select
Selection.OnAction = "CopyBondCalcSheet"
Selection.Characters.Text = "Put Page Contents into Clipboard"

ActiveSheet.Buttons.Add(400, 55, 72, 23).Select
Selection.OnAction = "ClearBondCalcSheet"
Selection.Characters.Text = "Clear Page"
End If

Range("A1").Select

End Sub

Is it possible?

Don. www.donwiss.com (e-mail link at home page bottom).

Don Wiss

Can I put buttons on a sheet without selecting?
 
I played around with it and this works. And *much* more elegant.

Sub AddButtonsToPage()

Application.ScreenUpdating = False
With ThisWorkbook.Sheets("BondCalc")

If .Shapes.Count = 0 Then
With .Buttons.Add(400, 20, 170, 23)
.OnAction = "CopyBondCalcSheet"
.Characters.Text = "Put Page Contents into Clipboard"
End With

With .Buttons.Add(400, 55, 72, 23)
.OnAction = "ClearBondCalcSheet"
.Characters.Text = "Clear Page"
End With
End If

End With

End Sub

Don. www.donwiss.com (e-mail link at home page bottom).


All times are GMT +1. The time now is 07:48 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com