Dr. Demento
Well-known Member
- Joined
- Nov 2, 2010
- Messages
- 618
- Office Version
- 2019
- 2016
- Platform
- Windows
I've got a number of pictures already copied into a sheet and I'd like to arrange them (withOUT resizing the picture) into a grid pattern. This needs to take into consideration the max number of pictures I designate that will be in any particular column AND space the pictures out based on how many rows/columns they occupy. I've cobbled some code together but I'm no where near where I want it to be.
I realize I may be barking up various wrong trees but I'm too frustrated to see a way forward.
Any pointers would be greatly appreciated.
Thanks, y'all!
I realize I may be barking up various wrong trees but I'm too frustrated to see a way forward.
Any pointers would be greatly appreciated.
Thanks, y'all!
VBA Code:
Sub pic_GRID()
Dim wbk As Workbook
Set wbk = ActiveWorkbook
Dim sht As Worksheet
Set sht = ActiveSheet
Dim shp As Shape
Dim rng As Range, rng0 As Range
Set rng0 = sht.Range("A1")
Dim down As Long, across As Long, _
rw As Long, col As Long, _
cntr As Long
Const maxPics As Long = 5
Const pxconv As Long = 96
Const rwHt As Double = 16.5
Const colWd As Double = 8.11
For Each shp In sht.Shapes
cntr = cntr + 1
' My attempt to have a counter that limits the number of pictures down = maxPics; once the down variable hits maxPics, it resets to top line and moves one across
down = ((cntr - 1) Mod maxPics)
across = Int((cntr - 1) / maxPics)
' I'm attempting to place each picture using a cell as a reference
Set rng = rng0.offset(rw * down, _
(col * across)).Resize(1, 1)
Debug.Print down & "|" & across & "|" & shp.Width & "|"& shp.Height & "|"& rng.Address
With shp ' I have no idea how to correct this. For the first picture, it should be placed in A1; the second picture (213px) should be placed in A16
.Left = sht.Cells(rng.Column).Left '
.Top = sht.Cells(rng.Row).Top
End With
rw = rw + WorksheetFunction.RoundUp(shp.Height / rwHt, 0) + 2 ' This gives the proper number of rows that a picture takes plus a row or two as spacer but it doesn't place it properly given my problem with shp.top & shp.left above
col = across * WorksheetFunction.max(col, WorksheetFunction.RoundUp(shp.Width / colWd, 0)) ' I have no idea what's going wrong here; I'm trying to find the next column to be populated based on the widest picture so far.
Next shp
End Sub