2015-09-28 91 views
0

我正在使用下面的宏將與單元格P2中的值對應的圖片插入到單元格Q2中。使用excel宏插入對應於單元格值的圖片

這適用於所選的一個單元(本例中爲P2)。

我想要創建一個循環來爲列P範圍(P2:P500)中不是空白的行執行相同的操作。

Sub Picture() 

Range("Q2").Select 
Dim picname As String 

picname = "C:\Users\kisnahr\Pictures\Test\" & Range("P2") & ".bmp" 'Link to the picture 
ActiveSheet.Pictures.Insert(picname).Select 

With Selection 
.Left = Range("Q2").Left 
.Top = Range("Q2").Top 
.ShapeRange.LockAspectRatio = msoFalse 
.ShapeRange.Height = 80# 
.ShapeRange.Width = 80# 
.ShapeRange.Rotation = 0# 
End With 

Range("Q10").Select 
Application.ScreenUpdating = True 

Exit Sub 

ErrNoPhoto: 
MsgBox "Unable to Find Photo" 'Shows message box if picture not found 
Exit Sub 
Range("P20").Select 

End Sub 

回答

0

嘗試沿着這些線。這是一個非常粗糙和現成的解決方案,因此您需要根據自己的需求進行調整。在這裏,我將圖像路徑放在B列中,並從CommandButton4點擊開始。不知道你如何定義你的單元格左和頂部雖然?

Private Sub CommandButton4_Click() 
Dim MyRange As String 
Dim picname As String 
Dim mySelectRange As String 
Dim rcell As Range 
Dim IntInstr As Integer 
Dim Mypath As String 

Mypath = "z:\My Pictures" 
MyRange = "B2:B500" 

Range(MyRange).Select 
For Each rcell In Selection.Cells 
    If Len(rcell.value) > 0 Then 
     picname = Mypath & rcell.value 
     mySelectRange = Replace(MyRange, "B", "A") 
     IntInstr = InStr(mySelectRange, ":") 
     mySelectRange = Left(mySelectRange, IntInstr - 1) 
     do_insertPic picname, mySelectRange, rcell.Left, rcell.Top 
    End If 
Next 
Application.ScreenUpdating = True 
End Sub 

Private Sub do_insertPic(ByRef picname As String, ByRef MyRange As String, myleft As Integer, mytop As Integer) 
    Dim rcell As Range 
    Range(MyRange).Select 
    On Error GoTo ErrNoPhoto 

    ActiveSheet.Pictures.Insert(picname).Select 
    On Error GoTo 0 

    With Selection 
    .Left = myleft 
    .Top = mytop 
    .ShapeRange.LockAspectRatio = msoFalse 
    .ShapeRange.Height = 80# 
    .ShapeRange.Width = 80# 
    .ShapeRange.Rotation = 0# 
    End With 
Exit Sub 
ErrNoPhoto: 
MsgBox "Unable to Find Photo" 'Shows message box if picture not found 
End Sub 
+0

米格爾你好,非常感謝你的幫助!這很好,我可以循環訪問輸入範圍,並從我的本地驅動器插入圖片。但是,圖片插入與我的輸入範圍相同的列中,我無法將其更改爲行中的下一個單元格。例如,如果MyRange是「B2:B500」,則將相應的圖片插入相同的單元格中。 – kisnah

+0

如果你想把圖片放在文件名的左邊,那麼用「數字1」替換「do_insertPic」調用中的「rcell.Left」。如果你想把它放在右邊,那麼使用200-250左右的值。 – MiguelH

+0

謝謝!你能指導我如何使用單元ID在任何指定的單元格中添加圖片 - 例如,如果我想將單元格B2中的圖片名稱添加到單元格T2中,並將單元格B3中的圖片名稱添加到單元格T3中。 – kisnah

0

我使用後,使紙張可以郵寄等:

Picname in Column B7 and corresponding picture in Column M7 

Sub Picture() 
Dim picname As String 
Dim shp As Shape 
Dim pasteAt As Integer 
Dim lThisRow As Long 

lThisRow = 7 'This is the start row 

Do While (Cells(lThisRow, 2) <> "") 


    pasteAt = lThisRow 
    Cells(pasteAt, 13).Select 'This is where picture will be inserted (column) 


    picname = Cells(lThisRow, 2) 'This is the picture name 

    present = Dir("C:\foto\" & picname & ".jpg") 

    If present <> "" Then 

      Cells(pasteAt, 13).Select 

      Call ActiveSheet.Shapes.AddPicture("C:\foto\" & picname & ".jpg", _ 
      msoCTrue, msoCTrue, Left:=Cells(pasteAt, 13).Left, Top:=Cells(pasteAt, 13).Top, Width:=100, Height:=100).Select 


    Else 
      Cells(pasteAt, 14) = "No Picture Found" 
    End If 

     lThisRow = lThisRow + 1 
Loop 

Range("A1").Select 
Application.ScreenUpdating = True 

Exit Sub 

ErrNoPhoto: 
    MsgBox "Unable to Find Photo" 'Shows message box if picture not found 
    Exit Sub 
    Range("O7").Select 

End Sub 
相關問題