返回列表 上一主題 發帖

請問我這VBA哪裡有問題?為什麼會無法出現 424 無法找到物件呢?

回復 4# jeffrey628litw
試試看
  1. Option Explicit                  '強制 模組的變數必須要 Dim的宣告,會使程式易於偵錯
  2. Dim D As Object, Sh(1 To 2) As Worksheet '模組頂端上 Dim的變數 可在UserForm1的全部程式中使用
  3. Dim xTempPicture As String
  4. Private Sub UserForm_Initialize()
  5.     Dim A As Range, S As String
  6.     Set D = CreateObject("Scripting.Dictionary")
  7.     Set Sh(1) = ThisWorkbook.Sheets("Database") '工作表如有變動時在此修改即可
  8.     Set Sh(2) = ThisWorkbook.Sheets.Add
  9.     xTempPicture = "D:\IE.jpg"
  10.     匯入圖片   '載入表單畫面圖片
  11.     With Sh(1)
  12.         For Each A In .Range(.[E2], .[E2].End(xlDown))
  13.             '**************************
  14.             'F10 ,F11 有換行字元 需修改
  15.             '大 谷翔平
  16.             'Shohei Ohtan
  17.             '***************************
  18.             S = Replace(Trim(A), vbLf, Space(1)) '換行字元 改成 Space(1)
  19.             Set D(S) = Range(A.Offset(, 1).Address)
  20.             'Debug.Print S, D(S).Address  '指令:檢視->及時視窗可看看
  21.         Next
  22.     End With
  23.     ComboBox1.List = D.KEYS
  24.     Label1.WordWrap = False   '內容在行末是否自動換行
  25.     With Image1               '設定圖片的顯示設模式
  26.         .Picture = LoadPicture(xTempPicture)
  27.         .PictureAlignment = fmPictureAlignmentCenter '2
  28.         .PictureSizeMode = fmPictureSizeModeClip     '0
  29.     End With
  30.     With Image2               '設定圖片的顯示設模式
  31.         .Picture = LoadPicture(xTempPicture)
  32.         .PictureAlignment = fmPictureAlignmentCenter '2
  33.         .PictureSizeMode = fmPictureSizeModeClip     '0
  34.     End With
  35. End Sub
  36. Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  37.     Application.DisplayAlerts = False
  38.     Sh(2).Delete
  39.     Kill xTempPicture
  40.     Application.DisplayAlerts = True
  41. End Sub
  42. Private Sub ComboBox1_Change()
  43.     Dim A As Range
  44.     Label1.Caption = "沒有此圖片"
  45.     Image1.Picture = LoadPicture(xTempPicture) '表單預設的圖片
  46.     Image2.Picture = LoadPicture(xTempPicture) '表單預設的圖片
  47.     'Image1.Visible = False      '或是隱藏
  48.     With ComboBox1
  49.         If .ListIndex = -1 Then Exit Sub
  50.         圖片檢查 D(.List(.ListIndex)).Address, Image1
  51.         If 圖片檢查(D(.Value).Address, Image1) Then Label1.Caption = ComboBox1
  52.         If .ListIndex < .ListCount - 1 Then 圖片檢查 D(.List(.ListIndex + 1)).Address, Image2
  53.     End With
  54. End Sub
  55. Private Function 圖片檢查(xPicture As String, xImage As Image) As Boolean
  56.     Dim S As Shape, P As Object, xName As String
  57.     For Each S In Sh(1).Shapes
  58.         '*************************************************
  59.         'Shape物件是照片且位置是D(ComboBox1.Value).Address)
  60.         If S.Type = msoPicture And S.TopLeftCell.Address = xPicture Then
  61.             圖片檢查 = True
  62.             Set P = S '.Copy '圖片複製
  63.             Exit For
  64.         End If
  65.         '***************************************************
  66.     Next
  67.     If 圖片檢查 = True Then
  68.         xName = "D:\temp.jpg"
  69.         照片Export P, xName
  70.         xImage.Picture = LoadPicture(xName) '表單顯示圖片
  71.         Kill xName ' "D:\temp.jpg" '刪除暫存圖片
  72.     End If
  73.     End Function
  74. Private Sub 照片Export(P As Object, xName As String)
  75.     P.Copy
  76.     With Sh(2).ChartObjects.Add(1, 1, P.Width, P.Height) '新增圖表
  77.         .Chart.Paste '貼上圖片
  78.         .Chart.Export xName '匯出圖表,暫存圖片
  79.         .Delete '刪除圖表
  80.      End With
  81. End Sub
  82. Sub 匯入圖片()
  83.     Dim P As Picture
  84.     With Sh(2)
  85.         Set P = .Pictures.Insert("http://forum.twbts.com/templates/discuz6/images/logotop.png") '(工作表上插入照片)
  86.         With [a1]                           '指定的儲存格
  87.             P.Top = .Top                    '照片的右方在工作表上的位置
  88.             P.Left = .Left                  '照片的右方在工作表上的位置
  89.             .RowHeight = IIf(P.Height >= 409, 409, P.Height)        '調整儲存格高度=>照片的高度
  90.             P.Height = IIf(P.Height >= 409, 409, P.Height)          '調整儲存格高度=>照片的高度
  91.             If .Width < P.Width * (.ColumnWidth / .Width) Then   '下載照片的最大寬度
  92.              .Width = P.Width * (.ColumnWidth / .Width)
  93.             .ColumnWidth = P.Width * (.ColumnWidth / .Width)    '調整儲存格欄寬=>照片的寬度
  94.             End If
  95.        End With
  96.      End With
  97.     照片Export P, xTempPicture
  98. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

回復  jeffrey628litw

試試看
GBKEE 發表於 2018-6-13 08:26



    請問我想要按Display Photo按鈕後,
    下拉選單 Player Name
     選Mchael  Jordan 後可以出現
      Image1:為F2圖案
       Image2為F3圖案

       類似EBAY 篩選完有陣列圖表,類似下圖



檔案在雲端  http://webhd.xuite.net/_oops/jeffrey628litw/c6x

TOP

回復 2# GBKEE


    謝謝 G 超級版主的回覆,目前發現最後要加 End Function 就可以Run了,其他還在研究中,超級感謝您的幫忙。

TOP

本帖最後由 GBKEE 於 2018-6-13 08:27 編輯

回復 1# jeffrey628litw

試試看
  1. Option Explicit                  '強制 模組的變數必須要 Dim的宣告,會使程式易於偵錯
  2. Dim D As Object, Sh As Worksheet '模組頂端上 Dim的變數 可在UserForm1的全部程式中使用
  3. Private Sub UserForm_Initialize()
  4.     Dim A As Range, S As String
  5.     Set D = CreateObject("Scripting.Dictionary")
  6.     Set Sh = Sheets("Database") '工作表如有變動時在此修改即可
  7.     'With Sheets("Database") ' 或是 With Sheet1
  8.     With Sh
  9.         For Each A In .Range(.[E2], .[E2].End(xlDown))
  10.             '**************************
  11.             'F10 ,F11 有換行字元 需修改
  12.             '大 谷翔平
  13.             'Shohei Ohtan
  14.             '***************************
  15.             S = Replace(Trim(A), vbLf, Space(1))  '換行字元 改成 Space(1)
  16.             Set D(S) = Range(A.Offset(, 1).Address)
  17.             'Debug.Print S, D(S).Address  '指令:檢視->及時視窗可看看
  18.         Next
  19.     End With
  20.     ComboBox1.List = D.keys
  21.     Label1.WordWrap = False   '內容在行末是否自動換行
  22.     With Image1               '設定圖片的顯示設模式
  23.         .PictureAlignment = fmPictureAlignmentCenter '2
  24.         .PictureSizeMode = fmPictureSizeModeClip     '0
  25.     End With
  26. End Sub
  27. Private Sub ComboBox1_Change()
  28.     Dim A As Range
  29.     Label1.Caption = "沒有此圖片"
  30.     Image1.Picture = LoadPicture("") '不顯示圖片
  31.     'Image1.Visible = False      '或是隱藏
  32.     If ComboBox1.ListIndex = -1 Then Exit Sub
  33.     If 圖片檢查(D(ComboBox1.Value).Address) = False Then Exit Sub
  34.     'Image1.Visible = True      '顯示
  35. End Sub
  36. Private Function 圖片檢查(xPicture As String) As Boolean
  37.     Dim S As Shape, P As Object, xTop As Double
  38.     For Each S In Sh.Shapes
  39.         '*************************************************
  40.         'Shape物件是照片且位置是D(ComboBox1.Value).Address)
  41.         If S.Type = msoPicture And S.TopLeftCell.Address = xPicture Then
  42.             圖片檢查 = True
  43.             S.Copy  '圖片複製
  44.             Set P = S
  45.             Exit For
  46.         End If
  47.         '***************************************************
  48.     Next
  49.     If 圖片檢查 = True Then
  50.         With Sh
  51.             Label1.Caption = ComboBox1
  52.             With .ChartObjects.Add(1, 1, P.Width, P.Height) '新增圖表
  53.                 .Chart.Paste '貼上圖片
  54.                 .Chart.Export "D:\temp.jpg" '匯出圖表,暫存圖片
  55.                 .Delete '刪除圖表
  56.             End With
  57.             Image1.Picture = LoadPicture("D:\temp.jpg") '表單顯示圖片
  58.             Kill "D:\temp.jpg" '刪除暫存圖片
  59.         End With
  60.     End If
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 不要小看自己,因為人有無限的可能。
返回列表 上一主題