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