Note: The other languages of the website are Google-translated. Back to English

如何在Excel中将图像从工作表复制到用户窗体?

本文将向您展示将图像从活动工作表复制到Excel中的用户窗体的方法。

通过复制和粘贴将图像从工作表复制到用户窗体
使用VBA代码将图像从工作表复制到用户窗体


通过复制和粘贴将图像从工作表复制到用户窗体

请执行以下操作,将图像从工作表复制到用户窗体。

1.切换到工作表,其中包含您需要复制到用户窗体的图像,然后按 其他 + F11 钥匙一起打开 Microsoft Visual Basic应用程序 窗口。

2。 在里面 Microsoft Visual Basic应用程序 窗口,请点击 插页 > 用户表格。 看截图:

3.现在,请在用户窗体中插入一个图像控件。 请点击 图片 按钮在 工具箱 对话框,然后将图像控件绘制到 用户表格 手动。 看截图:

4.右键单击插入的Image控件,然后单击“确定”。 租房 从上下文菜单中,如下所示的屏幕截图:

5.然后您可以看到一个 租房 窗格显示在 Microsoft Visual Basic应用程序 窗口。

6.返回工作表,然后选择要在用户窗体中显示的图片,然后按来复制图片 按Ctrl + C 键。

7.进入 Microsoft Visual Basic应用程序 再次向下滚动以单击 图片 字段,然后将复制的图片名称粘贴到“字母”选项卡下。

现在,工作表中的图片已复制到用户窗体中。


使用VBA代码将图像从工作表复制到用户窗体

本节将向您显示一种解决方法,将多张图片从工作表复制到一个用户窗体,然后通过从“组合”框中选择图片名称,在用户窗体中显示相应的图片。 请执行以下操作。

1.请按照上述步骤1和2进行操作,或者在工作表中插入一个用户表单。

2.然后插入一个 框架控制 进入UserFrom,如下图所示:

3.右键单击插入的框架,然后选择 租房 从上下文菜单中。 在左边 租房 面板,请选择 来自 可见 领域。 看截图:

4.如下图所示,将多个Image控件插入Frame中。

5.返回工作表并选择要在用户窗体中显示的图片,然后按 按Ctrl + C 键复制它。

6.进入 Microsoft Visual Basic应用程序 窗口,右键单击第一个图像控件,然后选择 租房 从右键单击菜单中。 请参阅显示的屏幕截图。

7。 在里面 租房 窗格,请单击 图片 字段,然后按 按Ctrl + V 键粘贴您在步骤5中复制的图片,然后选择 1 – fmPictureSizeModeStrtch 来自 图片大小模式 下拉列表如下图所示。

8.重复步骤5-7,将其他图片从工作表粘贴到用户窗体的左侧“图像”控件中。 结果将显示如下所示的屏幕截图。

9.移动 控件移到用户窗体的角,然后插入一个新的 图片 控制和 组合框 控制进入UserForm。 看截图:

10.右键单击 用户表格,然后单击 查看代码。 然后复制并粘贴下面的VBA代码,以将原始代码替换到“代码”窗口中。

VBA代码1:将图像从工作表复制到用户窗体

Private Sub ComboBox1_Change()
    UserForm1.Controls.Item("Image4").Picture = UserForm1.Controls.Item(UserForm1.ComboBox1.Value).Picture
End Sub
Private Sub UserForm_Initialize()
    Dim xImg As Control
    On Error Resume Next
    For Each xImg In UserForm1.Controls
        If TypeName(xImg) = "Image" And xImg.Name <> "Image4" Then
            UserForm1.ComboBox1.AddItem xImg.Name
        End If
    Next
End Sub

Note: In the code, UserForm1 is the name of the inserted UserForm, Image4 is the name of the Image control you will display pictures inside. And Image represents all Image controls which contain the pictures you copied from the worksheet. ComboBox1 is the combobox name you have inserted into the userform. Please change them to your own.

11. Close the Microsoft Visual Basic for Applications window.

12. Insert a Command button into the worksheet by clicking Developer > Insert > Command Button (ActiveX Control).

13. Right-click the command button and select View Code from the Context menu. And then copy and paste the below VBA code into the Code window. And finally close the window.

VBA code2: Copy image from worksheet to Userform

Private Sub CommandButton1_Click()
UserForm1.Show
End Sub

15. Click Developer > Design Mode to turn off the Design Mode.

From now on, when clicking the command button, the UserForm will pop up. You can display picture by selecting the image name from the combo box. See screenshot:

Select different names from the combo box will display different pictures in the UserForm.


Related Articles:


The Best Office Productivity Tools

Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office / Excel 2007-2021 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Comments (1)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
I'm having difficulties, can someone explain the code for me?
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations

Follow Us

Copyright © 2009 - www.extendoffice.com. | All rights reserved. Powered by ExtendOffice. | Sitemap
Microsoft and the Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.
Protected by Sectigo SSL