{"id":17740,"date":"2024-10-25T17:35:30","date_gmt":"2024-10-25T09:35:30","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=17740"},"modified":"2025-11-15T15:03:24","modified_gmt":"2025-11-15T07:03:24","slug":"excel-%e5%9f%ba%e4%ba%8e-vba-%e5%ae%9e%e7%8e%b0%e5%ba%8f%e5%88%97%e5%8f%b7%e5%bd%95%e5%85%a5%ef%bc%88%e6%9f%a5%e9%87%8d%e3%80%81%e8%ae%b0%e5%bd%95%e6%97%b6%e9%97%b4%ef%bc%89","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=17740","title":{"rendered":"Excel \u57fa\u4e8e VBA \u5b9e\u73b0\u5e8f\u5217\u53f7\u5f55\u5165\uff08\u67e5\u91cd\u3001\u8bb0\u5f55\u65f6\u95f4\uff09"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor:pointer\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.iyatt.com\/?p=17740\/#%E5%89%8D%E8%A8%80\" >\u524d\u8a00<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.iyatt.com\/?p=17740\/#%E7%8E%AF%E5%A2%83\" >\u73af\u5883<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.iyatt.com\/?p=17740\/#%E6%96%B9%E6%A1%88\" >\u65b9\u6848<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E5%89%8D%E8%A8%80\"><\/span>\u524d\u8a00<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u516c\u53f8\u751f\u4ea7\u7684\u96f6\u4ef6\u4ea7\u54c1\u4f1a\u6253\u4e0a\u4e8c\u7ef4\u7801\uff0c\u7136\u540e\u4f7f\u7528\u626b\u7801\u67aa\u626b\u63cf\u5f55\u5165\u7cfb\u7edf\uff0c\u4fbf\u4e8e\u8ffd\u6eaf\u3002\u8fd9\u91cc\u6211\u7528 Excel \u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u6613\u7684\u201c\u6570\u636e\u5e93\u201d\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E7%8E%AF%E5%A2%83\"><\/span>\u73af\u5883<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Microsoft Office 2021<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E6%96%B9%E6%A1%88\"><\/span>\u65b9\u6848<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u6253\u5f00 Excel\uff0c\u65b0\u5efa\u4e00\u4e2a\u8868\u683c\uff0c\u6309<code class=\"kb-btn\">Alt<\/code>+<code class=\"kb-btn\">F11<\/code>\u6253\u5f00VBA\u7f16\u8f91\u5668\uff0c\u5728\u5de6\u4fa7\u627e\u5230\u4f7f\u7528\u7684\u5de5\u4f5c\u8868\uff0c\u6bd4\u5982\u6211\u8fd9\u91cc\u662fSheet1\uff0c\u5c31\u53cc\u51fb\u6253\u5f00<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/10\/image-1729848407670.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 272px; --smush-placeholder-aspect-ratio: 272\/90;\" \/><\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/10\/image-1729848385941.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 191px; --smush-placeholder-aspect-ratio: 191\/114;\" \/><\/p>\n<p>\u7136\u540e\u590d\u5236\u7c98\u7c98\u4e0b\u9762\u5185\u5bb9\u540e\uff0c\u76f4\u63a5\u5173\u95ed\u7f16\u8f91\u9a91\u7a97\u53e3<\/p>\n<pre><code class=\"language-bash\">Private Sub Worksheet_Change(ByVal Target As Range)\n    Dim ws As Worksheet\n    Dim LastRow As Long\n    Dim i As Long\n    Dim InputStr As String\n\n    &#039; \u786e\u4fdd\u4ec5\u5728A1\u5355\u5143\u683c\u53d8\u5316\u65f6\u6267\u884c\n    If Target.Address = &quot;$A$1&quot; Then\n        Set ws = ThisWorkbook.Sheets(&quot;Sheet1&quot;) &#039;\u6839\u636e\u9700\u8981\u4fee\u6539\u5de5\u4f5c\u8868\u540d\u79f0\n        InputStr = Target.Value\n\n        &#039; \u5982\u679c\u8f93\u5165\u4e3a\u7a7a\uff0c\u5219\u4e0d\u5904\u7406\n        If InputStr = &quot;&quot; Then Exit Sub\n\n        &#039; \u68c0\u67e5\u662f\u5426\u6709\u91cd\u590d\n        LastRow = ws.Cells(ws.Rows.Count, &quot;A&quot;).End(xlUp).Row\n        For i = 2 To LastRow\n            If ws.Cells(i, 1).Value = InputStr Then\n                MsgBox &quot;\u8f93\u5165\u5185\u5bb9\u91cd\u590d\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u3002&quot;, vbExclamation\n                ws.Cells(1, 1).Select\n                Exit Sub\n            End If\n        Next i\n\n        &#039; \u65e0\u91cd\u590d\uff0c\u63d2\u5165\u6570\u636e\u5e76\u4e0b\u79fb\u5176\u4ed6\u5185\u5bb9\n        ws.Rows(2).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove\n        ws.Cells(2, 1).Value = InputStr\n        ws.Cells(2, 2).Value = Format(Now, &quot;yyyy-mm-dd&quot;)\n        ws.Cells(2, 3).Value = Format(Now, &quot;hh:nn:ss&quot;)\n\n        &#039; \u6e05\u7a7aA1\uff0c\u4fdd\u6301\u5149\u6807\u5728A1\n        Application.EnableEvents = False\n        ws.Cells(1, 1).Value = &quot;&quot;\n        Application.EnableEvents = True\n        ws.Cells(1, 1).Select\n    End If\nEnd Sub\n\nPrivate Sub Worksheet_SelectionChange(ByVal Target As Range)\n    If Not Application.Intersect(Target, Me.Range(&quot;A1&quot;)) Is Nothing Then\n        &#039; \u5141\u8bb8\u5728A1\u5355\u5143\u683c\u4e2d\u9009\u62e9\u548c\u8f93\u5165\n        Application.EnableEvents = True\n    Else\n        &#039; \u5982\u679c\u5f53\u524d\u5355\u5143\u683c\u4e0d\u662fA1\uff0c\u5219\u5207\u6362\u5230A1\n        Application.EnableEvents = False\n        Me.Range(&quot;A1&quot;).Select\n        Application.EnableEvents = True\n    End If\nEnd Sub<\/code><\/pre>\n<p>\u5728 Sheet1 \u5de5\u4f5c\u8868\u4e2d\u9996\u5148\u4f1a\u9501\u5b9a\u6d3b\u52a8\u5355\u5143\u683c\u4e3a<strong>A1<\/strong>\uff0c\u53ea\u5728<strong>A1<\/strong>\u5355\u5143\u683c\u8f93\u5165\u5185\u5bb9\uff0c\u6211\u5206\u522b\u8f93\u5165<br \/>\n\u201cABC\u201d\u3001\u201cabc\u201d\uff0c\u6bcf\u6b21\u8f93\u5165\u540e\u56de\u8f66\uff0c\u4f1a\u5f97\u5230\u4e0b\u9762\u5185\u5bb9<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/10\/image-1729848649013.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 303px; --smush-placeholder-aspect-ratio: 303\/115;\" \/><\/p>\n<p>\u6bcf\u6b21\u56de\u8f66\u8f93\u5165\u7684\u5185\u5bb9\u4f1a\u63d2\u5165\u5230<strong>A2<\/strong>\uff0c\u5e76\u4e14\u5728\u540e\u9762\u4e24\u5217\u5206\u522b\u63d2\u5165\u8f93\u5165\u7684\u65e5\u671f\u548c\u65f6\u95f4\u3002<br \/>\n\u5f53\u8f93\u5165\u91cd\u590d\u5185\u5bb9\u65f6<\/p>\n<p><img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2024\/10\/image-1729848764702.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 373px; --smush-placeholder-aspect-ratio: 373\/321;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u516c\u53f8\u751f\u4ea7\u7684\u96f6\u4ef6\u4ea7\u54c1\u4f1a\u6253\u4e0a\u4e8c\u7ef4\u7801\uff0c\u7136\u540e\u4f7f\u7528\u626b\u7801\u67aa\u626b\u63cf\u5f55\u5165\u7cfb\u7edf\uff0c\u4fbf\u4e8e\u8ffd\u6eaf\u3002\u8fd9\u91cc\u6211\u7528 Excel \u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80 [&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":"","zakra_menu_item_active_color":"","zakra_menu_active_style":"","zakra_page_header":true,"_lmt_disableupdate":"no","_lmt_disable":"no","footnotes":""},"categories":[1,649,648,611,594],"tags":[],"class_list":["post-17740","post","type-post","status-publish","format-standard","hentry","category-all","category-excel","category-office","category-windows","category-594"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/17740","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=17740"}],"version-history":[{"count":1,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/17740\/revisions"}],"predecessor-version":[{"id":22768,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/17740\/revisions\/22768"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}