I have a macro that performs a number of functions and works properly, but the buttons resize in one of two situations. The buttons reize when the macro uses an autofilter, which deletes roughly 500 rows of data (the buttons get much larger), and they also resize when I first paste in the data before I run the program (once again get larger). I have tried going into properties and selecting "Don't move or size with the cells" in object positioning. I have also tried locking the aspect ratio of the buttons. But neither have kept the button from resizing. Any ideas of what else I could try?
1 Answer
I think this should do the trick.
One other thing I've seen before is that the font size sometimes changes if the button changes. I included this but left it commented out. You may also want/need to reset the button's .TopLeftCell location, which is also included, but commented out in the code below:
'## Add the following to your existing macro:
Dim btn As Shape
Dim btLeft As Double, btTop As Double, btWidth As Double, _
btHeight As Double, btFontSize As Double
Dim btCell As Range
'## Put this block of code before your existing code:
Set btn = ActiveSheet.Shapes("Button 3") '## Modify to your button name
With btn
btLeft = .Left
btTop = .Top
btHeight = .Height
btWidth = .Width
'btFontSize = TextFrame2.TextRange.Font.Size
'Set btCell = btn.TopLeftCell
End With
'Your code goes here:
'
'
'
'End of your code
'## Put this block of code before the Exit Sub statement/after your existing code
With btn
'.TopLeftCell = btCell
'.TextFrame2.TextRange.Font.Size = btnFontSize
.Left = btLeft
.Top = btTop
.Height = btHeight
.Width = btWidth
End With
2 Comments
Jon Crowell
+1. Answered a similar question a while back, and David's answer above covers option 2 in this answer: stackoverflow.com/a/11532902/138938
David Zemens
+1 @HeadofCatering for some more options. I have personally been using your option 4 most recently, too. Shapes attached to macro seems most reliable, and not prone to this weird behavior.
Exit Substatement..Left,.Top,.Height, and.Widthof the button, and make sure to reset those at the end of the code. If you have any trouble with it, post your code here and we can try to help!