Thursday, December 5, 2019

 



Option Explicit


Sub main()

    Dim imagepath As String
    
    Dim imagePos As Range
    
    imagepath = "C:\Users\mypc\Pictures\myimg\commandbutton.JPG"
    Set imagePos = Range("B2:D8")
    
    Call InsertPictureInRange(imagepath, imagePos)

End Sub


' inserts a picture and resizes it to fit the TargetCells range
Sub InsertPictureInRange(PictureFileName As String, TargetCells As Range)
    If TypeName(ActiveSheet) <> "Worksheet" Then Exit Sub
    
    If Dir(PictureFileName) = "" Then Exit Sub
    
    Dim p As Object, t As Double, l As Double, w As Double, h As Double
    
    ' import picture
    Set p = ActiveSheet.Pictures.Insert(PictureFileName)
    
    ' determine positions
    With TargetCells
    
        t = .Top
        
        l = .Left
        
        w = .Offset(0, .Columns.Count).Left - .Left
        
        h = .Offset(.Rows.Count, 0).Top - .Top
    
    End With
    
    ' position picture
    With p
    
        .Top = t
        
        .Left = l
        
        .Width = w
        
        .Height = h
    
    End With
    
    Set p = Nothing

End Sub


Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Lập trình hệ thống nhúng Linux . Powered by Luong Duy Ninh -