Cooked_Bread13
New Member
- Joined
- Feb 1, 2022
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
Hi all,
I am pretty new to VBA and have been learning a lot from reading through forum posts from other users, but have hit a snag and cant quite find the answer I need.
I have a Workbook which I have written some basic code which adjusts the position and width of Shape "Rectangle 1" based off values in cells D3 & F3. Although there is probably a far easier way to achieve what I'm aiming for, this works as expected. The code sizes the shape so that it spans across the period of two dates (snapshot below).
What I am looking to achieve now is if there is a way to loop through and create additional rectangles and set their position and width if there was additional rows of information.
I know I currently have used specific cell references like "D3" & "F3" in the VBA which is incorrect, I am just unsure what to do instead.
Any help would be much appreciated. My code example is below, with a screenshot of the sheet.
I am pretty new to VBA and have been learning a lot from reading through forum posts from other users, but have hit a snag and cant quite find the answer I need.
I have a Workbook which I have written some basic code which adjusts the position and width of Shape "Rectangle 1" based off values in cells D3 & F3. Although there is probably a far easier way to achieve what I'm aiming for, this works as expected. The code sizes the shape so that it spans across the period of two dates (snapshot below).
What I am looking to achieve now is if there is a way to loop through and create additional rectangles and set their position and width if there was additional rows of information.
I know I currently have used specific cell references like "D3" & "F3" in the VBA which is incorrect, I am just unsure what to do instead.
Any help would be much appreciated. My code example is below, with a screenshot of the sheet.
VBA Code:
Sub SizeRectangle()
Dim cellStartLocation As Range
Dim cellEndLocation As Range
Dim cellWidth As Range
Set cellStartLocation = Worksheets(2).Range("D3")
Set cellEndLocation = Worksheets(2).Range("F3")
Set cellWidth = Worksheets(2).Range(Cells(3, cellStartLocation), Cells(3, cellEndLocation))
With ActiveSheet.Shapes("Rectangle 1")
.Top = Cells(3, cellStartLocation).Top
.Left = Cells(3, cellStartLocation).Left
.Width = cellWidth.Width
End With
End Sub