我知道这个问题已经在几个方面被多次询问过,所以如果弹出一个副本,请标记为重复。
我想在excel中创建一个库存,我需要一种快速的方法在单元格中插入图像。我看到你可以通过插入注释来实现这一点,但它只是一个非常缓慢的过程。
这可以通过VBA或将文件夹中的图片添加到所需单元格的脚本来完成吗?
答案 0 :(得分:0)
看看下面的代码:
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
答案 1 :(得分:0)
谢谢, 我做了一个模板,根据其他单元格值将图像插入到单元格中: https://www.youtube.com/watch?v=qTUWCk7NpXk