VBA to resize shape incrementally

Lil Stinker

Board Regular
Joined
Feb 16, 2022
Messages
151
Office Version
  1. 2019
Platform
  1. Windows
Just like how you can resize a shape using the up and down arrow buttons for height and width, how would you write this in VBA? I'm trying to mimic the inherent ability of Excel to increase or decrease the size of a shape incrementally using my own button shapes on the worksheet. Each time you click the Up button, I want the shape to increase height and width by 0.02" until it reaches 1" and stops. Similar for decreasing until it reaches 0.1" then stops.

I tried using the macro recorder but between .ScaleHeight, .ScaleWidth or .Height, .Width, I only seem to get one size adjustment. Haven't been able to find anything on the web that seems to work for what I want to do either.

Does anyone have any suggestions? 🙏
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
something like this:
Screenshot 2024-02-28 120748.png


VBA Code:
Sub Resize(z As Integer)
    Set mySh = ActiveSheet.Shapes.Range(Array("myShape"))
    mySh.Top = mySh.Top - (z / 2)
    mySh.Left = mySh.Left - (z / 2)
    
    mySh.Height = mySh.Height + z
    mySh.Width = mySh.Width + z
End Sub

'assign into buttons
Sub Bigger()
    Resize (1)
End Sub
Sub Smaller()
    Resize (-1)
End Sub
add some IFs to min / max size.
 
Upvote 0
Solution
This works brilliantly!
Forgive me but my IFs don't seem to stop anything from running. Could you provide an example or tell me what I'm doing wrong?

VBA Code:
Sub Smaller()
   If Sheet1.Shapes.Range(Array("myShape")).Height <= 0.1 Then Exit Sub
   Resize (-1)
End sub

I can't seem to get anywhere unless I change the <= to >=, but then it Exits every time.
 
Upvote 0
Okay, I fixed my IFs situation. I didn't realize the numbers I was seeing in the Height and Width fields as I was running through the code do not directly correlate in VBA. Apparently, you can't use decimals with .Height & .Width (even though that's how Excel displays it in the dialog box)? I finally got where I wanted to be using whole numbers. 🤔

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top