picture size

ttowncorp

Board Regular
Joined
Feb 2, 2015
Messages
188
Office Version
  1. 365
Platform
  1. Windows
I'm trying to get a picture to snap into a specific range but for some reason when i copy and paste the code again and change the range it keep getting an error highlighted in yellow, even though it identical besides the range location. what am I doing wrong?

Private Sub CommandButton2_Click()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png;*.bmp),*.jpg;*.gif;*.png;*.bmp", _
Title:="Please select an image...")
If strFileName = "False" Then Exit Sub

Set rngDest = Range("B3:H39") 'This line sets the ActiveCell as the Target for insertion of the picture
Set objPic2 = Worksheets("pic 2").Pictures.Insert(strFileName)
With objPic
.ShapeRange.LockAspectRatio = msoFalse 'This line locks the ratio of Width to Height if =msoTrue, no distortion
.Left = rngDest.Left 'The ' .Left=' and ' .Top=' in this and the next line set the respective locations for the placement of the picture
.Top = rngDest.Top
.Width = rngDest.Width 'You can put an single quote before this line if you want to hold just the Height of the picture to be the height of the cell where it will be placed
.Height = rngDest.Height 'You can put an single quote before this line if you want to hold just the Width of the picture to be the width of the cell where it will be placed
End With

End Sub


Private Sub CommandButton3_Click()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png;*.bmp),*.jpg;*.gif;*.png;*.bmp", _
Title:="Please select an image...")
If strFileName = "False" Then Exit Sub

Set rngDest = Range("K3:Q39") 'This line sets the ActiveCell as the Target for insertion of the picture
Set objPic2 = Worksheets("pic 2").Pictures.Insert(strFileName)
Set objPic2 = Worksheets("pic 2").Pictures.Insert(strFileName)
With objPic
.ShapeRange.LockAspectRatio = msoFalse 'This line locks the ratio of Width to Height if =msoTrue, no distortion
.Left = rngDest.Left 'The ' .Left=' and ' .Top=' in this and the next line set the respective locations for the placement of the picture
.Top = rngDest.Top
.Width = rngDest.Width 'You can put an single quote before this line if you want to hold just the Height of the picture to be the height of the cell where it will be placed
.Height = rngDest.Height 'You can put an single quote before this line if you want to hold just the Width of the picture to be the width of the cell where it will be placed
End With

End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Code:
Sub Maybe()
Dim strFileName As String
Dim objPic As Picture
Dim rngDest As Range
strFileName = Application.GetOpenFilename( _
FileFilter:="Images (*.jpg;*.gif;*.png;*.bmp),*.jpg;*.gif;*.png;*.bmp", _
Title:="Please select an image...")
If strFileName = "False" Then Exit Sub
Set rngDest = Range("B3:H39") 'This line sets the ActiveCell as the Target for insertion of the picture
Set objPic = Worksheets("Sheet1").Pictures.Insert(strFileName)
With objPic
.ShapeRange.LockAspectRatio = msoFalse 'This line locks the ratio of Width to Height if =msoTrue, no distortion
.Left = rngDest.Cells(1).Left 'The ' .Left=' and ' .Top=' in this and the next line set the respective locations for the placement of the picture
.Top = rngDest.Cells(1).Top
.Width = rngDest.Width 'You can put an single quote before this line if you want to hold just the Height of the picture to be the height of the cell where it will be placed
.Height = rngDest.Height 'You can put an single quote before this line if you want to hold just the Width of the picture to be the width of the cell where it will be placed
End With
End Sub
 
Upvote 0
Have not heard back if suggestion works.
If you want to insert pictures at intervals of 9 columns, you might try this. No need changing ranges in the code.
Run the macro as many times as you want. It will insert the selected picture 2 columns past the last picture.
It is important that you don't move pictures because the code works on counting pictures with the topleftcell in row 3.
Code:
Sub Pics_At_Intervals()
Dim shp As Shape, startcol As Long, col As Long, x As String
startcol = 0
    For Each shp In ActiveSheet.Shapes
        If shp.TopLeftCell.Row = 3 Then col = shp.TopLeftCell.Column
        If startcol < col Then startcol = col
    Next shp
        startcol = IIf(startcol = 0, 2, startcol + 9)
    With Application.FileDialog(msoFileDialogFilePicker)
        .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
        If .Show = -1 Then x = .SelectedItems(1)
    End With
ActiveSheet.Shapes.AddPicture(x, False, True, Cells(3, startcol).Left, Cells(3, startcol).Top, Cells(3, startcol + 7).Left - Columns(startcol).Left, Cells(38, startcol + 6).Top).Name = "Pic " & startcol
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,482
Messages
6,185,253
Members
453,283
Latest member
Shortm88

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