請問我這VBA哪裡有問題?為什麼會無法出現 424 無法找到物件呢?
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
回復 4# jeffrey628litw
試試看- Option Explicit '強制 模組的變數必須要 Dim的宣告,會使程式易於偵錯
- Dim D As Object, Sh(1 To 2) As Worksheet '模組頂端上 Dim的變數 可在UserForm1的全部程式中使用
- Dim xTempPicture As String
- Private Sub UserForm_Initialize()
- Dim A As Range, S As String
- Set D = CreateObject("Scripting.Dictionary")
- Set Sh(1) = ThisWorkbook.Sheets("Database") '工作表如有變動時在此修改即可
- Set Sh(2) = ThisWorkbook.Sheets.Add
- xTempPicture = "D:\IE.jpg"
- 匯入圖片 '載入表單畫面圖片
- With Sh(1)
- For Each A In .Range(.[E2], .[E2].End(xlDown))
- '**************************
- 'F10 ,F11 有換行字元 需修改
- '大 谷翔平
- 'Shohei Ohtan
- '***************************
- S = Replace(Trim(A), vbLf, Space(1)) '換行字元 改成 Space(1)
- Set D(S) = Range(A.Offset(, 1).Address)
- 'Debug.Print S, D(S).Address '指令:檢視->及時視窗可看看
- Next
- End With
- ComboBox1.List = D.KEYS
- Label1.WordWrap = False '內容在行末是否自動換行
- With Image1 '設定圖片的顯示設模式
- .Picture = LoadPicture(xTempPicture)
- .PictureAlignment = fmPictureAlignmentCenter '2
- .PictureSizeMode = fmPictureSizeModeClip '0
- End With
- With Image2 '設定圖片的顯示設模式
- .Picture = LoadPicture(xTempPicture)
- .PictureAlignment = fmPictureAlignmentCenter '2
- .PictureSizeMode = fmPictureSizeModeClip '0
- End With
- End Sub
- Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
- Application.DisplayAlerts = False
- Sh(2).Delete
- Kill xTempPicture
- Application.DisplayAlerts = True
- End Sub
- Private Sub ComboBox1_Change()
- Dim A As Range
- Label1.Caption = "沒有此圖片"
- Image1.Picture = LoadPicture(xTempPicture) '表單預設的圖片
- Image2.Picture = LoadPicture(xTempPicture) '表單預設的圖片
- 'Image1.Visible = False '或是隱藏
- With ComboBox1
- If .ListIndex = -1 Then Exit Sub
- 圖片檢查 D(.List(.ListIndex)).Address, Image1
- If 圖片檢查(D(.Value).Address, Image1) Then Label1.Caption = ComboBox1
- If .ListIndex < .ListCount - 1 Then 圖片檢查 D(.List(.ListIndex + 1)).Address, Image2
- End With
- End Sub
- Private Function 圖片檢查(xPicture As String, xImage As Image) As Boolean
- Dim S As Shape, P As Object, xName As String
- For Each S In Sh(1).Shapes
- '*************************************************
- 'Shape物件是照片且位置是D(ComboBox1.Value).Address)
- If S.Type = msoPicture And S.TopLeftCell.Address = xPicture Then
- 圖片檢查 = True
- Set P = S '.Copy '圖片複製
- Exit For
- End If
- '***************************************************
- Next
- If 圖片檢查 = True Then
- xName = "D:\temp.jpg"
- 照片Export P, xName
- xImage.Picture = LoadPicture(xName) '表單顯示圖片
- Kill xName ' "D:\temp.jpg" '刪除暫存圖片
- End If
- End Function
- Private Sub 照片Export(P As Object, xName As String)
- P.Copy
- With Sh(2).ChartObjects.Add(1, 1, P.Width, P.Height) '新增圖表
- .Chart.Paste '貼上圖片
- .Chart.Export xName '匯出圖表,暫存圖片
- .Delete '刪除圖表
- End With
- End Sub
- Sub 匯入圖片()
- Dim P As Picture
- With Sh(2)
- Set P = .Pictures.Insert("http://forum.twbts.com/templates/discuz6/images/logotop.png") '(工作表上插入照片)
- With [a1] '指定的儲存格
- P.Top = .Top '照片的右方在工作表上的位置
- P.Left = .Left '照片的右方在工作表上的位置
- .RowHeight = IIf(P.Height >= 409, 409, P.Height) '調整儲存格高度=>照片的高度
- P.Height = IIf(P.Height >= 409, 409, P.Height) '調整儲存格高度=>照片的高度
- If .Width < P.Width * (.ColumnWidth / .Width) Then '下載照片的最大寬度
- .Width = P.Width * (.ColumnWidth / .Width)
- .ColumnWidth = P.Width * (.ColumnWidth / .Width) '調整儲存格欄寬=>照片的寬度
- End If
- End With
- End With
- 照片Export P, xTempPicture
- End Sub
複製代碼 |
|
|
|
|
|
|
|
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 0
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2026-10-17

|
4#
發表於 2018-6-13 22:26
| 只看該作者
回復 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 |
|
|
|
|
|
|
|
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 0
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2026-10-17

|
3#
發表於 2018-6-13 14:11
| 只看該作者
回復 2# GBKEE
謝謝 G 超級版主的回覆,目前發現最後要加 End Function 就可以Run了,其他還在研究中,超級感謝您的幫忙。 |
|
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
2#
發表於 2018-6-13 08:26
| 只看該作者
本帖最後由 GBKEE 於 2018-6-13 08:27 編輯
回復 1# jeffrey628litw
試試看- Option Explicit '強制 模組的變數必須要 Dim的宣告,會使程式易於偵錯
- Dim D As Object, Sh As Worksheet '模組頂端上 Dim的變數 可在UserForm1的全部程式中使用
- Private Sub UserForm_Initialize()
- Dim A As Range, S As String
- Set D = CreateObject("Scripting.Dictionary")
- Set Sh = Sheets("Database") '工作表如有變動時在此修改即可
- 'With Sheets("Database") ' 或是 With Sheet1
- With Sh
- For Each A In .Range(.[E2], .[E2].End(xlDown))
- '**************************
- 'F10 ,F11 有換行字元 需修改
- '大 谷翔平
- 'Shohei Ohtan
- '***************************
- S = Replace(Trim(A), vbLf, Space(1)) '換行字元 改成 Space(1)
- Set D(S) = Range(A.Offset(, 1).Address)
- 'Debug.Print S, D(S).Address '指令:檢視->及時視窗可看看
- Next
- End With
- ComboBox1.List = D.keys
- Label1.WordWrap = False '內容在行末是否自動換行
- With Image1 '設定圖片的顯示設模式
- .PictureAlignment = fmPictureAlignmentCenter '2
- .PictureSizeMode = fmPictureSizeModeClip '0
- End With
- End Sub
- Private Sub ComboBox1_Change()
- Dim A As Range
- Label1.Caption = "沒有此圖片"
- Image1.Picture = LoadPicture("") '不顯示圖片
- 'Image1.Visible = False '或是隱藏
- If ComboBox1.ListIndex = -1 Then Exit Sub
- If 圖片檢查(D(ComboBox1.Value).Address) = False Then Exit Sub
- 'Image1.Visible = True '顯示
- End Sub
- Private Function 圖片檢查(xPicture As String) As Boolean
- Dim S As Shape, P As Object, xTop As Double
- For Each S In Sh.Shapes
- '*************************************************
- 'Shape物件是照片且位置是D(ComboBox1.Value).Address)
- If S.Type = msoPicture And S.TopLeftCell.Address = xPicture Then
- 圖片檢查 = True
- S.Copy '圖片複製
- Set P = S
- Exit For
- End If
- '***************************************************
- Next
- If 圖片檢查 = True Then
- With Sh
- Label1.Caption = ComboBox1
- With .ChartObjects.Add(1, 1, P.Width, P.Height) '新增圖表
- .Chart.Paste '貼上圖片
- .Chart.Export "D:\temp.jpg" '匯出圖表,暫存圖片
- .Delete '刪除圖表
- End With
- Image1.Picture = LoadPicture("D:\temp.jpg") '表單顯示圖片
- Kill "D:\temp.jpg" '刪除暫存圖片
- End With
- End If
複製代碼 |
|
|
|
|
|
|
|