{"id":25237,"date":"2026-08-25T10:43:11","date_gmt":"2026-08-25T02:43:11","guid":{"rendered":"https:\/\/blog.iyatt.com\/?p=25237"},"modified":"2026-08-25T10:46:52","modified_gmt":"2026-08-25T02:46:52","slug":"excel-%e5%b8%a6%e5%90%88%e5%b9%b6%e5%8d%95%e5%85%83%e6%a0%bc%e7%9a%84%e6%95%b0%e6%8d%ae%e9%a1%ba%e5%bb%b6%e7%b2%98%e8%b4%b4-vba-%e5%ae%8f","status":"publish","type":"post","link":"https:\/\/blog.iyatt.com\/?p=25237","title":{"rendered":"Excel \u5e26\u5408\u5e76\u5355\u5143\u683c\u7684\u6570\u636e\u987a\u5ef6\u590d\u5236\u7c98\u8d34 VBA \u5b8f"},"content":{"rendered":"<p>2026\/8\/25<br \/>\n\u4e3b\u8981\u5b9e\u73b0\u6309\u5408\u5e76\u5355\u5143\u683c\u590d\u5236\u7c98\u8d34\u6570\u636e\u3002<br \/>\n\u6bd4\u5982\u6211\u6709 5 \u5217\u6570\u636e\uff08\u65e0\u5408\u5e76\u5355\u5143\u683c\uff09\uff0c\u9700\u8981\u7c98\u8d34\u5230\u62a5\u544a\u4e2d\uff0c\u4f46\u662f\u62a5\u544a\u6a21\u677f\u4e2d\u7b2c 3 \u5217\u5b9e\u9645\u662f 2 \u5217\u5408\u5e76\u800c\u6765\u7684\u3002\u76f4\u63a5\u7c98\u8d34\u53ef\u80fd\u4f1a\u5931\u8d25\u6216\u8005\u6ca1\u6709\u6309\u7167\u9884\u671f\u5408\u5e76\u540e\u7684\u5355\u5143\u683c\u987a\u5e8f\u7c98\u8d34\u3002<br \/>\n\u6bd4\u5982\u53d8\u6210\u4e0b\u9762\u8fd9\u6837\uff0c\u628a\u5408\u5e76\u7684\u5355\u5143\u683c\u91cd\u65b0\u7ed9\u6211\u62c6\u5206\u6210 2 \u5217\u518d\u7c98\u8d34\u4e86\u3002<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/08\/image-1787625317842.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 603px; --smush-placeholder-aspect-ratio: 603\/542;\" \/><\/p>\n<p>Excel \u6253\u5f00\u72b6\u6001\uff0c\u6309<code class=\"kb-btn\">Alt<\/code>+<code class=\"kb-btn\">F11<\/code>\u6253\u5f00\u5b8f\u7f16\u8f91\u5668\uff0c\u5728\u67d0\u5f20\u8868\u4e0b\uff0c\u7c98\u8d34\u4e0b\u9762\u4ee3\u7801\uff08\u5982\u679c\u8981\u628a\u5b8f\u4fdd\u5b58\u5230 Excel \u6587\u4ef6\u4e2d\uff0c\u9700\u8981\u4fdd\u5b58\u4e3a .xlsm \u6269\u5c55\u540d\uff09<\/p>\n<pre><code class=\"language-vba\">Sub CopyMergedByMatrixFixed()\n    Dim srcRange As Range, destStartCell As Range\n    Dim cell As Range, currentArea As Range\n    Dim uniqueBlocks As New Collection\n    Dim block As Range, i As Long, j As Long, temp As Range\n\n    &#039; 1. \u6267\u884c\u811a\u672c\u540e\uff0c\u63d0\u793a\u9009\u62e9\u3010\u6e90\u6570\u636e\u533a\u57df\u3011\n    On Error Resume Next\n    Set srcRange = Application.InputBox(&quot;\u8bf7\u6846\u9009\u3010\u6e90\u6570\u636e\u533a\u57df\u3011\uff1a&quot;, &quot;\u9009\u62e9\u6e90\u6570\u636e&quot;, Type:=8)\n    On Error GoTo 0\n\n    &#039; \u5982\u679c\u7528\u6237\u70b9\u51fb\u53d6\u6d88\u6216\u672a\u9009\u62e9\uff0c\u9000\u51fa\u811a\u672c\n    If srcRange Is Nothing Then Exit Sub\n\n    &#039; 2. \u786e\u5b9a\u6e90\u6570\u636e\u540e\uff0c\u63d0\u793a\u9009\u62e9\u76ee\u6807\u7684\u3010\u7b2c\u4e00\u4e2a\u5355\u5143\u683c\/\u5408\u5e76\u5757\u3011\n    On Error Resume Next\n    Set destStartCell = Application.InputBox(&quot;\u8bf7\u70b9\u51fb\u76ee\u6807\u8868\u683c\u7684\u3010\u7b2c\u4e00\u4e2a\u5355\u5143\u683c\/\u5408\u5e76\u5757\u3011\uff1a&quot;, &quot;\u9009\u62e9\u76ee\u6807\u8d77\u59cb\u4f4d\u7f6e&quot;, Type:=8)\n    On Error GoTo 0\n\n    &#039; \u5982\u679c\u7528\u6237\u70b9\u51fb\u53d6\u6d88\u6216\u672a\u9009\u62e9\uff0c\u9000\u51fa\u811a\u672c\n    If destStartCell Is Nothing Then Exit Sub\n    Set destStartCell = destStartCell.Cells(1, 1).MergeArea.Cells(1, 1)\n\n    Application.ScreenUpdating = False\n\n    &#039; 3. \u63d0\u53d6\u6e90\u533a\u57df\u6240\u6709\u201c\u975e\u91cd\u590d\u5408\u5e76\u5757\/\u5355\u683c\u201d\u7684\u5de6\u4e0a\u89d2\u5355\u5143\u683c\n    For Each cell In srcRange\n        Set currentArea = cell.MergeArea\n        If cell.Address = currentArea.Cells(1, 1).Address Then\n            uniqueBlocks.Add currentArea.Cells(1, 1)\n        End If\n    Next cell\n\n    If uniqueBlocks.Count = 0 Then GoTo CleanUp\n\n    &#039; 4. \u5bf9\u63d0\u53d6\u51fa\u7684\u5408\u5e76\u5757\u8fdb\u884c\u53cc\u5411\u6392\u5e8f\uff08\u6309 Top \u5347\u5e8f\uff0cTop \u76f8\u540c\u65f6\u6309 Left \u5347\u5e8f\uff09\n    For i = 1 To uniqueBlocks.Count - 1\n        For j = i + 1 To uniqueBlocks.Count\n            Dim topDiff As Double, leftDiff As Double\n            topDiff = uniqueBlocks(i).Top - uniqueBlocks(j).Top\n            leftDiff = uniqueBlocks(i).Left - uniqueBlocks(j).Left\n\n            &#039; \u5982\u679c i \u5728 j \u4e0b\u65b9\uff0c\u6216\u8005\u5728\u540c\u4e00\u884c\u4f46 i \u5728 j \u53f3\u4fa7\uff0c\u5219\u4ea4\u6362\n            If topDiff &gt; 1 Or (Abs(topDiff) &lt;= 1 And leftDiff &gt; 1) Then\n                Set temp = uniqueBlocks(i)\n                Set uniqueBlocks(i) = uniqueBlocks(j)\n                Set uniqueBlocks(j) = temp\n            End If\n        Next j\n    Next i\n\n    &#039; 5. \u89e3\u6790\u6e90\u6570\u636e\u903b\u8f91\u884c\u5217\u5e76\u5b58\u5165\u5b57\u5178 (Key: &quot;\u884c_\u5217&quot;, Value: \u503c)\n    Dim valDict As Object\n    Set valDict = CreateObject(&quot;Scripting.Dictionary&quot;)\n\n    Dim curR As Long, curC As Long\n    Dim lastTop As Double, maxR As Long, maxC As Long\n    lastTop = -1: curR = 0: curC = 0\n\n    For i = 1 To uniqueBlocks.Count\n        Set block = uniqueBlocks(i)\n\n        &#039; \u5224\u5b9a\u662f\u5426\u6362\u884c\n        If lastTop = -1 Or Abs(block.Top - lastTop) &gt; 1 Then\n            curR = curR + 1\n            curC = 1\n            lastTop = block.Top\n        Else\n            curC = curC + 1\n        End If\n\n        If curR &gt; maxR Then maxR = curR\n        If curC &gt; maxC Then maxC = curC\n\n        valDict.Add curR &amp; &quot;_&quot; &amp; curC, block.Value\n    Next i\n\n    &#039; 6. \u4e25\u683c\u6309\u76ee\u6807\u7684\u3010\u7269\u7406\u884c\u5217\u3011\u63a8\u6f14\u903b\u8f91\u77e9\u9635\u5e76\u586b\u5145\u6570\u636e\n    Dim r As Long, c As Long\n    Dim curRowAnchor As Range, curCell As Range\n    Set curRowAnchor = destStartCell\n\n    For r = 1 To maxR\n        Set curCell = curRowAnchor\n        For c = 1 To maxC\n            &#039; \u5c06\u6e90\u903b\u8f91\u4f4d\u7f6e (r, c) \u7684\u503c\u586b\u5165\u76ee\u6807\u903b\u8f91\u4f4d\u7f6e\n            If valDict.Exists(r &amp; &quot;_&quot; &amp; c) Then\n                curCell.Value = valDict(r &amp; &quot;_&quot; &amp; c)\n            End If\n\n            &#039; \u903b\u8f91\u5217\u5411\u53f3\u63a8\u6f14\uff1a\u627e\u5230\u4e0b\u4e00\u4e2a\u4e0d\u5c5e\u4e8e\u5f53\u524d MergeArea \u7684\u7269\u7406\u5217\n            If c &lt; maxC Then\n                Set curCell = curCell.Worksheet.Cells(curCell.Row, curCell.MergeArea.Column + curCell.MergeArea.Columns.Count).MergeArea.Cells(1, 1)\n            End If\n        Next c\n\n        &#039; \u903b\u8f91\u884c\u5411\u4e0b\u63a8\u6f14\uff1a\u627e\u5230\u4e0b\u4e00\u4e2a\u4e0d\u5c5e\u4e8e\u5f53\u524d\u884c MergeArea \u7684\u7269\u7406\u884c\n        If r &lt; maxR Then\n            Set curRowAnchor = curRowAnchor.Worksheet.Cells(curRowAnchor.MergeArea.Row + curRowAnchor.MergeArea.Rows.Count, destStartCell.Column).MergeArea.Cells(1, 1)\n        End If\n    Next r\n\nCleanUp:\n    Application.ScreenUpdating = True\n    MsgBox &quot;\u590d\u5236\u5b8c\u6210\uff01\u5df2\u6309\u903b\u8f91\u884c\u5217\u6210\u529f\u586b\u5145\u3002&quot;, vbInformation\nEnd Sub<\/code><\/pre>\n<p>\u6309<code class=\"kb-btn\">Alt<\/code>+<code class=\"kb-btn\">F8<\/code>\u9009\u62e9\u5b8f\u51fd\u6570\u6267\u884c<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/08\/image-1787625605874.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 612px; --smush-placeholder-aspect-ratio: 612\/460;\" \/><\/p>\n<p>\u9009\u62e9\u8981\u590d\u5236\u7684\u6570\u636e\uff0c\u70b9<code class=\"kb-btn\">\u786e\u5b9a<\/code><br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/08\/image-1787625636898.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 768px; --smush-placeholder-aspect-ratio: 768\/363;\" \/><\/p>\n<p>\u9009\u62e9\u8981\u7c98\u8d34\u5230\u7684\u4f4d\u7f6e\u7684\u5de6\u4e0a\u89d2\u5355\u5143\u683c\uff0c\u70b9<code class=\"kb-btn\">\u786e\u5b9a<\/code><br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/08\/image-1787625737584.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 574px; --smush-placeholder-aspect-ratio: 574\/495;\" \/><\/p>\n<p>\u7c98\u8d34\u5b8c\u6210<br \/>\n<img decoding=\"async\" data-src=\"https:\/\/blog.iyatt.com\/wp-content\/uploads\/2026\/08\/image-1787625781089.png\" alt=\"file\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" class=\"lazyload\" style=\"--smush-placeholder-width: 503px; --smush-placeholder-aspect-ratio: 503\/475;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>2026\/8\/25 \u4e3b\u8981\u5b9e\u73b0\u6309\u5408\u5e76\u5355\u5143\u683c\u590d\u5236\u7c98\u8d34\u6570\u636e\u3002 \u6bd4\u5982\u6211\u6709 5 \u5217\u6570\u636e\uff08\u65e0\u5408\u5e76\u5355\u5143\u683c\uff09\uff0c\u9700\u8981\u7c98\u8d34\u5230\u62a5\u544a\u4e2d [&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,575],"tags":[1750,1964,2222,2095,2220,1965,2221],"class_list":["post-25237","post","type-post","status-publish","format-standard","hentry","category-all","category-excel","category-office","category-575","tag-excel","tag-vba","tag-2222","tag-2095","tag-2220","tag-1965","tag-2221"],"modified_by":"IYATT-yx","_links":{"self":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25237","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=25237"}],"version-history":[{"count":2,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25237\/revisions"}],"predecessor-version":[{"id":25244,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=\/wp\/v2\/posts\/25237\/revisions\/25244"}],"wp:attachment":[{"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.iyatt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}