将图像添加为注释VBA

将图像添加为注释VBA,vba,excel,comments,Vba,Excel,Comments,我找到了将图像插入excel 2013的代码,但是图像比要插入的单元格大。我认为最好的选择是将图片作为评论加载 是否有人可以修改下面的VBA以将其添加为注释 Sub URLPictureInsert() Dim cell, shp As Shape, target As Range Set rng = ActiveSheet.Range("R2:R5") ' range with URLs For Each cell In rng filenam = cell

我找到了将图像插入excel 2013的代码,但是图像比要插入的单元格大。我认为最好的选择是将图片作为评论加载

是否有人可以修改下面的VBA以将其添加为注释

Sub URLPictureInsert()
Dim cell, shp As Shape, target As Range
    Set rng = ActiveSheet.Range("R2:R5") ' range with URLs
    For Each cell In rng
       filenam = cell
       ActiveSheet.Pictures.Insert(filenam).Select

  Set shp = Selection.ShapeRange.Item(1)
   With shp
      .LockAspectRatio = msoTrue
      .Width = 50
      .Height = 50
      .Cut
   End With
   Cells(cell.Row, cell.Column + 5).PasteSpecial

Next

End Sub

我相信下面的链接有你想要的


我相信下面的链接有你想要的


如果希望图像与目标单元格高度大小匹配,请使用:

With shp
    .LockAspectRatio = msoTrue
    '.Width = Cells(cell.Row, cell.Column + 5).Width 'Uncomment this  line and comment out .Height line to match cell width
    .Height = Cells(cell.Row, cell.Column + 5).Height 
    .Cut
End With
如果要同时匹配单元格和高度,请使用:

With shp
    .LockAspectRatio = msoFalse
    .Width = Cells(cell.Row, cell.Column + 5).Width
    .Height = Cells(cell.Row, cell.Column + 5).Height
    .Cut
End With

如果希望图像与目标单元格高度大小匹配,请使用:

With shp
    .LockAspectRatio = msoTrue
    '.Width = Cells(cell.Row, cell.Column + 5).Width 'Uncomment this  line and comment out .Height line to match cell width
    .Height = Cells(cell.Row, cell.Column + 5).Height 
    .Cut
End With
如果要同时匹配单元格和高度,请使用:

With shp
    .LockAspectRatio = msoFalse
    .Width = Cells(cell.Row, cell.Column + 5).Width
    .Height = Cells(cell.Row, cell.Column + 5).Height
    .Cut
End With

我更新了上面的代码,并且从“B”列(第2列)获取图像的路径。我在单元格上单击我的宏:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim listWS As Worksheet
Dim targetCol, targetRow As Long
Dim TheFile As String

Set listWS = Application.ThisWorkbook.Sheets("Catalogue")
    If Target.Column = 2 Then
        targetCol = Target.Column
        targetRow = Target.Row
        TheFile = listWS.Cells(targetRow, targetCol).Value
        With listWS.Range(listWS.Cells(targetRow, 4), listWS.Cells(targetRow, 4))
            .AddComment
            .Comment.Visible = True
            .Comment.Shape.Fill.UserPicture TheFile
        End With
    End If
End Sub

我更新了上面的代码,并且从“B”列(第2列)获取图像的路径。我在单元格上单击我的宏:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim listWS As Worksheet
Dim targetCol, targetRow As Long
Dim TheFile As String

Set listWS = Application.ThisWorkbook.Sheets("Catalogue")
    If Target.Column = 2 Then
        targetCol = Target.Column
        targetRow = Target.Row
        TheFile = listWS.Cells(targetRow, targetCol).Value
        With listWS.Range(listWS.Cells(targetRow, 4), listWS.Cells(targetRow, 4))
            .AddComment
            .Comment.Visible = True
            .Comment.Shape.Fill.UserPicture TheFile
        End With
    End If
End Sub

这将在单击的单元格上快速添加图片作为注释。它还可以根据我所做项目的喜好调整大小

With Application.FileDialog(msoFileDialogFilePicker)
     .AllowMultiSelect = False          'Only one file
     .InitialFileName = CurDir         'directory to open the window
     .Filters.Clear                    'Cancel the filter
     .Filters.Add Description:="Images", Extensions:="*.png", Position:=1
     .Title = "Choose image"

     If .Show = -1 Then TheFile = .SelectedItems(1) Else TheFile = 0
End With
'No file selected
If TheFile = 0 Then
MsgBox ("No image selected")
Exit Sub
End If
Selection.AddComment
Selection.Comment.Visible = True
Selection.Comment.Shape.Fill.UserPicture TheFile
Selection.Comment.Shape.Select True
Selection.ShapeRange.ScaleWidth 2.6, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 2.8, msoFalse, msoScaleFromTopLeft
ActiveCell.Comment.Visible = False

这将在单击的单元格上快速添加图片作为注释。它还可以根据我所做项目的喜好调整大小

With Application.FileDialog(msoFileDialogFilePicker)
     .AllowMultiSelect = False          'Only one file
     .InitialFileName = CurDir         'directory to open the window
     .Filters.Clear                    'Cancel the filter
     .Filters.Add Description:="Images", Extensions:="*.png", Position:=1
     .Title = "Choose image"

     If .Show = -1 Then TheFile = .SelectedItems(1) Else TheFile = 0
End With
'No file selected
If TheFile = 0 Then
MsgBox ("No image selected")
Exit Sub
End If
Selection.AddComment
Selection.Comment.Visible = True
Selection.Comment.Shape.Fill.UserPicture TheFile
Selection.Comment.Shape.Select True
Selection.ShapeRange.ScaleWidth 2.6, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 2.8, msoFalse, msoScaleFromTopLeft
ActiveCell.Comment.Visible = False

将以下代码粘贴到此工作簿中,然后将其关闭并打开。 无论何时在单元格中粘贴屏幕截图,它都会自动调整大小

Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Declare PtrSafe Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare PtrSafe Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
#End If

Private WithEvents CmndBras As CommandBars


Private Sub Workbook_Open()
Set CmndBras = Application.CommandBars
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)
Set CmndBras = Application.CommandBars
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set CmndBras = Nothing
End Sub


Private Sub CmndBras_OnUpdate()
Dim oShp As Shape

On Error Resume Next
If TypeName(Selection) <> "Range" Then
    If ScreenShotInClipBoard Then
        Set oShp = Selection.Parent.Shapes(Selection.Name)
        With oShp
        If .AlternativeText <> "Tagged" Then
            If .Type = msoPicture Then
                If Err.Number = 0 Then
                    .AlternativeText = "Tagged"
                    .Visible = False
                    .LockAspectRatio = msoFalse
                    .Top = ActiveWindow.RangeSelection.Top
                    .Left = ActiveWindow.RangeSelection.Left
                    .Width = ActiveWindow.RangeSelection.Width
                    .Height = ActiveWindow.RangeSelection.Height
                    ActiveWindow.RangeSelection.Activate
                    .Visible = True
                End If
            End If
        End If
        End With
    End If
End If
End Sub


Private Function ScreenShotInClipBoard() As Boolean
Dim sClipboardFormatName As String, sBuffer As String
Dim CF_Format As Long, i As Long
Dim bDtataInClipBoard As Boolean

If OpenClipboard(0) Then
    CF_Format = EnumClipboardFormats(0&)
    Do While CF_Format <> 0
        sClipboardFormatName = String(255, vbNullChar)
        i = GetClipboardFormatName(CF_Format, sClipboardFormatName, 255)
        sBuffer = sBuffer & Left(sClipboardFormatName, i)
       bDtataInClipBoard = True
         CF_Format = EnumClipboardFormats(CF_Format)
    Loop
    CloseClipboard
 End If
 ScreenShotInClipBoard = bDtataInClipBoard And Len(sBuffer) = 0
End Function
选项显式
#如果是VBA7,则
私有声明PtrSafe函数OpenClipboard Lib“user32”(ByVal hwnd作为LongPtr)作为Long
私有声明PtrSafe函数CloseClipboard Lib“user32”(长度为
私有声明PtrSafe函数EnumClipboardFormats Lib“user32”(ByVal wFormat As Long)尽可能长
私有声明PtrSafe函数GetClipboardFormatName Lib“user32”别名“GetClipboardFormatNameA”(ByVal wFormat为Long,ByVal lpString为String,ByVal nMaxCount为Long)为Long
#否则
私有声明函数OpenClipboard Lib“user32”(ByVal hwnd作为Long)作为Long
私有声明函数CloseClipboard Lib“user32”(长度为
私有声明函数EnumClipboardFormats Lib“user32”(ByVal wFormat As Long)的长度
私有声明函数GetClipboardFormatName Lib“user32”别名“GetClipboardFormatNameA”(ByVal wFormat为Long,ByVal lpString为String,ByVal nMaxCount为Long)为Long
#如果结束
将事件CmndBras用作命令栏的专用
私有子工作簿_Open()
设置CmndBras=Application.commandbar
端接头
私有子工作簿\u Sheet SelectionChange(ByVal sh作为对象,ByVal目标作为范围)
设置CmndBras=Application.commandbar
端接头
私有子工作簿\u关闭前(取消为布尔值)
设置CmndBras=Nothing
端接头
私有子CmndBras_OnUpdate()
将oShp调暗为形状
出错时继续下一步
如果TypeName(选择)“范围”,则
如果屏幕截图包含在黑板上,那么
设置oShp=Selection.Parent.Shapes(Selection.Name)
与oShp合作
如果.AlternativeText“标记”,则
如果.Type=msoPicture,则
如果Err.Number=0,则
.AlternativeText=“标记”
.Visible=False
.LockAspectRatio=msoFalse
.Top=ActiveWindow.RangeSelection.Top
.Left=ActiveWindow.RangeSelection.Left
.Width=ActiveWindow.RangeSelection.Width
.Height=ActiveWindow.RangeSelection.Height
ActiveWindow.RangeSelection.Activate
.Visible=True
如果结束
如果结束
如果结束
以
如果结束
如果结束
端接头
私有函数ScreenShotInClipBoard()为布尔值
Dim sClipboardFormatName作为字符串,sBuffer作为字符串
Dim CF_格式为长,i为长
将bDtataInClipBoard设置为布尔值
如果打开剪贴板(0),则
CF_Format=EnumClipboardFormats(0&)
当CF_格式为0时执行
sClipboardFormatName=字符串(255,vbNullChar)
i=GetClipboardFormatName(CF_格式,sClipboardFormatName,255)
sBuffer=sBuffer&Left(sClipboardFormatName,i)
bDtataInClipBoard=True
CF_格式=枚举剪贴板格式(CF_格式)
环
关闭剪贴板
如果结束
ScreenShotInClipBoard=bDtataInClipBoard和Len(sBuffer)=0
端函数

将以下代码粘贴到此工作簿中,然后将其关闭并打开。 无论何时在单元格中粘贴屏幕截图,它都会自动调整大小

Option Explicit

#If VBA7 Then
Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
Private Declare PtrSafe Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare PtrSafe Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
#Else
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function EnumClipboardFormats Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GetClipboardFormatName Lib "user32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
#End If

Private WithEvents CmndBras As CommandBars


Private Sub Workbook_Open()
Set CmndBras = Application.CommandBars
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal sh As Object, ByVal Target As Range)
Set CmndBras = Application.CommandBars
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Set CmndBras = Nothing
End Sub


Private Sub CmndBras_OnUpdate()
Dim oShp As Shape

On Error Resume Next
If TypeName(Selection) <> "Range" Then
    If ScreenShotInClipBoard Then
        Set oShp = Selection.Parent.Shapes(Selection.Name)
        With oShp
        If .AlternativeText <> "Tagged" Then
            If .Type = msoPicture Then
                If Err.Number = 0 Then
                    .AlternativeText = "Tagged"
                    .Visible = False
                    .LockAspectRatio = msoFalse
                    .Top = ActiveWindow.RangeSelection.Top
                    .Left = ActiveWindow.RangeSelection.Left
                    .Width = ActiveWindow.RangeSelection.Width
                    .Height = ActiveWindow.RangeSelection.Height
                    ActiveWindow.RangeSelection.Activate
                    .Visible = True
                End If
            End If
        End If
        End With
    End If
End If
End Sub


Private Function ScreenShotInClipBoard() As Boolean
Dim sClipboardFormatName As String, sBuffer As String
Dim CF_Format As Long, i As Long
Dim bDtataInClipBoard As Boolean

If OpenClipboard(0) Then
    CF_Format = EnumClipboardFormats(0&)
    Do While CF_Format <> 0
        sClipboardFormatName = String(255, vbNullChar)
        i = GetClipboardFormatName(CF_Format, sClipboardFormatName, 255)
        sBuffer = sBuffer & Left(sClipboardFormatName, i)
       bDtataInClipBoard = True
         CF_Format = EnumClipboardFormats(CF_Format)
    Loop
    CloseClipboard
 End If
 ScreenShotInClipBoard = bDtataInClipBoard And Len(sBuffer) = 0
End Function
选项显式
#如果是VBA7,则
私有声明PtrSafe函数OpenClipboard Lib“user32”(ByVal hwnd作为LongPtr)作为Long
私有声明PtrSafe函数CloseClipboard Lib“user32”(长度为
私有声明PtrSafe函数EnumClipboardFormats Lib“user32”(ByVal wFormat As Long)尽可能长
私有声明PtrSafe函数GetClipboardFormatName Lib“user32”别名“GetClipboardFormatNameA”(ByVal wFormat为Long,ByVal lpString为String,ByVal nMaxCount为Long)为Long
#否则
私有声明函数OpenClipboard Lib“user32”(ByVal hwnd作为Long)作为Long
私有声明函数CloseClipboard Lib“user32”(长度为
私有声明函数EnumClipboardFormats Lib“user32”(ByVal wFormat As Long)的长度
私有声明函数GetClipboardFormatName Lib“user32”别名“GetClipboardFormatNameA”(ByVal wFormat为Long,ByVal lpString为String,ByVal nMaxCount为Long)为Long
#如果结束
将事件CmndBras用作命令栏的专用
私有子工作簿_Open()
设置CmndBras=Application.commandbar
端接头
私有子工作簿\u Sheet SelectionChange(ByVal sh作为对象,ByVal目标作为范围)
设置CmndBras=Application.commandbar
端接头
私有子工作簿\u关闭前(取消为布尔值)
设置CmndBras=Nothing
端接头
私有子CmndBras_OnUpdate()
将oShp调暗为形状
出错时继续下一步
如果TypeName(选择)“范围”,则
如果屏幕截图包含在黑板上,那么
设置oShp=Selection.Parent.Shapes(Selection.Name)
与oShp合作
如果.AlternativeText“标记”,则
如果.Type=msoPicture,则
如果Err.Number=0,则