• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

VBA to add picture to cell only works with landscape|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / VBA to add picture to cell only works with landscape|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosVBA to add picture to cell only wor…
sp_PrintTopic sp_TopicIcon
VBA to add picture to cell only works with landscape
Avatar
John Langham-Service
Member
Members
Level 0
Forum Posts: 56
Member Since:
July 17, 2018
sp_UserOfflineSmall Offline
1
January 14, 2021 - 10:51 pm
sp_Permalink sp_Print

Using the below code to add pictures to excel when you click in a cell, it works as expected with landscape images but portrait ones are added to the right and above the target cell. 

 

Sub picAdd()
Dim sFile As Variant, r As Range
sFile = Application.GetOpenFilename(FileFilter:="Pic Files (*.jpg;*.JPG;*.JPEG;*.png;*.bmp), *.jpg;*.JPG;*.JPEG;*.png;*.bmp", Title:="Browse to select a picture")
If sFile = False Then Exit Sub
On Error Resume Next
Set r = ActiveCell
On Error GoTo 0
If r Is Nothing Then Exit Sub
If r.Count > 1 Then Exit Sub
ActiveSheet.Pictures.Insert (sFile)
With ActiveSheet.Shapes(ActiveSheet.Shapes.Count)
.LockAspectRatio = True
.Top = r.Top
.Left = r.Left
.Height = r.RowHeight
.Placement = xlMoveAndSize
End With
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Not Intersect(Target, Range("1:2")) Is Nothing Or Target.Cells.CountLarge > 1 Then
Exit Sub
End If

If Not Intersect(Target, Range("J:K")) Is Nothing Then
Call picAdd
End If
End Sub

Avatar
Lionel Baijot
Member
Members
Level 0
Forum Posts: 119
Member Since:
September 9, 2020
sp_UserOfflineSmall Offline
2
January 15, 2021 - 3:23 pm
sp_Permalink sp_Print

Hi John,

When I try this code on my machine, everything seems correct (png). Could you put an example of your file or a photo that is problematic.

BR,

Lionel

Avatar
John Langham-Service
Member
Members
Level 0
Forum Posts: 56
Member Since:
July 17, 2018
sp_UserOfflineSmall Offline
3
January 18, 2021 - 8:46 pm
sp_Permalink sp_Print

Hi Lionel I have added the workbook and three pictures and a screen shot of where the macro places them

Clicking in L3 Landscape correct orientation is placed in the correct cell

Clicking in M3 Landscape Upside down is placed below and to the right

Clicking in N3 Portrait is placed up and to the right.

 

The Portrait one should be resized to fit in the cell vertically as well

the only difference between the pictures is the angle the phone was held at to take the pictures but when selecting them the grab handles are in different positions.Screenshot-2021-01-18-103611.pngImage Enlarger

Landscape-correct-Orientation-sm.jpgImage Enlarger
Landscape-Upside-Down-sm.jpgImage Enlarger
Portrait-sm.jpgImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage Screenshot-2021-01-18-103611.png (231 KB)
  • sp_PlupImage Landscape-correct-Orientation-sm.jpg (68 KB)
  • sp_PlupImage Landscape-Upside-Down-sm.jpg (74 KB)
  • sp_PlupImage Portrait-sm.jpg (92 KB)
Avatar
John Langham-Service
Member
Members
Level 0
Forum Posts: 56
Member Since:
July 17, 2018
sp_UserOfflineSmall Offline
4
January 28, 2021 - 1:10 am
sp_Permalink sp_Print

Hi I have tried adding a piece of code to check rotation but that has no effect

...

ActiveSheet.Pictures.Insert (sFile)

If ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Rotation = 0 Or ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Rotation = 180 Then

ActiveSheet.Shapes(ActiveSheet.Shapes.Count).Rotation = 90

End If

With ActiveSheet.Shapes(ActiveSheet.Shapes.Count)

....

regards

John

Avatar
John Langham-Service
Member
Members
Level 0
Forum Posts: 56
Member Since:
July 17, 2018
sp_UserOfflineSmall Offline
5
February 10, 2021 - 2:11 am
sp_Permalink sp_Print

Answer here https://www.excelforum.com/exc.....ost5468610

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Fluff, Ineke Smit
Guest(s) 11
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 880
Purfleet: 414
Frans Visser: 346
David_Ng: 306
lea cohen: 237
Jessica Stewart: 219
A.Maurizio: 213
Aye Mu: 201
Hans Hallebeek: 186
Newest Members:
Kate Dyka
Kwaje Alfred Mogga
thong nguyen
Appiagyei Kofi Frimpong
Hilary Burchfield
Richie Wright
Adel Kock
Barbara Murray
Doug Milne
Siobhan Stringer
Forum Stats:
Groups: 3
Forums: 24
Topics: 6546
Posts: 28658

 

Member Stats:
Guest Posters: 49
Members: 32832
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.