"Error '424' Object required" 试图在图像框中显示 "Temp.jpg" (imgRpts1)
"Error '424' Object required" when trying to display "Temp.jpg" in an Image Box (imgRpts1)
Application
我在 Excel Workbook
前面处理 Runs
,因此 User
无法直接访问个人 Worksheets
。主要关注的领域是 Reports Worksheets
,我想 Display
相当于 Worksheet
的 Print Preview
,所有 data
都填充在 Worksheet
。我有 4 个 ComboBoxes configured as DDLs
和一个 Submit Button
实现了选择所需 Worksheet
的功能,Named Ranges
与相应的 Worksheets
相关联,并填充适当的 cells
在那些 Worksheets
上(见下图)。
ALL Variables
for Worksheets
and Ranges
are Declared
as Global Variables
in the WB_Initialization
Module
和 Set
在 UserForm_Initialize()
!
Choose Report Type
ComboBox Change Event
设置 ActiveWorksheet
其余 3 ComboBoxes
控制 values
Worksheet
用来实现适当的选择和显示数据,Submit
填充 Worksheet
上的 Cells
并调用 Function
DisplayRange rngToDisplay
!在实施DisplayRange rngToDisplay
之前,我确认了ALLWorksheets
的正确操作!
Choose Report Type
的Code
如下:
Private Sub cmbPropWksts_Change() 'Located on MultiPage1.Page4
Select Case cmbPropWksts.ListIndex
Case 0
wkstSum.Select
rngRptSum.Select
Set rngToDisplay = rngRptSum
Case 1
wkstDetail.Select
rngRptDetail.Select
Set rngToDisplay = rngRptDetail
Case 2
wkstCmpYrsElec.Select
rngRptCmpYrsElect.Select
Set rngToDisplay = rngRptCmpYrsElect
Case 3
wkstCmpYrsGas.Select
rngRptCmpYrsGas.Select
Set rngToDisplay = rngRptCmpYrsGas
End Select
cmdSubmit.Caption = "Display " & cmbPropWksts.Value
Call MeasureSelection_Pixels
If cmbPropWksts.ListIndex > 1 Then
cmbwkstYrs2.Visible = True
End If
End Sub
Submit Button Code
在这里:
pID = cmbRptPrpID.Text
pIndex = cmbRptPrpID.ListIndex + 2
wsYr1 = cmbWkstYrs1.Text
wsYr2 = cmbwkstYrs2.Text
Select Case cmbPropWksts.ListIndex
Case 0
ActiveSheet.Cells(3, "B") = cmbRptPrpID.Text 'Cell B3 of the Summary Worksheet
ActiveSheet.Cells(3, "Q") = cmbWkstYrs1.Text 'Cell Q3 of the Summary Worksheet
ActiveSheet.Cells(1, "A") = wsCntrl.Cells(pIndex, "N") 'Cell for the Property's address
Case 1
ActiveSheet.Cells(3, "B") = cmbRptPrpID.Text 'Cell B3 of the Summary Worksheet
ActiveSheet.Cells(3, "Q") = cmbWkstYrs1.Text 'Cell Q3 of the Summary Worksheet
ActiveSheet.Cells(1, "A") = wsCntrl.Cells(pIndex, "N") 'Cell for the Property's address
Case 2, 3
ActiveSheet.Cells(2, "B") = cmbRptPrpID.Text 'Cell B3 of the Summary Worksheet
ActiveSheet.Cells(2, "C") = cmbWkstYrs1.Text 'Cell Q3 of the Summary Worksheet
ActiveSheet.Cells(17, "C") = cmbwkstYrs2.Text 'Cell Q17 of the Summary Worksheet
ActiveSheet.Cells(1, "A") = wsCntrl.Cells(pIndex, "N") 'Cell for the Property's address
End Select
DisplayRange rngToDisplay
End Sub
最后,DisplayRange
代码在这里:
Function DisplayRange(r As Range)
Dim wsChart As Worksheet
Dim fname As String
Set wsChart = ThisWorkbook.Worksheets("tmpChart")
fname = ThisWorkbook.Path & "\TempImages\temp.jpg"
r.CopyPicture xlScreen, xlBitmap
With wsChart
Dim chtObj As ChartObject
Set chtObj = .ChartObjects.Add(100, 30, 400, 250)
With chtObj
.Width = r.Width: .Height = r.Height
.Chart.Paste
.Chart.Export Filename:=fname, FilterName:="jpg"
.Delete
End With
DoEvents
End With
imgRpts1.Picture = LoadPicture(fname)
End Function
上面 Function
的 Call
正确地在正确的 Worksheet
上生成 Chart
如下确认:
并在此处创建 temp.jpg
图像:
在适当的 Folder
此处:
Error 424
发生在这里!
我的研究表明 Error
最常见的原因是 Undeclared/Improperly Declared Varables
!我检查了 Varable Scopes
、Image Box Name
和 File Paths
,显然我遗漏了一些东西。
在此先感谢您的帮助!
由于@Ambie 的问题,我重新访问了 Module Code
以获得 Function DisplayRange
。我记得我在尝试将 Code
从 UserForm
移动到 Module
并不断得到 Variable Not Defined Error
时遇到的类似问题。我今天学到了两个宝贵的教训。首先,确保在每个 Module
中使用 Option Explicit
。其次,我学习了如何设置 Inter-Module
和 Module to UserForm
通信。
我通过添加这两行 Code
:
解决了这个问题
Dim imgRpts1 As Object
Set imgRpts1 = UserForm1.imgRpts1
这是最后的 Function Code
:
Option Explicit
Function DisplayRange(r As Range)
Dim wsChart As Worksheet
Dim fname As String
Dim imgRpts1 As Object
Set imgRpts1 = UserForm1.imgRpts1
Set wsChart = ThisWorkbook.Worksheets("tmpChart")
fname = ThisWorkbook.Path & "\TempImages\temp.jpg"
r.CopyPicture xlScreen, xlBitmap
With wsChart
Dim chtObj As ChartObject
Set chtObj = .ChartObjects.Add(100, 30, 400, 250)
With chtObj
.Width = r.Width: .Height = r.Height
.Chart.Paste
.Chart.Export Filename:=fname, FilterName:="jpg"
.Delete
End With
DoEvents
End With
imgRpts1.Picture = LoadPicture(fname)
End Function
成功了!
[![并且有效!][1]][1]
[1]:https://i.stack.imgur.com/3DfCD.jpg
Application
我在 Excel Workbook
前面处理 Runs
,因此 User
无法直接访问个人 Worksheets
。主要关注的领域是 Reports Worksheets
,我想 Display
相当于 Worksheet
的 Print Preview
,所有 data
都填充在 Worksheet
。我有 4 个 ComboBoxes configured as DDLs
和一个 Submit Button
实现了选择所需 Worksheet
的功能,Named Ranges
与相应的 Worksheets
相关联,并填充适当的 cells
在那些 Worksheets
上(见下图)。
Variables
for Worksheets
and Ranges
are Declared
as Global Variables
in the WB_Initialization
Module
和 Set
在 UserForm_Initialize()
!
Choose Report Type
ComboBox Change Event
设置 ActiveWorksheet
其余 3 ComboBoxes
控制 values
Worksheet
用来实现适当的选择和显示数据,Submit
填充 Worksheet
上的 Cells
并调用 Function
DisplayRange rngToDisplay
!在实施DisplayRange rngToDisplay
之前,我确认了ALLWorksheets
的正确操作!
Choose Report Type
的Code
如下:
Private Sub cmbPropWksts_Change() 'Located on MultiPage1.Page4
Select Case cmbPropWksts.ListIndex
Case 0
wkstSum.Select
rngRptSum.Select
Set rngToDisplay = rngRptSum
Case 1
wkstDetail.Select
rngRptDetail.Select
Set rngToDisplay = rngRptDetail
Case 2
wkstCmpYrsElec.Select
rngRptCmpYrsElect.Select
Set rngToDisplay = rngRptCmpYrsElect
Case 3
wkstCmpYrsGas.Select
rngRptCmpYrsGas.Select
Set rngToDisplay = rngRptCmpYrsGas
End Select
cmdSubmit.Caption = "Display " & cmbPropWksts.Value
Call MeasureSelection_Pixels
If cmbPropWksts.ListIndex > 1 Then
cmbwkstYrs2.Visible = True
End If
End Sub
Submit Button Code
在这里:
pID = cmbRptPrpID.Text
pIndex = cmbRptPrpID.ListIndex + 2
wsYr1 = cmbWkstYrs1.Text
wsYr2 = cmbwkstYrs2.Text
Select Case cmbPropWksts.ListIndex
Case 0
ActiveSheet.Cells(3, "B") = cmbRptPrpID.Text 'Cell B3 of the Summary Worksheet
ActiveSheet.Cells(3, "Q") = cmbWkstYrs1.Text 'Cell Q3 of the Summary Worksheet
ActiveSheet.Cells(1, "A") = wsCntrl.Cells(pIndex, "N") 'Cell for the Property's address
Case 1
ActiveSheet.Cells(3, "B") = cmbRptPrpID.Text 'Cell B3 of the Summary Worksheet
ActiveSheet.Cells(3, "Q") = cmbWkstYrs1.Text 'Cell Q3 of the Summary Worksheet
ActiveSheet.Cells(1, "A") = wsCntrl.Cells(pIndex, "N") 'Cell for the Property's address
Case 2, 3
ActiveSheet.Cells(2, "B") = cmbRptPrpID.Text 'Cell B3 of the Summary Worksheet
ActiveSheet.Cells(2, "C") = cmbWkstYrs1.Text 'Cell Q3 of the Summary Worksheet
ActiveSheet.Cells(17, "C") = cmbwkstYrs2.Text 'Cell Q17 of the Summary Worksheet
ActiveSheet.Cells(1, "A") = wsCntrl.Cells(pIndex, "N") 'Cell for the Property's address
End Select
DisplayRange rngToDisplay
End Sub
最后,DisplayRange
代码在这里:
Function DisplayRange(r As Range)
Dim wsChart As Worksheet
Dim fname As String
Set wsChart = ThisWorkbook.Worksheets("tmpChart")
fname = ThisWorkbook.Path & "\TempImages\temp.jpg"
r.CopyPicture xlScreen, xlBitmap
With wsChart
Dim chtObj As ChartObject
Set chtObj = .ChartObjects.Add(100, 30, 400, 250)
With chtObj
.Width = r.Width: .Height = r.Height
.Chart.Paste
.Chart.Export Filename:=fname, FilterName:="jpg"
.Delete
End With
DoEvents
End With
imgRpts1.Picture = LoadPicture(fname)
End Function
上面 Function
的 Call
正确地在正确的 Worksheet
上生成 Chart
如下确认:
temp.jpg
图像:
Folder
此处:
Error 424
发生在这里!
我的研究表明 Error
最常见的原因是 Undeclared/Improperly Declared Varables
!我检查了 Varable Scopes
、Image Box Name
和 File Paths
,显然我遗漏了一些东西。
在此先感谢您的帮助!
由于@Ambie 的问题,我重新访问了 Module Code
以获得 Function DisplayRange
。我记得我在尝试将 Code
从 UserForm
移动到 Module
并不断得到 Variable Not Defined Error
时遇到的类似问题。我今天学到了两个宝贵的教训。首先,确保在每个 Module
中使用 Option Explicit
。其次,我学习了如何设置 Inter-Module
和 Module to UserForm
通信。
我通过添加这两行 Code
:
Dim imgRpts1 As Object
Set imgRpts1 = UserForm1.imgRpts1
这是最后的 Function Code
:
Option Explicit
Function DisplayRange(r As Range)
Dim wsChart As Worksheet
Dim fname As String
Dim imgRpts1 As Object
Set imgRpts1 = UserForm1.imgRpts1
Set wsChart = ThisWorkbook.Worksheets("tmpChart")
fname = ThisWorkbook.Path & "\TempImages\temp.jpg"
r.CopyPicture xlScreen, xlBitmap
With wsChart
Dim chtObj As ChartObject
Set chtObj = .ChartObjects.Add(100, 30, 400, 250)
With chtObj
.Width = r.Width: .Height = r.Height
.Chart.Paste
.Chart.Export Filename:=fname, FilterName:="jpg"
.Delete
End With
DoEvents
End With
imgRpts1.Picture = LoadPicture(fname)
End Function
成功了! [![并且有效!][1]][1] [1]:https://i.stack.imgur.com/3DfCD.jpg