Tự động chỉnh ảnh vừa với ô trong bảng excel (1 người xem)

  • Thread starter Thread starter Trojan
  • Ngày gửi Ngày gửi
Liên hệ QC

Người dùng đang xem chủ đề này

Trojan

Thành viên hoạt động
Tham gia
13/3/08
Bài viết
162
Được thích
78
Chào cả nhà, tôi có 1 vấn đề này nhờ các cao thủ VBE giúp đỡ vì tôi tìm kiếm trên mạng có code nhưng chưa hợp lý lắm. Vấn đề của tôi là tôi cần chèn nhiều ảnh vào 1 sheet excel. Tuy nhiên mỗi ảnh 1 cỡ nên khi chèn vào sheet thì ảnh to ảnh nhỏ. Vậy nhờ các bác giúp đỡ hoặc ai có code VBE cho tôi xin với để làm sao chạy xong thì các ảnh đó tự căn chỉnh cho vừa chiều cao và chiều rộng của cell chứa ảnh đó mà không cần chỉnh lại bằng tay nữa.
Chân thành cảm ơn mọi ý kiến giúp đỡ.
 
Chào cả nhà, tôi có 1 vấn đề này nhờ các cao thủ VBE giúp đỡ vì tôi tìm kiếm trên mạng có code nhưng chưa hợp lý lắm. Vấn đề của tôi là tôi cần chèn nhiều ảnh vào 1 sheet excel. Tuy nhiên mỗi ảnh 1 cỡ nên khi chèn vào sheet thì ảnh to ảnh nhỏ. Vậy nhờ các bác giúp đỡ hoặc ai có code VBE cho tôi xin với để làm sao chạy xong thì các ảnh đó tự căn chỉnh cho vừa chiều cao và chiều rộng của cell chứa ảnh đó mà không cần chỉnh lại bằng tay nữa.
Chân thành cảm ơn mọi ý kiến giúp đỡ.
hình như topic này đúng ý bạn cần nè:
http://www.giaiphapexcel.com/forum/showthread.php?51408-Chèn-hình-vào-cell-bằng-hàm-tự-tạo
 
Upvote 0
Cái này nó lại đưa vào comment bác à. Nhu cầu của tôi là đưa vào cell, với lại dùng hàm hơi bất tiện.
 
Upvote 0
Chào cả nhà, tôi có 1 vấn đề này nhờ các cao thủ VBE giúp đỡ vì tôi tìm kiếm trên mạng có code nhưng chưa hợp lý lắm. Vấn đề của tôi là tôi cần chèn nhiều ảnh vào 1 sheet excel. Tuy nhiên mỗi ảnh 1 cỡ nên khi chèn vào sheet thì ảnh to ảnh nhỏ. Vậy nhờ các bác giúp đỡ hoặc ai có code VBE cho tôi xin với để làm sao chạy xong thì các ảnh đó tự căn chỉnh cho vừa chiều cao và chiều rộng của cell chứa ảnh đó mà không cần chỉnh lại bằng tay nữa.
Chân thành cảm ơn mọi ý kiến giúp đỡ.

Module 1,Named it as "basAddPictrue":
Mã:
Option Explicit
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>   Author:     Joforn                            <<<<<<<<<<<<<<<<<<
'>>>>>>>>   Email:      Joforn@sohu.com                   <<<<<<<<<<<<<<<<<<
'>>>>>>>>   QQ:         42978116                          <<<<<<<<<<<<<<<<<<
'>>>>>>>>   Last time : 11/16/2015                        <<<<<<<<<<<<<<<<<<
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#If VBA7 Then
  Private Declare PtrSafe Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As LongPtr, ByRef Image As LongPtr) As LongPtr
  Private Declare PtrSafe Function GdipGetImageHeight Lib "GdiPlus.dll" (ByVal mImage As LongPtr, ByRef mHeight As Long) As Long
  Private Declare PtrSafe Function GdipGetImageWidth Lib "GdiPlus.dll" (ByVal mImage As LongPtr, ByRef mWidth As Long) As Long
  Private Declare PtrSafe Function GdipDisposeImage Lib "GdiPlus.dll" (ByVal mImage As LongPtr) As Long
#Else
  Private Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal FileName As Long, ByRef Image As Long) As Long
  Private Declare Function GdipGetImageHeight Lib "GdiPlus.dll" (ByVal mImage As Long, ByRef mHeight As Long) As Long
  Private Declare Function GdipGetImageWidth Lib "GdiPlus.dll" (ByVal mImage As Long, ByRef mWidth As Long)  As Long
  Private Declare Function GdipDisposeImage Lib "GdiPlus.dll" (ByVal mImage As Long) As Long
#End If

Public Function AddPictureToRange(ByVal Target As Range, ByVal PicFileName As String, _
  Optional ByVal AutoSize As Boolean = True) As Shape
  Dim rngWidth As Single, rngHeight As Single, picWidth As Single, picHeight As Single
  
  If Target Is Nothing Then Exit Function
  picWidth = GetImageWidth(PicFileName): picHeight = GetImageHeight(PicFileName)
  If (picWidth > 0) And (picHeight > 0) Then
    Set Target = Target.MergeArea
    rngWidth = RangeWidth(Target): rngHeight = RangeHeight(Target)

    With Target
      Set AddPictureToRange = .Parent.Shapes.AddPicture(PicFileName, msoFalse, msoTrue, .Left, .Top, rngWidth, rngHeight)
      If AutoSize Then
        With AddPictureToRange
          picWidth = ScreenScaleX(picWidth, vbPixels, vbPoints)
          picHeight = ScreenScaleY(picHeight, vbPixels, vbPoints)
          GetAutoSize rngWidth, picWidth, rngHeight, picHeight
          .LockAspectRatio = msoFalse
          .Left = .Left + (.Width - picWidth) / 2
          .Top = .Top + (.Height - picHeight) / 2
          .Width = picWidth
          .Height = picHeight
          .LockAspectRatio = msoTrue
        End With
      End If
    End With
  End If
End Function

Public Function GetImageHeight(ByVal PicFileName As String) As Long
  Dim mHeight As Long
  #If VBA7 Then
    Dim mImage As LongPtr
  #Else
    Dim mIamge As Long
  #End If
  
  GdipLoadImageFromFile StrPtr(PicFileName), mImage
  If mImage Then
    GdipGetImageHeight mImage, mHeight
    GetImageHeight = mHeight
    GdipDisposeImage mImage
  End If
End Function

Public Function GetImageWidth(ByVal PicFileName As String) As Long
  Dim mWidth As Long
  #If VBA7 Then
    Dim mImage As LongPtr
  #Else
    Dim mIamge As Long
  #End If
  
  GdipLoadImageFromFile StrPtr(PicFileName), mImage
  If mImage Then
    GdipGetImageWidth mImage, mWidth
    GetImageWidth = mWidth
    GdipDisposeImage mImage
  End If
End Function

Private Sub GetAutoSize(ByVal MaxWidth As Single, ByRef mWidth As Single, _
                         ByVal MaxHeight As Single, ByRef mHeight As Single)
  Dim Proportion As Single
  Dim sgnTemp    As Single
  
  mHeight = mHeight * (MaxWidth / mWidth)
  mWidth = MaxWidth
  If MaxHeight < mHeight Then GetAutoSize MaxHeight, mHeight, MaxWidth, mWidth
End Sub

Module 2,Named it as "basScreen":

Mã:
Option Explicit
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'>>>>>>>>   Author:     Joforn                            <<<<<<<<<<<<<<<<<<
'>>>>>>>>   Email:      Joforn@sohu.com                   <<<<<<<<<<<<<<<<<<
'>>>>>>>>   QQ:         42978116                          <<<<<<<<<<<<<<<<<<
'>>>>>>>>   Last time : 11/16/2015                        <<<<<<<<<<<<<<<<<<
'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#If VBA7 Then
  Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hWnd As Long) As LongPtr
  Private Declare PtrSafe Function GetDeviceCaps Lib "gdi32" (ByVal hDc As LongPtr, ByVal nIndex As Long) As Long
  Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDc As LongPtr) As Long
#Else
  Private Declare Function GetDC Lib "user32"(ByVal hWnd As Long) As Long
  Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
  Private Declare Function ReleaseDC Lib "user32"(ByVal hWnd As Long,ByVal hDC As Long) As Long
#End If

Private Const HORZRES       As Long = 8
Private Const VERTRES       As Long = 10
Private Const LOGPIXELSX    As Long = 88
Private Const LOGPIXELSY    As Long = 90

Public Enum EnumScale
  vbTwips = 1     '缇
  vbPoints        '磅
  vbPixels        '像素
  vbCharacters    '字符
  vbInches        '英寸
  vbMillimeters   '毫米
  vbCentimeters   '厘米
End Enum

Public Property Get ScreenLogPixelsX() As Long
  '获取屏幕分辨率宽度的每逻辑英寸的像素数
  #If VBA7 Then
    Dim hDc As LongPtr
  #Else
    Dim hDc As Long
  #End If
  hDc = GetDC(0)
  ScreenLogPixelsX = GetDeviceCaps(hDc, LOGPIXELSX)
  ReleaseDC 0, hDc
End Property

Public Property Get ScreenLogPixelsY() As Long
  '获取屏幕分辨率高度的每逻辑英寸的像素数
  #If VBA7 Then
    Dim hDc As LongPtr
  #Else
    Dim hDc As Long
  #End If
  hDc = GetDC(0)
  ScreenLogPixelsY = GetDeviceCaps(hDc, LOGPIXELSY)
  ReleaseDC 0, hDc
End Property

Public Property Get ScreenWidth() As Long
  '获取屏幕的宽度,以像素为单位
  #If VBA7 Then
    Dim hDc As LongPtr
  #Else
    Dim hDc As Long
  #End If
  hDc = GetDC(0)
  ScreenWidth = GetDeviceCaps(hDc, HORZRES)
  ReleaseDC 0, hDc
End Property

Public Property Get ScreenHeight() As Long
  '获取屏幕的高度,以像素为单位
  #If VBA7 Then
    Dim hDc As LongPtr
  #Else
    Dim hDc As Long
  #End If
  hDc = GetDC(0)
  ScreenHeight = GetDeviceCaps(hDc, VERTRES)
  ReleaseDC 0, hDc
End Property

Public Function ScreenScaleX(ByVal Width As Single, Optional ByVal FromScale As EnumScale = vbPoints, Optional ByVal ToScale As EnumScale = vbPixels) As Single
  Const TWIPSPERINCH = 1440!
  Const TWIPSPERCENT = 567!
  Const TWIPSPERMILL = 56.7!
  Const TWIPSPERCHAR = 120!
  Const TWIPSPERPOIT = 20!
  If FromScale = ToScale Then
    ScreenScaleX = Width
  Else
    Select Case FromScale
      Case vbTwips
        Select Case ToScale
          Case vbPoints
            ScreenScaleX = Width / TWIPSPERPOIT
          Case vbPixels
            ScreenScaleX = Width / TWIPSPERINCH * ScreenLogPixelsX
          Case vbCharacters
            ScreenScaleX = Width / TWIPSPERCHAR
          Case vbInches
            ScreenScaleX = Width / TWIPSPERINCH
          Case vbMillimeters
            ScreenScaleX = Width / TWIPSPERMILL
          Case vbCentimeters
            ScreenScaleX = Width / TWIPSPERCENT
          Case vbTwips
            ScreenScaleX = Width
        End Select
      Case vbCentimeters
        ScreenScaleX = ScreenScaleX(Width * TWIPSPERCENT, vbTwips, ToScale)
      Case vbMillimeters
        ScreenScaleX = ScreenScaleX(Width * TWIPSPERMILL, vbTwips, ToScale)
      Case vbInches
        ScreenScaleX = ScreenScaleX(Width * TWIPSPERINCH, vbTwips, ToScale)
      Case vbPoints
        ScreenScaleX = ScreenScaleX(Width * TWIPSPERPOIT, vbTwips, ToScale)
      Case vbPixels
        ScreenScaleX = ScreenScaleX(Width * TWIPSPERINCH / ScreenLogPixelsX, vbTwips, ToScale)
      Case vbCharacters
        ScreenScaleX = ScreenScaleX(Width * TWIPSPERCHAR, vbTwips, ToScale)
    End Select
  End If
End Function

Public Function ScreenScaleY(ByVal Height As Single, Optional ByVal FromScale As EnumScale = vbPoints, Optional ByVal ToScale As EnumScale = vbPixels) As Single
  Const TWIPSPERINCH = 1440!
  Const TWIPSPERCENT = 567!
  Const TWIPSPERMILL = 56.7!
  Const TWIPSPERCHAR = 240!
  Const TWIPSPERPOIT = 20!
  
  If FromScale = ToScale Then
    ScreenScaleY = Height
  Else
    Select Case FromScale
      Case vbTwips
        Select Case ToScale
          Case vbPoints
            ScreenScaleY = Height / TWIPSPERPOIT
          Case vbPixels
            ScreenScaleY = Height / TWIPSPERINCH * ScreenLogPixelsY
          Case vbCharacters
            ScreenScaleY = Height / TWIPSPERCHAR
          Case vbInches
            ScreenScaleY = Height / TWIPSPERINCH
          Case vbMillimeters
            ScreenScaleY = Height / TWIPSPERMILL
          Case vbCentimeters
            ScreenScaleY = Height / TWIPSPERCENT
          Case vbTwips
            ScreenScaleY = Height
        End Select
      Case vbCentimeters
        ScreenScaleY = ScreenScaleY(Height * TWIPSPERCENT, vbTwips, ToScale)
      Case vbMillimeters
        ScreenScaleY = ScreenScaleY(Height * TWIPSPERMILL, vbTwips, ToScale)
      Case vbInches
        ScreenScaleY = ScreenScaleY(Height * TWIPSPERINCH, vbTwips, ToScale)
      Case vbPoints
        ScreenScaleY = ScreenScaleY(Height * TWIPSPERPOIT, vbTwips, ToScale)
      Case vbPixels
        ScreenScaleY = ScreenScaleY(Height * TWIPSPERINCH / ScreenLogPixelsY, vbTwips, ToScale)
      Case vbCharacters
        ScreenScaleY = ScreenScaleY(Height * TWIPSPERCHAR, vbTwips, ToScale)
    End Select
  End If
End Function

Public Property Get RangeWidth(ByVal Target As Range, Optional ByVal ScaleMode As EnumScale = vbPoints) As Single
  Dim I As Long, rngWidth As Single
  If Target Is Nothing Then Exit Property
  If Target.Count Then
    Set Target = Target.Cells(1)
    Set Target = Target.MergeArea
    For I = 1 To Target.Columns.Count
      rngWidth = rngWidth + Target.Cells(1, I).Width
    Next
    RangeWidth = ScreenScaleX(rngWidth, vbPoints, ScaleMode)
  End If
End Property

Public Property Get RangeHeight(ByVal Target As Range, Optional ByVal ScaleMode As EnumScale = vbPoints) As Single
  Dim I As Long, rngHeight As Single
  If Target Is Nothing Then Exit Property
  If Target.Count Then
    Set Target = Target.Cells(1)
    Set Target = Target.MergeArea
    For I = 1 To Target.Rows.Count
      rngHeight = rngHeight + Target.Cells(I, 1).Height
    Next
    RangeHeight = ScreenScaleY(rngHeight, vbPoints, ScaleMode)
  End If
End Property

in the sheet1,Mergeed the Range("C8:C12"),Range("C18:C26") and Range("E5:I27"),and add a bouton set captional as "Add Picture" into this sheet,button's codes as:
Mã:
Private Sub CommandButton1_Click()
  Dim FileName As Variant
  FileName = Application.GetOpenFilename("Picture File,*.BMP;*.JPG;*.GIF;*.PNG", 1, "Please choose a pictrue file:")
  If VarType(FileName) = vbString Then
    AddPictureToRange Range("C8"), FileName
    AddPictureToRange Range("C23"), FileName
    AddPictureToRange Range("F18"), FileName
  End If
End Sub
click the button,you can see then example.
and this is a example XLSM file:
 

File đính kèm

Lần chỉnh sửa cuối:
Upvote 0
Cái này nó lại đưa vào comment bác à. Nhu cầu của tôi là đưa vào cell, với lại dùng hàm hơi bất tiện.
có gì bất lợi không bạn. miễn sao hiện ra ngon lành cành đào được rồi.
mình đang dùng thấy rất tuyệt
code trong trang trên là tự điều chỉnh tấm ảnh vừa với cell cho dù cell đó có Merge nhiều dòng nhiều cột
có thể kiến thức mình chưa được tới đâu nhưng mình chưa nghe nói là có thể chèn ảnh vào cell được bạn có thể làm mẫu được không
 
Lần chỉnh sửa cuối:
Upvote 0

Bài viết mới nhất

Back
Top Bottom