Excel 选择将图像作为图像或注释插入

Excel 选择将图像作为图像或注释插入,excel,vba,Excel,Vba,我有一个代码,它使用一组特定的数字从给定的路径插入图像,我已经有了一个图像数据库 Sub InsertPics() Dim fPath As String, fName As String Dim r As Range, rng As Range Dim shpPic As Shape Application.ScreenUpdating = False fPath = "C:\Users\DELL\Documents\FY18-19\Images\"

我有一个代码,它使用一组特定的数字从给定的路径插入图像,我已经有了一个图像数据库

Sub InsertPics()
    Dim fPath As String, fName As String
    Dim r As Range, rng As Range
    Dim shpPic As Shape
    Application.ScreenUpdating = False
    fPath = "C:\Users\DELL\Documents\FY18-19\Images\"
    Set rng = Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
    For Each r In rng
    On Error GoTo errHandler
    If r.Value <> "" Then
        Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
            savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-1, Height:=-1)
        With shpPic
            .LockAspectRatio = msoTrue
            If .Width > Columns(2).Width Then .Width = Columns(2).Width
            Rows(r.Row).RowHeight = .Height
        End With
    End If
    errHandler:
    If Err.Number <> 0 Then
        Debug.Print Err.Number & ", " & Err.Description & ", " & r.Value
        On Error GoTo -1
    End If
    Next r
    Application.ScreenUpdating = True
End Sub
Sub InsertPics()
Dim fPath作为字符串,fName作为字符串
变暗r为范围,rng为范围
像形状一样暗淡
Application.ScreenUpdating=False
fPath=“C:\Users\DELL\Documents\FY18-19\Images\”
设置rng=范围(“D2:D”和单元格(Rows.Count,4)。结束(xlUp)。行)
对于rng中的每个r
关于错误转到错误处理程序
如果r.值为“”,则
设置shpPic=ActiveSheet.Shapes.AddPicture(文件名:=fPath&r.Value&“.jpg”,linktofile:=msoFalse_
savewithdocument:=msoTrue,左:=单元格(右行,2)。左,顶部:=单元格(右行,2)。顶部,宽度=-1,高度=-1)
与shpPic
.LockAspectRatio=msoTrue
如果.Width>列(2).Width,则.Width=列(2).Width
行(右行)。行高=.Height
以
如果结束
错误处理程序:
如果错误号为0,则
调试.打印错误号&“,”错误说明&“,”错误值
关于错误GoTo-1
如果结束
下一个r
Application.ScreenUpdating=True
端接头
我还需要做以下工作:

  • 询问文件路径
  • 询问是将图像作为图像插入,还是作为对这些数字集的注释插入,并相应地运行
  • 如果代码可以转换为选择模式运行,即在一组数字上运行代码(而不是当前嵌入的整个“D”列)

  • 可以尝试此代码并根据您的要求进行修改

    Sub InsertPics()
        Dim fPath As String, fName As String
        Dim r As Range, rng As Range
        Dim shpPic As Shape, IsCmnt As VbMsgBoxResult
    
    
        'Application.ScreenUpdating = False
        Set rng = ThisWorkbook.ActiveSheet.Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
    
        On Error GoTo Xexit
        Set rng = Application.InputBox("Select the range to import Images", "Import Image", rng.Address, , , , , 8)
        On Error GoTo 0
    
        If rng Is Nothing Then Exit Sub
    
        With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = " Select Folder to Upload Images"
        .AllowMultiSelect = False
        .InitialFileName = "C:\Users\user\DeskTop\"
        If .Show <> -1 Then Exit Sub
        fPath = .SelectedItems(1)
        End With
        fPath = fPath & "\"
    
        'Avoided further asking wheather all Images are to be uploaded as Comment
        'instead used bold font of the file names to do the same
        'try Next statement, if want all the images as comment
        'IsCmnt = MsgBox("Is the images to be uploaded as comments", vbYesNo)
    
    
      For Each r In rng
         If r.Value <> "" Then
            If Dir(fPath & r.Value & ".jpg") <> "" Then
    
                'If IsCmnt = vbYes Then    'try this branch if want all the images as comment
                If r.Font.Bold Then  ' instead of asking multiple times
                r.ClearComments
                r.AddComment ""
                r.Comment.Shape.Fill.UserPicture fPath & r.Value & ".jpg"
                Else
                Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
                savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-1, Height:=-1)
                    With shpPic
                    .LockAspectRatio = msoTrue
                    If .Width > Columns(2).Width Then .Width = Columns(2).Width
                    Rows(r.Row).RowHeight = .Height
                End With
                End If
            Else
            Debug.Print fPath & r.Value & ".jpg not found"
            End If
         End If
      Next r
    
    Xexit:
    'Application.ScreenUpdating = True
    End Sub
    
    Sub InsertPics()
    Dim fPath作为字符串,fName作为字符串
    变暗r为范围,rng为范围
    尺寸shpPic为形状,IsCmnt为VbMsgBoxResult
    'Application.ScreenUpdate=False
    Set rng=thiswook.ActiveSheet.Range(“D2:D”单元格(Rows.Count,4).End(xlUp).Row)
    关于错误GoTo Xexit
    设置rng=Application.InputBox(“选择要导入图像的范围”,“导入图像”,rng.Address,,,8)
    错误转到0
    如果rng为空,则退出Sub
    使用Application.FileDialog(msoFileDialogFolderPicker)
    .Title=“选择要上载图像的文件夹”
    .AllowMultiSelect=False
    .InitialFileName=“C:\Users\user\DeskTop\”
    如果.Show-1,则退出Sub
    fPath=.SelectedItems(1)
    以
    fPath=fPath&“\”
    避免进一步询问是否所有图片都将作为评论上传
    '而是使用文件名的粗体字体来执行相同的操作
    '如果希望所有图像都作为注释,请尝试下一个语句
    'IsCmnt=MsgBox(“是作为评论上传的图像”,vbYesNo)
    对于rng中的每个r
    如果r.值为“”,则
    如果Dir(fPath&r.Value&“.jpg”)”,则
    '如果IsCmnt=vbYes,则'如果希望所有图像都作为注释,请尝试此分支
    如果是r.Font.Bold,则“而不是多次询问”
    r、 清晰评论
    r、 添加注释“”
    r、 Comment.Shape.Fill.UserPicture fPath和r.Value&“.jpg”
    其他的
    设置shpPic=ActiveSheet.Shapes.AddPicture(文件名:=fPath&r.Value&“.jpg”,linktofile:=msoFalse_
    savewithdocument:=msoTrue,左:=单元格(右行,2)。左,顶部:=单元格(右行,2)。顶部,宽度=-1,高度=-1)
    与shpPic
    .LockAspectRatio=msoTrue
    如果.Width>列(2).Width,则.Width=列(2).Width
    行(右行)。行高=.Height
    以
    如果结束
    其他的
    Debug.Print fPath&r.Value&“.jpg未找到”
    如果结束
    如果结束
    下一个r
    Xexit:
    'Application.ScreenUpdating=True
    端接头
    

    代码使用临时图像进行测试。可根据实际情况禁用
    屏幕更新

    可以尝试此代码并根据您的要求进行修改

    Sub InsertPics()
        Dim fPath As String, fName As String
        Dim r As Range, rng As Range
        Dim shpPic As Shape, IsCmnt As VbMsgBoxResult
    
    
        'Application.ScreenUpdating = False
        Set rng = ThisWorkbook.ActiveSheet.Range("D2:D" & Cells(Rows.Count, 4).End(xlUp).Row)
    
        On Error GoTo Xexit
        Set rng = Application.InputBox("Select the range to import Images", "Import Image", rng.Address, , , , , 8)
        On Error GoTo 0
    
        If rng Is Nothing Then Exit Sub
    
        With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = " Select Folder to Upload Images"
        .AllowMultiSelect = False
        .InitialFileName = "C:\Users\user\DeskTop\"
        If .Show <> -1 Then Exit Sub
        fPath = .SelectedItems(1)
        End With
        fPath = fPath & "\"
    
        'Avoided further asking wheather all Images are to be uploaded as Comment
        'instead used bold font of the file names to do the same
        'try Next statement, if want all the images as comment
        'IsCmnt = MsgBox("Is the images to be uploaded as comments", vbYesNo)
    
    
      For Each r In rng
         If r.Value <> "" Then
            If Dir(fPath & r.Value & ".jpg") <> "" Then
    
                'If IsCmnt = vbYes Then    'try this branch if want all the images as comment
                If r.Font.Bold Then  ' instead of asking multiple times
                r.ClearComments
                r.AddComment ""
                r.Comment.Shape.Fill.UserPicture fPath & r.Value & ".jpg"
                Else
                Set shpPic = ActiveSheet.Shapes.AddPicture(Filename:=fPath & r.Value & ".jpg", linktofile:=msoFalse, _
                savewithdocument:=msoTrue, Left:=Cells(r.Row, 2).Left, Top:=Cells(r.Row, 2).Top, Width:=-1, Height:=-1)
                    With shpPic
                    .LockAspectRatio = msoTrue
                    If .Width > Columns(2).Width Then .Width = Columns(2).Width
                    Rows(r.Row).RowHeight = .Height
                End With
                End If
            Else
            Debug.Print fPath & r.Value & ".jpg not found"
            End If
         End If
      Next r
    
    Xexit:
    'Application.ScreenUpdating = True
    End Sub
    
    Sub InsertPics()
    Dim fPath作为字符串,fName作为字符串
    变暗r为范围,rng为范围
    尺寸shpPic为形状,IsCmnt为VbMsgBoxResult
    'Application.ScreenUpdate=False
    Set rng=thiswook.ActiveSheet.Range(“D2:D”单元格(Rows.Count,4).End(xlUp).Row)
    关于错误GoTo Xexit
    设置rng=Application.InputBox(“选择要导入图像的范围”,“导入图像”,rng.Address,,,8)
    错误转到0
    如果rng为空,则退出Sub
    使用Application.FileDialog(msoFileDialogFolderPicker)
    .Title=“选择要上载图像的文件夹”
    .AllowMultiSelect=False
    .InitialFileName=“C:\Users\user\DeskTop\”
    如果.Show-1,则退出Sub
    fPath=.SelectedItems(1)
    以
    fPath=fPath&“\”
    避免进一步询问是否所有图片都将作为评论上传
    '而是使用文件名的粗体字体来执行相同的操作
    '如果希望所有图像都作为注释,请尝试下一个语句
    'IsCmnt=MsgBox(“是作为评论上传的图像”,vbYesNo)
    对于rng中的每个r
    如果r.值为“”,则
    如果Dir(fPath&r.Value&“.jpg”)”,则
    '如果IsCmnt=vbYes,则'如果希望所有图像都作为注释,请尝试此分支
    如果是r.Font.Bold,则“而不是多次询问”
    r、 清晰评论
    r、 添加注释“”
    r、 Comment.Shape.Fill.UserPicture fPath和r.Value&“.jpg”
    其他的
    设置shpPic=ActiveSheet.Shapes.AddPicture(文件名:=fPath&r.Value&“.jpg”,linktofile:=msoFalse_
    savewithdocument:=msoTrue,左:=单元格(右行,2)。左,顶部:=单元格(右行,2)。顶部,宽度=-1,高度=-1)
    与shpPic
    .LockAspectRatio=msoTrue
    如果.Width>列(2).Width,则.Width=列(2).Width
    行(右行)。行高=.Height
    以
    如果结束
    其他的
    Debug.Print fPath&r.Value&“.jpg未找到”
    如果结束
    如果结束
    下一个r
    Xexit:
    'Application.ScreenUpdating=True
    端接头
    
    代码使用临时图像进行测试。可根据实际情况禁用
    屏幕更新