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

如何在Excel中將圖像從工作表複製到用戶窗體?

本文將向您展示將圖像從活動工作表複製到Excel中的用戶窗體的方法。

通過複製和粘貼將圖像從工作表複製到用戶窗體
使用VBA代碼將圖像從工作表複製到用戶窗體


通過複製和粘貼將圖像從工作表複製到用戶窗體

請執行以下操作,將圖像從工作表複製到用戶窗體。

1.切換到工作表,其中包含您需要復製到用戶窗體的圖像,然後按 其他 + F11 鑰匙一起打開 Microsoft Visual Basic for Applications 窗口。

2。 在裡面 Microsoft Visual Basic for Applications 窗口,請點擊 插入 > 用戶表格。 看截圖:

3.現在,請在用戶窗體中插入一個圖像控件。 請點擊 圖片 按鈕在 工具箱 對話框,然後將圖像控件繪製到 用戶窗體 手動。 看截圖:

4.右鍵單擊插入的Image控件,然後單擊“確定”。 氟化鈉性能 從上下文菜單中,如下所示的屏幕截圖:

5.然後您可以看到一個 氟化鈉性能 窗格顯示在 Microsoft Visual Basic for Applications 窗口。

6.返回工作表,然後選擇要在用戶窗體中顯示的圖片,然後按來複製圖片 按Ctrl + C 鍵。

7.進入 Microsoft Visual Basic for Applications 再次向下滾動以單擊 圖片 字段,然後將復制的圖片名稱粘貼到“字母”選項卡下。

現在,工作表中的圖片已復製到用戶窗體中。


使用VBA代碼將圖像從工作表複製到用戶窗體

本節將向您顯示一種解決方法,將多張圖片從工作表複製到一個用戶窗體,然後通過從“組合”框中選擇圖片名稱,在用戶窗體中顯示相應的圖片。 請執行以下操作。

1.請按照上述步驟1和2進行操作,或者在工作表中插入一個用戶表單。

2.然後插入一個 框架控制 進入UserFrom,如下所示的屏幕截圖:

3.右鍵單擊插入的框架,然後選擇 氟化鈉性能 從上下文菜單中。 在左邊 氟化鈉性能 面板,請選擇 來自 可見 領域。 看截圖:

4.如圖所示,將多個Image控件插入Frame中。

5.返回工作表,然後選擇要在用戶窗體中顯示的圖片,然後按 按Ctrl + C 鍵複製它。

6.進入 Microsoft Visual Basic for Applications 窗口,右鍵單擊第一個圖像控件,然後選擇 氟化鈉性能 從右鍵單擊菜單中。 請參閱顯示的屏幕截圖。

7。 在裡面 氟化鈉性能 窗格,請單擊 圖片 字段,然後按 按Ctrl + V 鍵粘貼您在步驟5中復制的圖片,然後選擇 1 – fmPictureSizeModeStrtch 來自 圖片大小模式 下拉列表如下圖所示。

8.重複步驟5-7,將其他圖片從工作表粘貼到用戶窗體的左側“圖像”控件中。 結果將顯示如下所示的屏幕截圖。

9.移動 框架 控件放到用戶窗體的一角,然後插入一個新的 圖片 控制和 組合框 控制進入用戶窗體。 看截圖:

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