Resize and Move to Fit

Pestomania

Active Member
Joined
May 30, 2018
Messages
332
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I'm not sure if this is even possible but it would be awesome!!

Is there a vba code to select all pictures and textboxes *only* and rearrange, resize to make all of them fit on the Excel sheet without overlay of each other or overlay of text in column A?
 
You said you wanted to resize them and this is the line that is doing it
Code:
.Width = 100

How do you want to resize?
- are images to be treated same as textboxes etc?
Are all images named "Picture " followed by a number?
Are all TextBoxes named "TextBox" followed by a number?

Hi. I want every thing that is a "picture" to resize and fill the width of the print area. Sorry for all of the confusion.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
So are you wanting this calculation applied
PArea = PrintArea width
TBoxes = combined widths of text boxes
Each Pic width = (pArea - Tboxes) divided by number of pictures
 
Upvote 0
So are you wanting this calculation applied
PArea = PrintArea width
TBoxes = combined widths of text boxes
Each Pic width = (pArea - Tboxes) divided by number of pictures

Yes. That sounds like what I would be looking for. The width should stop at Column T and the height should be under the last row and not past the print area.
 
Upvote 0
See if this gives you what you want
- it is only considering the width of the print area at the moment
- that may break your last row constraint

To take account of the row constraint
- add a further test at the end of the code
- test to see if the height of any of the pictures is greater than the height alowed
- height allowed = total row heights of the rows in the print area lying below the last cell containing text in column A
- if any picture is too tall, then reduce its height to the maximum allowed

Code:
Sub reArrange()
    Dim Shp As Shape, L As Double, T As Double, W As Double, cP As Long
    With ActiveSheet
        W = Range(.PageSetup.PrintArea).Width                   'total width
        With .Range("A" & Rows.Count).End(xlUp).Offset(1)
            L = .Left
            T = .Top
        End With
        For Each Shp In .Shapes
            If Shp.Type = msoPicture Then
                cP = cP + 1                                     'count pictures
            Else
                W = W - Shp.Width                               'deduct shape width
            End If
        Next Shp
        
        If W > 0 And cP > 0 Then W = (W / cP) Else W = 1000     'divide available width by number of pictures
                                                                'prevents code failing if textboxes too wide or there are zero pictures
        For Each Shp In .Shapes
            With Shp
                .Top = T
                .Left = L
                If .Type = msoPicture Then .Width = W           'resize pictures only
                L = L + Shp.Width
            End With
        Next Shp
    End With
End Sub
 
Upvote 0
See if this gives you what you want
- it is only considering the width of the print area at the moment
- that may break your last row constraint

To take account of the row constraint
- add a further test at the end of the code
- test to see if the height of any of the pictures is greater than the height alowed
- height allowed = total row heights of the rows in the print area lying below the last cell containing text in column A
- if any picture is too tall, then reduce its height to the maximum allowed

Code:
Sub reArrange()
    Dim Shp As Shape, L As Double, T As Double, W As Double, cP As Long
    With ActiveSheet
        W = Range(.PageSetup.PrintArea).Width                   'total width
        With .Range("A" & Rows.Count).End(xlUp).Offset(1)
            L = .Left
            T = .Top
        End With
        For Each Shp In .Shapes
            If Shp.Type = msoPicture Then
                cP = cP + 1                                     'count pictures
            Else
                W = W - Shp.Width                               'deduct shape width
            End If
        Next Shp
        
        If W > 0 And cP > 0 Then W = (W / cP) Else W = 1000     'divide available width by number of pictures
                                                                'prevents code failing if textboxes too wide or there are zero pictures
        For Each Shp In .Shapes
            With Shp
                .Top = T
                .Left = L
                If .Type = msoPicture Then .Width = W           'resize pictures only
                L = L + Shp.Width
            End With
        Next Shp
    End With
End Sub

Hello! Thank you for all of your help!;

Code:
 W = Range(.PageSetup.Printarea).Width

Is causing issues and giving runtime error 1004, method "range of object" global failed.
 
Upvote 0
Post#11 Hi. I want every thing that is a "picture" to resize and fill the width of the print area
which suggests that you clicked on Set Print Area

BUT runtime error 1004, method "range of object" global failed at this line suggests that a Print Area is not set
Code:
 W = Range(.PageSetup.Printarea).Width

That is something for you to sort out :confused:

Whatever the cause of the error, replace that line with the one below and it should work for you
Code:
 W = Range("A:T).Width
 
Last edited:
Upvote 0
So that worked! I did add in a last row option just by using row 32 (known).

Essentially
Code:
 H = Range("A" & lastrow & ":s32").Height

The only thing I haven't figured out is saying

Determining if the H or W exceeds pick the other one. Some pages gave landscape and other portrait pictures. So it would grow H to it's limits as long as W doesn't exceed their limits. And vice versa.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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