在Excel单元格中插入图像

我知道这个问题已经有好几次被问过了,所以如果这个问题重复出现,请标记为重复。

我想在Excel中创build一个清单,我需要一个快速的方式插入单元格中的图像。 我看到你可以通过插入注释来做到这一点,但它只是一个非常缓慢的过程。

这可以通过VBA或脚本将文件夹中的图片添加到所需的单元格?

看看下面的代码:

Public Sub sampleCode() Dim WB As Workbook Dim targetWS As Worksheet Dim targetCell As Range Dim imageCounter As Long Dim sourceFolderPath As String Dim dirStr As String Dim imageObj As Picture Set WB = ThisWorkbook Set targetWS = WB.Sheets(1) With Application.FileDialog(msoFileDialogFolderPicker) .Title = "Select the folder that contains the pictures." .Show sourceFolderPath = .SelectedItems(1) & "\" End With dirStr = Dir(sourceFolderPath) Do Until dirStr = "" If Right(dirStr, 3) = "png" Or Right(dirStr, 3) = "jpg" Then imageCounter = imageCounter + 1 Set imageObj = targetWS.Pictures.Insert(sourceFolderPath & dirStr) Set targetCell = targetWS.Range("A" & imageCounter) With targetCell .ColumnWidth = imageObj.Width / 5.4636515404462 .RowHeight = imageObj.Height imageObj.Top = .Top imageObj.Left = .Left End With End If dirStr = Dir Loop End Sub 

问候,TheSilkCode