首頁 > 易卦

vba自動插入圖片(含程式碼)

作者:由 YY表格省時秘籍 發表于 易卦日期:2023-01-27

一串程式碼怎麼弄成圖片

首先新建一個資料夾將表格和圖片放置在同一表格下面

vba自動插入圖片(含程式碼)

圖片格式為jpg,圖片名稱要與表格內名稱相同

vba自動插入圖片(含程式碼)

vba自動插入圖片(含程式碼)

在表格內新建模組中輸入一下程式:

Sub 插入圖片()

Application。ScreenUpdating = False

Dim i As Integer

Dim filpath As String

Dim rng As Range

Dim s As String

Dim picTemp As Picture

Dim x As Shape

For Each x In ActiveSheet。Shapes

x。Delete

Next

With Sheet1

For i = 2 To 。Cells(Rows。Count, 2)。End(xlUp)。Row

filpath = ThisWorkbook。Path & “\測試圖片\” & 。Cells(i, 2)。Text & “。jpg”

If Dir(filpath) <> “” Then

Set rng = 。Cells(i, 3)

Set picTemp = 。Pictures。Insert(filpath)

picTemp。Placement = xlMoveAndSize

With picTemp

。ShapeRange。LockAspectRatio = msoFalse

。Top = rng。Top

。Left = rng。Left

。Height = rng。Height

。Width = rng。Width

End With

Else

s = s & Chr(10) & 。Cells(i, 2)。Text

End If

Next

End With

If s <> “” Then

MsgBox s & Chr(10) & “沒有照片”

End If

Application。ScreenUpdating = True

End Sub

vba自動插入圖片(含程式碼)

鍵盤上按F5即可執行程式,插入的圖會隨表格變化而變化

同時在本號中也有具體操做的影片,作本篇文章主要是為了放置程式碼方便大家複製貼上,如果此篇文章或影片對您有所幫助請給點贊和關注喲~