請問我這VBA哪裡有問題?為什麼會無法出現 424 無法找到物件呢?
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 1
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2025-1-28

|
請問我這VBA哪裡有問題?為什麼會無法出現 424 無法找到物件呢?
請問各位前輩先進
如何讓這檔案
能自動跳出多張圖片及對應資料)-英文版-02.rar (341.33 KB)
可以像右邊這檔案開啟圖檔呢?
20180611 VBA 讀取圖片 v.02.zip (43.52 KB)
右邊的檔案 圖片如下:
請問我這VBA哪裡有問題?為什麼會無法出現 424 無法找到物件呢?
Private Sub ComboBox1_Change()
Dim a As Range
Application.ScreenUpdating = False
With Database
Set a = .Columns("E").Find(ComboBox1, lookat:=xlWhole)
Label1.Caption = a.Offset(, 1)
a.Offset(, 1).CopyPicture '複製成圖片 a.Offset(, 1). 1為傳回上1行程式 a = .Columns("E").後1格儲存格的圖片
With .ChartObjects.Add(1, 1, a.Offset(, 2).Width, a.Offset(, 2).Height) '新增圖表
.Chart.Paste '貼上圖片
.Chart.Export "D:\temp.jpg" '匯出圖表,暫存圖片
.Delete '刪除圖表
End With
Image1.Picture = LoadPicture("D:\temp.jpg") '表單顯示圖片
Kill "D:\temp.jpg" '刪除暫存圖片
End With
Application.ScreenUpdating = True
End Sub
Private Sub UserForm_Initialize()
Dim a As Range
Set d = CreateObject("Scripting.Dictionary")
With Database
For Each a In .Range(.[E2], .[E2].End(xlDown))
d(a.Value) = ""
Next
End With
ComboBox1.List = d.keys
End Sub
-------------------------------------------------------------------
謝謝各位 |
|
|
|
|
|
|
- 帖子
- 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
複製代碼 |
|
|
|
|
|
|
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 1
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2025-1-28

|
3#
發表於 2018-6-13 14:11
| 只看該作者
回復 2# GBKEE
謝謝 G 超級版主的回覆,目前發現最後要加 End Function 就可以Run了,其他還在研究中,超級感謝您的幫忙。 |
|
|
|
|
|
|
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 1
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2025-1-28

|
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 |
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
5#
發表於 2018-6-15 09:31
| 只看該作者
回復 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
- 點名
- 1
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2025-1-28

|
6#
發表於 2018-6-15 13:32
| 只看該作者
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
7#
發表於 2018-6-15 16:39
| 只看該作者
回復 6# jeffrey628litw
再試試- Option Explicit '強制 模組的變數必須要 Dim的宣告,會使程式易於偵錯
- Dim Sh(1 To 2) As Worksheet, xTempPicture As String '模組頂端上 Dim的變數 可在UserForm1的全部程式中使用
- Private Sub UserForm_Initialize()
- Dim A As Range, i As Integer
- Set Sh(1) = ThisWorkbook.Sheets("Database") '工作表如有變動時在此修改即可
- Set Sh(2) = ThisWorkbook.Sheets.Add
- 匯入圖片 '置入"沒有圖片"檔
- '不要用字典物件 Set D = CreateObject("Scripting.Dictionary")
- With ComboBox1
- For Each A In Sh(1).Range(Sh(1).[E3], Sh(1).[E3].End(xlDown))
- 'ColumnCount 屬性 若將 ColumnCount 設成 0,顯示的行數便是 0;若設成 -1,便顯示所有的資料行。對一個非資料連結的資料來源而言,最多只能有 10 行 (0 到 9)。
- .ColumnCount = 2 '了解後,可不用此行程式碼?
- .AddItem
- .List(.ListCount - 1, 0) = A
- .List(.ListCount - 1, 1) = A.Offset(, 1).Address
- Next
- End With
- Label1.WordWrap = False '內容在行末是否自動換行
- Label4.WordWrap = False '*******新增Label控制項
-
- 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 = "沒有此圖片"
- Label4.Caption = "沒有此圖片" '*******新增Label控制項
- Image1.Picture = LoadPicture(xTempPicture) '表單預設的圖片
- Image2.Picture = LoadPicture(xTempPicture) '表單預設的圖片
- With ComboBox1
- If .ListIndex = -1 Then Exit Sub
- If 圖片檢查(.List(.ListIndex, 1), Image1) = True Then Label1.Caption = .Value
- If .ListIndex < .ListCount - 1 Then
- If 圖片檢查(.List(.ListIndex + 1, 1), Image2) Then Label4.Caption = .List(.ListIndex + 1, 0)
- '*******新增Label控制項
- End If
- 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 Range
- xTempPicture = "D:\NoPicture.jpg"
- Set P = Sh(1).[f2]
- P.CopyPicture
- With Sh(2).ChartObjects.Add(1, 1, P.Width, P.Height) '新增圖表
- .Chart.Paste '貼上圖片
- .Chart.Export xTempPicture '匯出圖表,暫存圖片
- .Delete '刪除圖表
- End With
- End Sub
複製代碼 |
|
|
|
|
|
|
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 1
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2025-1-28

|
8#
發表於 2018-6-15 18:31
| 只看該作者
回復 7# GBKEE
G大 超級版主您好,我看您已經有將我問題1解決了,另外要程式中說明要新增 Lable4 我也新增了,
問題2和3還沒解決,
接下來,
程式的邏輯是要依據撈出的Lable4 如果結果同Lable1的字串,則撈出圖片,否則撈出Null的圖片嗎?
是這樣的話請問要如何寫呢? 因為我對於VBA實在幾乎完全不了解,能否請您幫忙一點一點解給我看,我慢慢學呢?
謝謝您。
檔案在雲端:http://webhd.xuite.net/_oops/jeffrey628litw/wyh |
|
|
|
|
|
|
- 帖子
- 5923
- 主題
- 13
- 精華
- 1
- 積分
- 5986
- 點名
- 0
- 作業系統
- win10
- 軟體版本
- Office 2010
- 閱讀權限
- 150
- 性別
- 男
- 來自
- 台灣基隆
- 註冊時間
- 2010-5-1
- 最後登錄
- 2022-1-23
        
|
9#
發表於 2018-6-16 06:22
| 只看該作者
本帖最後由 GBKEE 於 2018-6-16 06:26 編輯
回復 8# jeffrey628litw
對於VBA不了解,可多看看論壇的主題及VBA上說明,並多練習,會進步的
不懂可提問.內容讓人看懂就會得到回覆(義務性)
如有不符,請再詳加說明- Option Explicit '強制 模組的變數必須要 Dim的宣告,會使程式易於偵錯
- Dim Sh(1 To 2) As Worksheet, D As Object, xTempPicture As String '模組頂端上 Dim的變數 可在UserForm1的全部程式中使用
- Private Sub UserForm_Initialize()
- Dim A As Range, S As String
- Set Sh(1) = ThisWorkbook.Sheets("Database") '工作表如有變動時在此修改即可
- Set Sh(2) = ThisWorkbook.Sheets.Add
- xTempPicture = "D:\NoPicture.jpg"
- 照片Export Sh(1).Range("F2"), xTempPicture '置入"沒有圖片"檔
- Set D = CreateObject("Scripting.Dictionary")
- For Each A In Sh(1).Range(Sh(1).[E3], Sh(1).[E3].End(xlDown))
- S = Replace(Trim(A), vbLf, Space(1)) '換行字元 改成 Space(1)
- If D.EXISTS(S) Then
- D(S) = D(S) & "," & A.Offset(, 1).Address
- Else
- D(S) = A.Offset(, 1).Address
- End If
- Next
- ComboBox1.List = D.KEYS
- Label1.WordWrap = False '內容在行末是否自動換行
- Label4.WordWrap = False '*******新增Label控制項
- 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 Variant, i As Integer, S As String
- Label1.Caption = "沒有此圖片"
- Label4.Caption = "沒有此圖片" '*******新增Label控制項
- Image1.Picture = LoadPicture(xTempPicture) '表單預設的圖片
- Image2.Picture = LoadPicture(xTempPicture) '表單預設的圖片
- With ComboBox1
- If .ListIndex = -1 Then Exit Sub
- A = Split(D(.Value), ",")
- For i = 0 To 1
- If i <= UBound(A) Then
- S = A(i)
- If 圖片檢查(S, IIf(i = 0, Image1, Image2)) Then Label1.Caption = .Value
- End If
- Next
- Label4.Visible = UBound(A) = 0 '有相同的 Player Name 則不顯示
- If UBound(A) = 0 And .ListIndex < .ListCount - 1 Then
- A = Split(D(.List(.ListIndex + 1)), ",")
- S = A(0)
- If 圖片檢查(S, Image2) Then Label4.Caption = .List(.ListIndex + 1, 0)
- '*******新增Label控制項
- End If
- End With
- End Sub
- Private Function 圖片檢查(xPicture As String, xImage As Image) As Boolean
- Dim S As Shape, 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
- Exit For
- End If
- '***************************************************
- Next
- If 圖片檢查 = True Then
- xName = "D:\temp.jpg"
- 照片Export S, xName
- xImage.Picture = LoadPicture(xName) '表單顯示圖片
- Kill xName ' "D:\temp.jpg" '刪除暫存圖片
- End If
- End Function
- Private Sub 照片Export(P As Object, xName As String)
- If xName <> "D:\temp.jpg" Then
- P.CopyPicture
- Else
- P.Copy
- End If
- With Sh(2).ChartObjects.Add(1, 1, P.Width, P.Height) '新增圖表
- .Chart.Paste '貼上圖片
- .Chart.Export xName '匯出圖表,暫存圖片
- .Delete '刪除圖表
- End With
- End Sub
複製代碼 |
|
|
|
|
|
|
- 帖子
- 228
- 主題
- 62
- 精華
- 0
- 積分
- 364
- 點名
- 1
- 作業系統
- Win 10
- 軟體版本
- Office 2007 & 2010
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2011-3-5
- 最後登錄
- 2025-1-28

|
10#
發表於 2018-6-16 11:22
| 只看該作者
回復 9# GBKEE
G大 版主您好:我看了問題3也解決了,剩下問題2,這邏輯是要寫
當Lable1 And Lable4 不為空時且不相同,則顯示2張圖片到Image1 和Image4,
否則 當Lable1不等於 Label4,則顯示Image1 且 Image4為空
否則 當Label1等於空,Lable4不等於空,則顯示Image1為空 且顯示 Image4的圖片
是這樣子嗎? 是的話程式要怎麼寫呢?再請您賜教,謝謝。
雲端檔案:http://webhd.xuite.net/_oops/jeffrey628litw/gdn |
|
|
|
|
|
|