从工作表获取图片到Excel用户窗体 [英] Get Picture from Worksheet into Excel Userform

查看:53
本文介绍了从工作表获取图片到Excel用户窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在用户窗体的图像"控件中查看工作表中的图像.

I am looking to view an image from the worksheet in an Image control on a userform.

此图像将根据组合框上的值更改.我已经在工作簿的"Sheet1"中插入(使用:Insert-> Pictures)几个图像,并将它们命名为"Picture1"&图片2".

This image will change based on the value on a combobox. I have inserted (Using: Insert -> Pictures) a couple of images into "Sheet1" of my workbook and named them "Picture1" & "Picture2".

我创建了以下用户窗体:

I have created the below UserForm:

表格http://im56.gulfup.com/msKyqi.png

这是我试图用来从工作表加载图像的代码,但不幸的是,此操作目前无法正常工作.

And this is the code that I am trying to use in order to load the images from the sheet, but unfortunately, this is not working at the moment.

Private Sub ComboBox1_Change()

UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))

End Sub

Private Sub UserForm_Initialize()

UserForm1.ComboBox1.Clear
UserForm1.ComboBox1.AddItem "Picture1"
UserForm1.ComboBox1.AddItem "Picture2"

UserForm1.ComboBox1.Value = "Picture1"

UserForm1.Image1.Picture = LoadPicture(Worksheets("Sheet1").Shapes(ComboBox1.Value))

End Sub

每次运行此代码时,都会出现以下错误:

Every time I run this code I get the below error:

错误http://im43.gulfup.com/YoWvTp.png

请告知.

推荐答案

我知道了!

当我使用UserForm时,有一个解决该问题的方法.

As I am using a UserForm there is a workaround to the issue.

这不是如何在工作表中包含图像,然后再尝试以我首先在UserForm中将它们包含在表单中的形式加载它们的.

Instead of having the images in the worksheet to then try and load them in the form I tried having them in the UserForm in the first place, here is how.

在用户表单上创建一个框架:框架http://im88.gulfup.com/Moy8I6.png

Create a frame on your userform: Frame http://im88.gulfup.com/Moy8I6.png

将框架的可见属性设置为" False ":可见http://im88.gulfup.com/sAIQqh.png

Set the visible property of the frame to "False": Visible http://im88.gulfup.com/sAIQqh.png

通过添加图片控件并加载图像来插入图像,您可以根据需要添加任意数量的图像:图片http://im88.gulfup.com/oas0EQ.png

Insert your images by adding a picture control and loading the images, you can add as many images as you need: Images http://im88.gulfup.com/oas0EQ.png

为图像命名:名称http://im88.gulfup.com/cIO317.png

将所有图像彼此拖放到框架中(然后您可以将框架移到角落,这样就不会打扰您了:

Drag all the images one over the other into the frame, (you can then move the frame into a corner so it doesn't bother you:

拖动http://im88.gulfup.com/1fOSut.png 移开http://im88.gulfup.com/Q1fzKd.png

接下来创建一个图片控件,这是您将根据选择显示图片的方法:

Next create a picture control, this is what you will use to display the picture based on a selection:

表单视图http://im88.gulfup.com/X1UVRB.png

在此示例中,我将使用组合框进行选择.现在,将下面的代码插入非常简单的表单中:

In this example, I am going to use a combobox for the selection. Now insert the below code in to the form which is pretty straight forward:

    Private Sub ComboBox1_Change()

    ' Image1 is the name of the created picture control
    UserForm3.Controls.Item("Image1").Picture = UserForm3.Controls.Item(UserForm3.ComboBox1.Value).Picture 

    End Sub

    Private Sub UserForm_Initialize()

    UserForm3.ComboBox1.AddItem "Argentina"
    UserForm3.ComboBox1.AddItem "Brazil"
    UserForm3.ComboBox1.AddItem "Chile"

    End Sub

如您所见,带有图片的框架被隐藏,并且图像根据选择在图片控件内部变化:

As you will see, the frame with the pictures is Hidden, and the image is changing inside the picture control based on a selection:

结果http://im88.gulfup.com/MSqyHF.png

相对于将图像从工作表中导出到Temp文件夹然后再将它们重新加载到图片控件中,我认为这是一种更好的方法.

I think it's the better way to go as opposed to exporting the images from the worksheet to a Temp folder and then loading them back into the picture controls.

这篇关于从工作表获取图片到Excel用户窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆