Insert Pictures From Multiple Sheet to a Sheet in Any Cell Then Autofit Resize

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,077
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi expert..

this code below working well to insert picture then automatic resize in a cell but unfortunally this code only work in the same sheet
i want to make how this code work between several/multiple sheet..
here this code :
VBA Code:
Public Sub FitPic()
On Error GoTo NOT_SHAPE
Dim PicWtoHRatio As Single
Dim CellWtoHRatio As Single
With Selection
PicWtoHRatio = .Width / .Height
End With
With Selection.TopLeftCell
CellWtoHRatio = .Width / .RowHeight
End With
Select Case PicWtoHRatio / CellWtoHRatio
Case Is > 1
With Selection
.Width = .TopLeftCell.Width
.Height = .Width / PicWtoHRatio
End With
Case Else
With Selection
.Height = .TopLeftCell.RowHeight
.Width = .Height * PicWtoHRatio
End With
End Select
With Selection
.Top = .TopLeftCell.Top
.Left = .TopLeftCell.Left
End With
Exit Sub
NOT_SHAPE:
MsgBox "Select a picture before running this macro."
End Sub

i want can select picture from another sheet before running this macro between sheet
my target in sheet bm1, bm2, etc..in any cell.....and then my photos from sheet p1,p2,p3, p...etc....(the name sheet is random)

this my link file Box

any help, greatly appreciated..

.sst
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Select picture (as before) before running macro

When input box appears ...
click on target sheet tab
click on target cell
click OK

VBA Code:
Public Sub FitPic()
On Error GoTo NOT_SHAPE
    Dim PicWtoHRatio As Single
    Dim CellWtoHRatio As Single
    Dim cel As Range
    Selection.Cut
    Set cel = Application.InputBox("Click on destination tab and cell", "", , , , , , 8)
    With cel
        .Parent.Activate
        .Activate
        .Parent.Paste
    End With
   
    With Selection
        PicWtoHRatio = .Width / .Height
    End With
    With cel
        CellWtoHRatio = .Width / .RowHeight
    End With
    Select Case PicWtoHRatio / CellWtoHRatio
        Case Is > 1
            With Selection
            .Width = cel.Width
            .Height = .Width / PicWtoHRatio
            End With
        Case Else
            With Selection
            .Height = cel.RowHeight
            .Width = .Height * PicWtoHRatio
            End With
    End Select
    With Selection
        .Top = cel.Top
        .Left = cel.Left
    End With
Exit Sub
NOT_SHAPE:
MsgBox "Select a picture before running this macro."
End Sub


Rich (BB code):
The above code MOVES the picture 

To COPY... replace
Selection.Cut
with
Selection.Copy
 
Upvote 0
hi Yongle .

for easy & simple step, would you, to modify your code so your step can be change, like this :
1. click on sheet tab/target cell;
2. then select target picture from another sheet to be copy (as before) before running macro;
3. click OK (finish)
 
Upvote 0
Select picture (as before) before running macro

When input box appears ...
click on target sheet tab
click on target cell
click OK

VBA Code:
Public Sub FitPic()
On Error GoTo NOT_SHAPE
    Dim PicWtoHRatio As Single
    Dim CellWtoHRatio As Single
    Dim cel As Range
    Selection.Cut
    Set cel = Application.InputBox("Click on destination tab and cell", "", , , , , , 8)
    With cel
        .Parent.Activate
        .Activate
        .Parent.Paste
    End With
 
    With Selection
        PicWtoHRatio = .Width / .Height
    End With
    With cel
        CellWtoHRatio = .Width / .RowHeight
    End With
    Select Case PicWtoHRatio / CellWtoHRatio
        Case Is > 1
            With Selection
            .Width = cel.Width
            .Height = .Width / PicWtoHRatio
            End With
        Case Else
            With Selection
            .Height = cel.RowHeight
            .Width = .Height * PicWtoHRatio
            End With
    End Select
    With Selection
        .Top = cel.Top
        .Left = cel.Left
    End With
Exit Sub
NOT_SHAPE:
MsgBox "Select a picture before running this macro."
End Sub


Rich (BB code):
The above code MOVES the picture 

To COPY... replace
Selection.Cut
with
Selection.Copy

hi Yongle .

for easy & simple step, would you, to modify your code so your step can be change, like this :
1. first, click on sheet tab/target cell;
2. then select target picture from another sheet to be copy (as before) before running macro;
3. click OK (finish)
 
Upvote 0
Application.InputBox method makes it easy to pause the code whilst you select the cell
That mehod is not available for shapes
I will think about it when I have some time
Check thread in 2 days
 
Upvote 0
VBA cannot be interrupted to select a shape
Trying to find a workaround
One way is to use 2 macros - is that ok?
 
Upvote 0
Delete previous code

1. Run PictureDestination
2. Select picture & run FitPic

In a module
VBA Code:
Option Explicit

Public dCell As Range   'destination for picture

Sub PictureDestination()
    Set dCell = Application.InputBox("Click destination tab, click cell,click OK", "", , , , , , 8)
    MsgBox "select image and run FitPic", vbOKOnly, ""
End Sub

Sub FitPic()
'variables and constants
    Const msg1 = "Select destination before selecting picture"
    Const msg2 = "Select a picture before running this macro"
    Dim msg As String
    Dim PicWtoHRatio As Single
    Dim CellWtoHRatio As Single
    Dim PicSheet As Worksheet
'destination details
    On Error GoTo Handling
    
'move picture
    Set PicSheet = ActiveSheet
    msg = msg1
    dCell.Parent.Activate           'forces error if destination empty
    dCell.Select                    'deselects images in destination sheet
    PicSheet.Activate
    msg = msg2
    Selection.Cut
    dCell.Parent.Activate
    dCell.Parent.Paste ActiveCell
    
'resize picture
    CellWtoHRatio = ActiveCell.Width / ActiveCell.RowHeight
    With Selection
        PicWtoHRatio = .Width / .Height
    End With
    Select Case PicWtoHRatio / CellWtoHRatio
    Case Is > 1
        With Selection
            .Width = ActiveCell.Width
            .Height = .Width / PicWtoHRatio
        End With
    Case Else
        With Selection
            .Height = ActiveCell.RowHeight
            .Width = .Height * PicWtoHRatio
        End With
    End Select
    With Selection
        .Top = ActiveCell.Top
        .Left = ActiveCell.Left
    End With
    Set dCell = Nothing
Exit Sub
Handling: MsgBox msg
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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