[{"data":1,"prerenderedAt":515},["ShallowReactive",2],{"post-\u002Fposts\u002Funiversity-excel-vba-macro":3,"post-nav-\u002Fposts\u002Funiversity-excel-vba-macro":506},{"id":4,"title":5,"body":6,"date":492,"description":493,"draft":494,"extension":495,"meta":496,"navigation":162,"path":497,"seo":498,"stem":499,"summary":500,"tags":501,"__hash__":505},"posts\u002Fposts\u002Funiversity-excel-vba-macro.md","大学时写的第一段宏：比对 Excel 的两列",{"type":7,"value":8,"toc":489},"minimark",[9,29,32,416,420,485],[10,11,12],"blockquote",{},[13,14,15,16,20,21,24,25,28],"p",{},"这篇是 2020 年 6 月记的，大学里第一次为了一张表去写宏。代码原样贴着：",[17,18,19],"code",{},"MsgBox"," 当调试输出、",[17,22,23],{},"Dim"," 写在循环里、变量名叫 ",[17,26,27],{},"dontknow","，都没动。",[13,30,31],{},"要做的事：选中两列，从第一行往下逐字对齐比较，哪里不一样，就把后面的内容弹出来。",[33,34,39],"pre",{"className":35,"code":36,"language":37,"meta":38,"style":38},"language-vb shiki shiki-themes github-light github-dark","Function diffcell(cell1 As Range, cell2 As Range)\n    Dim str1 As String, str2 As String\n    str1 = cell1.Text\n    str2 = cell2.Text\n    Dim i As Integer, j As Integer\n    i = 1\n    j = 1\n    While i \u003C> Len(str1) And j \u003C> Len(str2)\n        If Mid(str1, i, 1) = Mid(str2, j, 1) Then\n            i = i + 1\n            j = j + 1\n        Else\n            Dim afterward1 As String, afterward2 As String\n            afterward1 = Mid(str1, i)\n            afterward2 = Mid(str2, j)\n            Dim foundIdx1 As Integer, foundIdx2 As Integer\n            foundIdx1 = InStr(i, str1, afterward2)\n            foundIdx2 = InStr(j, str2, afterward2)\n            MsgBox Mid(str1, foundIdx1 - i)\n\n            If foundIdx1 \u003C foundIdx2 Then\n                MsgBox Mid(str1, foundIdx1 - i)\n                i = foundIdx1\n            Else\n                MsgBox Mid(str2, foundIdx2 - j)\n                j = foundIdx2\n            End If\n        End If\n    Wend\n    If i \u003C Len(str1) Then\n        MsgBox Mid(str1, i)\n    Else\n        MsgBox Mid(str2, j)\n    End If\nEnd Function\n\nSub test()\n    Dim rng As Range\n    On Error Resume Next\n    Set rng = Selection\n    On Error GoTo 0\n    If rng Is Nothing Then\n        MsgBox \"未选择区域\"\n        Return\n    End If\n    If TypeName(rng) \u003C> \"Range\" Then\n        MsgBox \"选的不是格子\"\n        Return\n    End If\n    If rng.Columns.Count \u003C> 2 Then\n        MsgBox \"选中列数量应为2\"\n        Return\n    End If\n    Dim skipRow As Integer\n    skipRow = 1\n    For i = 1 + skipRow To rng.Rows.Count\n        Dim cell1 As Range, cell2 As Range\n        Set cell1 = rng.Cells(i, 1)\n        Set cell2 = rng.Cells(i, 2)\n        Dim dontknow As Variant\n        dontknow = diffcell(cell1, cell2)\n    Next i\nEnd Sub\n","vb","",[17,40,41,49,55,61,67,73,79,85,91,97,103,109,115,121,127,133,139,145,151,157,164,170,176,182,188,194,200,206,212,218,224,230,236,242,248,254,259,265,271,277,283,289,295,301,307,312,318,324,329,334,340,346,351,356,362,368,374,380,386,392,398,404,410],{"__ignoreMap":38},[42,43,46],"span",{"class":44,"line":45},"line",1,[42,47,48],{},"Function diffcell(cell1 As Range, cell2 As Range)\n",[42,50,52],{"class":44,"line":51},2,[42,53,54],{},"    Dim str1 As String, str2 As String\n",[42,56,58],{"class":44,"line":57},3,[42,59,60],{},"    str1 = cell1.Text\n",[42,62,64],{"class":44,"line":63},4,[42,65,66],{},"    str2 = cell2.Text\n",[42,68,70],{"class":44,"line":69},5,[42,71,72],{},"    Dim i As Integer, j As Integer\n",[42,74,76],{"class":44,"line":75},6,[42,77,78],{},"    i = 1\n",[42,80,82],{"class":44,"line":81},7,[42,83,84],{},"    j = 1\n",[42,86,88],{"class":44,"line":87},8,[42,89,90],{},"    While i \u003C> Len(str1) And j \u003C> Len(str2)\n",[42,92,94],{"class":44,"line":93},9,[42,95,96],{},"        If Mid(str1, i, 1) = Mid(str2, j, 1) Then\n",[42,98,100],{"class":44,"line":99},10,[42,101,102],{},"            i = i + 1\n",[42,104,106],{"class":44,"line":105},11,[42,107,108],{},"            j = j + 1\n",[42,110,112],{"class":44,"line":111},12,[42,113,114],{},"        Else\n",[42,116,118],{"class":44,"line":117},13,[42,119,120],{},"            Dim afterward1 As String, afterward2 As String\n",[42,122,124],{"class":44,"line":123},14,[42,125,126],{},"            afterward1 = Mid(str1, i)\n",[42,128,130],{"class":44,"line":129},15,[42,131,132],{},"            afterward2 = Mid(str2, j)\n",[42,134,136],{"class":44,"line":135},16,[42,137,138],{},"            Dim foundIdx1 As Integer, foundIdx2 As Integer\n",[42,140,142],{"class":44,"line":141},17,[42,143,144],{},"            foundIdx1 = InStr(i, str1, afterward2)\n",[42,146,148],{"class":44,"line":147},18,[42,149,150],{},"            foundIdx2 = InStr(j, str2, afterward2)\n",[42,152,154],{"class":44,"line":153},19,[42,155,156],{},"            MsgBox Mid(str1, foundIdx1 - i)\n",[42,158,160],{"class":44,"line":159},20,[42,161,163],{"emptyLinePlaceholder":162},true,"\n",[42,165,167],{"class":44,"line":166},21,[42,168,169],{},"            If foundIdx1 \u003C foundIdx2 Then\n",[42,171,173],{"class":44,"line":172},22,[42,174,175],{},"                MsgBox Mid(str1, foundIdx1 - i)\n",[42,177,179],{"class":44,"line":178},23,[42,180,181],{},"                i = foundIdx1\n",[42,183,185],{"class":44,"line":184},24,[42,186,187],{},"            Else\n",[42,189,191],{"class":44,"line":190},25,[42,192,193],{},"                MsgBox Mid(str2, foundIdx2 - j)\n",[42,195,197],{"class":44,"line":196},26,[42,198,199],{},"                j = foundIdx2\n",[42,201,203],{"class":44,"line":202},27,[42,204,205],{},"            End If\n",[42,207,209],{"class":44,"line":208},28,[42,210,211],{},"        End If\n",[42,213,215],{"class":44,"line":214},29,[42,216,217],{},"    Wend\n",[42,219,221],{"class":44,"line":220},30,[42,222,223],{},"    If i \u003C Len(str1) Then\n",[42,225,227],{"class":44,"line":226},31,[42,228,229],{},"        MsgBox Mid(str1, i)\n",[42,231,233],{"class":44,"line":232},32,[42,234,235],{},"    Else\n",[42,237,239],{"class":44,"line":238},33,[42,240,241],{},"        MsgBox Mid(str2, j)\n",[42,243,245],{"class":44,"line":244},34,[42,246,247],{},"    End If\n",[42,249,251],{"class":44,"line":250},35,[42,252,253],{},"End Function\n",[42,255,257],{"class":44,"line":256},36,[42,258,163],{"emptyLinePlaceholder":162},[42,260,262],{"class":44,"line":261},37,[42,263,264],{},"Sub test()\n",[42,266,268],{"class":44,"line":267},38,[42,269,270],{},"    Dim rng As Range\n",[42,272,274],{"class":44,"line":273},39,[42,275,276],{},"    On Error Resume Next\n",[42,278,280],{"class":44,"line":279},40,[42,281,282],{},"    Set rng = Selection\n",[42,284,286],{"class":44,"line":285},41,[42,287,288],{},"    On Error GoTo 0\n",[42,290,292],{"class":44,"line":291},42,[42,293,294],{},"    If rng Is Nothing Then\n",[42,296,298],{"class":44,"line":297},43,[42,299,300],{},"        MsgBox \"未选择区域\"\n",[42,302,304],{"class":44,"line":303},44,[42,305,306],{},"        Return\n",[42,308,310],{"class":44,"line":309},45,[42,311,247],{},[42,313,315],{"class":44,"line":314},46,[42,316,317],{},"    If TypeName(rng) \u003C> \"Range\" Then\n",[42,319,321],{"class":44,"line":320},47,[42,322,323],{},"        MsgBox \"选的不是格子\"\n",[42,325,327],{"class":44,"line":326},48,[42,328,306],{},[42,330,332],{"class":44,"line":331},49,[42,333,247],{},[42,335,337],{"class":44,"line":336},50,[42,338,339],{},"    If rng.Columns.Count \u003C> 2 Then\n",[42,341,343],{"class":44,"line":342},51,[42,344,345],{},"        MsgBox \"选中列数量应为2\"\n",[42,347,349],{"class":44,"line":348},52,[42,350,306],{},[42,352,354],{"class":44,"line":353},53,[42,355,247],{},[42,357,359],{"class":44,"line":358},54,[42,360,361],{},"    Dim skipRow As Integer\n",[42,363,365],{"class":44,"line":364},55,[42,366,367],{},"    skipRow = 1\n",[42,369,371],{"class":44,"line":370},56,[42,372,373],{},"    For i = 1 + skipRow To rng.Rows.Count\n",[42,375,377],{"class":44,"line":376},57,[42,378,379],{},"        Dim cell1 As Range, cell2 As Range\n",[42,381,383],{"class":44,"line":382},58,[42,384,385],{},"        Set cell1 = rng.Cells(i, 1)\n",[42,387,389],{"class":44,"line":388},59,[42,390,391],{},"        Set cell2 = rng.Cells(i, 2)\n",[42,393,395],{"class":44,"line":394},60,[42,396,397],{},"        Dim dontknow As Variant\n",[42,399,401],{"class":44,"line":400},61,[42,402,403],{},"        dontknow = diffcell(cell1, cell2)\n",[42,405,407],{"class":44,"line":406},62,[42,408,409],{},"    Next i\n",[42,411,413],{"class":44,"line":412},63,[42,414,415],{},"End Sub\n",[417,418,419],"h2",{"id":419},"现在回头看",[421,422,423,434,447,456,476],"ul",{},[424,425,426,429,430,433],"li",{},[17,427,428],{},"While i \u003C> Len(str1)","：用不等号收尾，最后一个字符永远比不到，得写 ",[17,431,432],{},"\u003C=","。",[424,435,436,439,440,443,444,433],{},[17,437,438],{},"Mid(str1, foundIdx1 - i)","：",[17,441,442],{},"Mid"," 的第二个参数是起始位置，我当时按长度用了。想取中间一段得写 ",[17,445,446],{},"Mid(str1, i, foundIdx1 - i)",[424,448,449,452,453,455],{},[17,450,451],{},"If"," 之前那句 ",[17,454,19],{}," 跟两个分支里的重复，是调日志时留下的。",[424,457,458,461,462,465,466,468,469,472,473,433],{},[17,459,460],{},"Sub"," 里用 ",[17,463,464],{},"Return"," 提前退出：VBA 的 ",[17,467,464],{}," 是配 ",[17,470,471],{},"GoSub"," 用的，这里应该写 ",[17,474,475],{},"Exit Sub",[424,477,478,481,482,484],{},[17,479,480],{},"diffcell"," 没有返回值，所以 ",[17,483,27],{}," 拿到的一直是空。",[486,487,488],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":38,"searchDepth":57,"depth":57,"links":490},[491],{"id":419,"depth":51,"text":419},"2020-06-13","一个没写完的 Excel VBA 宏：选中两列，逐字比较，不一样的地方用 MsgBox 弹出来。代码原样贴着。",false,"md",{},"\u002Fposts\u002Funiversity-excel-vba-macro",{"title":5,"description":493},"posts\u002Funiversity-excel-vba-macro","为了一张表第一次写宏，靠 MsgBox 调日志，最后也没收尾。",[502,503,504],"VBA","Excel","大学旧笔记","HLZgdQkGyIiMWoZBcjFV7oZh57afs6CjfiBLoY5k3OM",{"prev":507,"next":511},{"path":508,"title":509,"date":510},"\u002Fposts\u002Funiversity-php-notes","大学里学 PHP 记的那几页","2021-11-07",{"path":512,"title":513,"date":514},"\u002Fposts\u002Funiversity-python-notes","开学前的那几页 Python 笔记","2019-08-15",1792556025030]