Excel shape starting in the correct position with VBA

Peter Davison

Active Member
Joined
Jun 4, 2020
Messages
435
Office Version
  1. 365
Platform
  1. Windows
I am creating some rectangles in excel with the code below.
I have set it to create 5 rectangles using i = 0 to 4
What I am not sure is how I get the first block to start at 405 position and not 0 which is what "i" is starting at (I think).
Any help would be great.

Private Sub CommandButton1_Click()

Dim s As Shape
Dim i As Integer
Dim sleft As Integer
Const sWidth As Integer = 100
Const sHeight As Integer = 200

Dim ws As Worksheet
Set ws = Sheets("sheet1")
For i = 0 To 4

sleft = i * sWidth

'add a shape
'Positions (type of shape, Start Position from left, Start Position from Top, Width, Height)
Set s = ws.Shapes.AddShape(1, sleft, 130, sWidth, sHeight)

Next i

'make the fixture Black
s.Fill.ForeColor.RGB = RGB(0, 0, 0)

s.Fill.Visible = False
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi there its me again. Hope you are well.
Following on from yesterday I'd like to always start the position of the first rectangle at the top left corner of an excel cell (e.g. H7) before it positioned by size etc.
What would I change in the code enclosed?
Hope I have posted the code as you said above okay.
Thanks for your time.

VBA Code:
 
Upvote 0
I think I have it.

I made the cell a variable and changed the code to the following which does seem to work.
sleft = H10.Left + (i * sWidth)
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

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