返回列表 上一主題 發帖

[發問] 請問該如何變更工作表中的圖片索引值?

回復 1# baconbacons
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim Sh As Worksheet, AR(), P(), i As Integer, X As Integer
  4.     Dim M As Single, M1 As Single, MyPicture As String
  5.     Set Sh = Sheet1
  6.     X = 1
  7.     For i = 1 To Sh.Shapes.Count
  8.         If Sh.Shapes(i).Type = msoPicture Then '表工作表上的圖案 是圖片
  9.             ReDim Preserve AR(1 To X)
  10.             ReDim Preserve P(1 To X)
  11.             AR(X) = Sh.Shapes(i).Top  '紀錄從圖案範圍中最頂端圖案的頂端到工作表頂端的距離
  12.             P(X) = i                  '紀錄表工作表上圖案的索引位置
  13.             X = X + 1
  14.         End If
  15.     Next
  16.     '******** 選取圖片 ********
  17.     With Application.FileDialog(msoFileDialogOpen)
  18.         .Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
  19.         .FilterIndex = 1
  20.         If .Show = True Then
  21.             MyPicture = .SelectedItems(1)
  22.         Else
  23.             MsgBox "沒有選擇圖片!!!"
  24.             Exit Sub
  25.         End If
  26.     End With
  27.    
  28.     If X = 1 Then
  29.        If MsgBox("新增圖片 :" & MyPicture, vbYesNo, Sh.Name) = vbNo Then Exit Sub
  30.        i = 0
  31.     Else
  32.         On Error Resume Next
  33.         i = InputBox("新增圖片 :" & MyPicture & " 為第幾張圖片", Sh.Name & " 圖片共 " & X - 1 & " 張")
  34.         If Err <> 0 Then Exit Sub
  35.         If i = 0 Or i > X Then
  36.             MsgBox "第" & i & "張 不在範圍中"
  37.             Exit Sub
  38.         End If
  39.         '*******移動圖片位置
  40.         For X = UBound(AR) To i Step -1  '圖片檔由下往上下移位置
  41.             M = Application.Small(AR, X)
  42.             M = Application.Match(M, AR, 0)
  43.             With Sh.Shapes(P(M))
  44.                 If UBound(AR) = 1 And X = UBound(AR) Then
  45.                     M1 = .Height + [A1].Height
  46.                 ElseIf X > 1 Then
  47.                     M1 = Application.Small(AR, X - 1)
  48.                     M1 = .Top + (.Top - Application.Small(AR, X - 1))
  49.                 ElseIf X = i Then
  50.                     M1 = Application.Small(AR, X + 1)
  51.                 End If
  52.                 .Top = M1
  53.             End With
  54.         Next
  55.     End If
  56.     With Sh.Pictures.Insert(MyPicture)
  57.         If i = 0 Then
  58.             .Top = 0
  59.             .Left = 0
  60.         ElseIf i >= 1 And i <= UBound(AR) Then
  61.             .Top = Application.Small(AR, i)
  62.             .Left = Sh.Shapes(P(1)).Left
  63.         ElseIf i > UBound(AR) Then
  64.             M = Application.Small(AR, UBound(AR))
  65.             M = Application.Match(M, AR, 0)
  66.             .Top = Sh.Shapes(P(M)).Top + Sh.Shapes(P(M)).Height + [A1].Height
  67.             .Left = Sh.Shapes(P(M)).Left
  68.         End If
  69.         .Height = 200
  70.         .Width = 200
  71.     End With
  72. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 時時好心就是時時好日。
返回列表 上一主題