{"id":23566,"date":"2026-02-25T16:00:32","date_gmt":"2026-02-25T08:00:32","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=23566"},"modified":"2026-02-26T08:05:00","modified_gmt":"2026-02-26T00:05:00","slug":"excel-%e4%b8%ad%e7%94%a8-vba-%e5%ae%9e%e7%8e%b0%e9%9a%8f%e6%9c%ba%e6%8f%92%e5%85%a5%e5%9b%be%e7%89%87%e5%88%b0%e6%8c%87%e5%ae%9a%e5%8d%95%e5%85%83%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=23566","title":{"rendered":"Excel \u4e2d\u7528 VBA \u5b9e\u73b0\u968f\u673a\u63d2\u5165\u56fe\u7247\u5230\u6307\u5b9a\u5355\u5143\u683c"},"content":{"rendered":"<p>2026\/2\/25<br \/>\n\u9700\u6c42\u662f\u4e00\u4e2a\u540c\u4e8b\u63d0\u51fa\u7684\uff0c\u7528\u4e8e\u968f\u673a\u62bd\u53d6\u56fe\u7247\u586b\u5145\u5230\u62a5\u544a\u4e2d\uff0c\u907f\u514d\u6bcf\u6b21\u62a5\u544a\u90fd\u662f\u540c\u4e00\u5f20\u56fe\u7247\u3002\u6211\u62df\u51fa\u63d0\u793a\u8bed\u540e\uff0c\u7531 Google Gemini \u751f\u6210\u4ee3\u7801\uff0c\u529f\u80fd\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u6307\u5b9a\u56fe\u7247\u6765\u6e90\u7684\u76ee\u5f55\uff1b<\/li>\n<li>\u6307\u5b9a\u56fe\u7247\u63d2\u5165\u7684\u5355\u5143\u683c\uff1b<\/li>\n<li>\u968f\u673a\u62bd\u53d6\u56fe\u7247\u63d2\u5165\uff1b<\/li>\n<li>\u63d2\u5165\u7684\u56fe\u7247\u586b\u5145\u6ee1\u5355\u5143\u683c\uff08\u5982\u679c\u5b58\u5728\u5408\u5e76\u5355\u5143\u683c\uff0c\u5219\u586b\u5145\u6ee1\u5408\u5e76\u540e\u7684\u5355\u5143\u683c\uff09\uff0c\u56fe\u7247\u7f29\u653e\u4fdd\u6301\u6bd4\u4f8b\uff1b<\/li>\n<li>\u5bf9\u5e94\u8868\u4e2d\u4efb\u610f\u5355\u5143\u683c\u53d1\u751f\u4fee\u6539\u65f6\u89e6\u53d1\u968f\u673a\u63d2\u5165\u56fe\u7247\uff1b<\/li>\n<li>\u63d2\u5165\u65b0\u56fe\u7247\u540e\u5220\u9664\u5728\u6307\u5b9a\u5355\u5143\u683c\uff08\u53ca\u5408\u5e76\u5355\u5143\u683c\uff09\u8303\u56f4\u5185\u7684\u5176\u5b83\u6240\u6709\u56fe\u7247\u3002<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/02\/20260225_160354.gif\" alt=\"\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 356px; --smush-placeholder-aspect-ratio: 356\/208;\" \/><\/li>\n<\/ul>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-vba\">Private Sub Worksheet_Change(ByVal Target As Range)\n    &#039; --- \u914d\u7f6e\u533a\u57df ---\n    Dim targetCellAddress As String\n    Dim imageFolderPath As String\n\n    &#039; 1. \u6307\u5b9a\u56fe\u7247\u586b\u5145\u7684\u5355\u5143\u683c\uff08\u652f\u6301\u5408\u5e76\u5355\u5143\u683c\uff09\n    targetCellAddress = &quot;F5&quot;\n\n    &#039; 2. \u6307\u5b9a\u56fe\u7247\u6587\u4ef6\u5939\u8def\u5f84\n    imageFolderPath = &quot;C:\\Users\\IYATTyx\\Pictures\\Screenshots&quot;\n    &#039; ----------------\n\n    Dim rngTarget As Range\n    Dim fso As Object\n    Dim file As Object\n    Dim imgFiles As New Collection\n    Dim randomIdx As Integer\n    Dim fullPath As String\n    Dim shp As Shape\n    Dim pic As Shape\n    Dim ratioCell As Double, ratioPic As Double\n    Dim ext As String\n\n    &#039; \u786e\u4fdd\u8def\u5f84\u4ee5\u53cd\u659c\u6760\u7ed3\u5c3e\n    If Right(imageFolderPath, 1) &lt;&gt; &quot;\\&quot; Then imageFolderPath = imageFolderPath &amp; &quot;\\&quot;\n\n    On Error GoTo ErrorHandler\n    Application.EnableEvents = False &#039; \u7981\u7528\u4e8b\u4ef6\uff0c\u9632\u6b62\u9012\u5f52\u89e6\u53d1\n\n    Set rngTarget = Me.Range(targetCellAddress).MergeArea\n\n    &#039; 1. \u5220\u9664\u76ee\u6807\u533a\u57df\u53ca\u5176\u91cd\u53e0\u8303\u56f4\u5185\u7684\u65e7\u56fe\u7247\n    For Each shp In Me.Shapes\n        If Not Intersect(shp.TopLeftCell, rngTarget) Is Nothing Then\n            shp.Delete\n        End If\n    Next shp\n\n    &#039; 2. \u521d\u59cb\u5316\u6587\u4ef6\u7cfb\u7edf\u5bf9\u8c61\n    Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)\n    If Not fso.FolderExists(imageFolderPath) Then\n        MsgBox &quot;\u6587\u4ef6\u5939\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u8bf7\u68c0\u67e5\uff1a&quot; &amp; vbCrLf &amp; imageFolderPath, vbExclamation\n        GoTo CleanUp\n    End If\n\n    &#039; 3. \u904d\u5386\u5e76\u8bb0\u5f55\u6240\u6709\u5e38\u7528\u683c\u5f0f\u56fe\u7247\n    For Each file In fso.GetFolder(imageFolderPath).Files\n        ext = LCase(fso.GetExtensionName(file.Name))\n        Select Case ext\n            &#039; \u6db5\u76d6\u7edd\u5927\u591a\u6570\u5e38\u7528\u56fe\u7247\u683c\u5f0f\n            Case &quot;jpg&quot;, &quot;jpeg&quot;, &quot;png&quot;, &quot;bmp&quot;, &quot;gif&quot;, &quot;webp&quot;, &quot;jfif&quot;, &quot;tif&quot;, &quot;tiff&quot;, &quot;ico&quot;\n                imgFiles.Add file.Path\n        End Select\n    Next file\n\n    If imgFiles.Count = 0 Then\n        &#039; \u5982\u679c\u6ca1\u627e\u5230\u56fe\u7247\uff0c\u76f4\u63a5\u9000\u51fa\u4e0d\u62a5\u9519\n        GoTo CleanUp\n    End If\n\n    &#039; 4. \u968f\u673a\u62bd\u53d6\n    Randomize\n    randomIdx = Int((imgFiles.Count * Rnd) + 1)\n    fullPath = imgFiles(randomIdx)\n\n    &#039; 5. \u63d2\u5165\u56fe\u7247\n    &#039; \u4f7f\u7528 Width:=-1, Height:=-1 \u4ee5\u4fdd\u6301\u539f\u59cb\u6bd4\u4f8b\u5bfc\u5165\n    Set pic = Me.Shapes.AddPicture(Filename:=fullPath, _\n                                  LinkToFile:=msoFalse, _\n                                  SaveWithDocument:=msoTrue, _\n                                  Left:=rngTarget.Left, _\n                                  Top:=rngTarget.Top, _\n                                  Width:=-1, Height:=-1)\n\n    pic.LockAspectRatio = msoTrue &#039; \u9501\u5b9a\u7eb5\u6a2a\u6bd4\n\n    &#039; 6. \u8ba1\u7b97\u7f29\u653e\u6bd4\u4f8b (\u4fdd\u8bc1\u56fe\u7247\u5b8c\u5168\u4f4d\u4e8e\u5355\u5143\u683c\u5185)\n    ratioCell = rngTarget.Width \/ rngTarget.Height\n    ratioPic = pic.Width \/ pic.Height\n\n    If ratioPic &gt; ratioCell Then\n        &#039; \u56fe\u7247\u592a\u5bbd\uff0c\u4ee5\u5355\u5143\u683c\u5bbd\u5ea6\u4e3a\u57fa\u51c6\u7f29\u653e\n        pic.Width = rngTarget.Width\n    Else\n        &#039; \u56fe\u7247\u592a\u9ad8\uff0c\u4ee5\u5355\u5143\u683c\u9ad8\u5ea6\u4e3a\u57fa\u51c6\u7f29\u653e\n        pic.Height = rngTarget.Height\n    End If\n\n    &#039; 7. \u5c45\u4e2d\u653e\u7f6e\n    pic.Left = rngTarget.Left + (rngTarget.Width - pic.Width) \/ 2\n    pic.Top = rngTarget.Top + (rngTarget.Height - pic.Height) \/ 2\n\nCleanUp:\n    Application.EnableEvents = True\n    Exit Sub\n\nErrorHandler:\n    MsgBox &quot;\u7a0b\u5e8f\u8fd0\u884c\u51fa\u9519: &quot; &amp; Err.Description, vbCritical\n    Resume CleanUp\nEnd Sub<\/code><\/pre>\n<p>\u5728 Excel \u4e2d\u6309 <code class=\"kb-btn\">Alt<\/code>+<code class=\"kb-btn\">F11<\/code> \u6253\u5f00\u5b8f\u7f16\u8f91\u5668\uff0c\u5728\u5bf9\u5e94\u7684\u5de5\u4f5c\u7c3f\u7684\u8868\u683c\u63d2\u5165\u4e0a\u9762\u4ee3\u7801\uff0c\u6211\u8fd9\u91cc\u5c31\u662f<strong>\u5de5\u4f5c\u7c3f4<\/strong>\u7684<strong>Sheet1<\/strong>\u8868<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/02\/image-1772006289838.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 986px; --smush-placeholder-aspect-ratio: 986\/783;\" \/><\/p>\n<p>\u4fdd\u5b58 Excel \u6587\u4ef6\u65f6\u9009 <strong>.xlsm<\/strong> \u683c\u5f0f\uff0c\u53ef\u4ee5\u4fdd\u5b58\u5b8f<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/02\/image-1772008737477.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 1017px; --smush-placeholder-aspect-ratio: 1017\/803;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2026\/2\/25 \u9700\u6c42\u662f\u4e00\u4e2a\u540c\u4e8b\u63d0\u51fa\u7684\uff0c\u7528\u4e8e\u968f\u673a\u62bd\u53d6\u56fe\u7247\u586b\u5145\u5230\u62a5\u544a\u4e2d\uff0c\u907f\u514d\u6bcf\u6b21\u62a5\u544a\u90fd\u662f\u540c\u4e00\u5f20\u56fe\u7247\u3002\u6211\u62df\u51fa\u63d0\u793a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"zakra_page_container_layout":"customizer","zakra_page_sidebar_layout":"customizer","zakra_remove_content_margin":false,"zakra_sidebar":"customizer","zakra_transparent_header":"customizer","zakra_logo":0,"zakra_main_header_style":"default","zakra_menu_item_color":"","zakra_menu_item_hover_color":null,"zakra_menu_item_active_color":null,"zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,649,648],"tags":[1791,1964,1966,1965,1968,1967],"class_list":["post-23566","post","type-post","status-publish","format-standard","hentry","category-all","category-excel","category-office","tag-office","tag-vba","tag-1966","tag-1965","tag-1968","tag-1967"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/23566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23566"}],"version-history":[{"count":10,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/23566\/revisions"}],"predecessor-version":[{"id":23588,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/23566\/revisions\/23588"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}