Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 14356|回复: 61

[习题] VBA第8讲作业答案上传贴

[复制链接]
发表于 2008-1-9 22:48 | 显示全部楼层 |阅读模式
<p>&nbsp; 把第8讲的示例<font color="#e70808">后两问</font>答案<font color="#f70909">代码跟贴传上来</font>.不用上传附件.</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题目链接:http://www.excelpx.com/forum.php?mod=viewthread&tid=35173&extra=&page=1#35173</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
[此贴子已经被作者于2008-1-9 22:49:06编辑过]
发表于 2008-1-9 22:50 | 显示全部楼层

<p>Sub 唯一的编号()<br/>Dim arr1, arr2, arr3(), x As Integer, y As Integer, mrow1 As Integer, mrow2 As Integer, k As Integer, i As Integer, t<br/>t = Timer<br/>Range("d2:d" &amp; Range("d65536").End(xlUp).Row) = ""<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>mrow1 = UBound(arr1)<br/>mrow2 = UBound(arr2)<br/>ReDim arr3(1 To mrow1 + mrow2, 1 To 1)<br/>For i = 1 To mrow1<br/>&nbsp;arr3(i, 1) = arr1(i, 1)</p><p>Next i<br/>For y = 1 To mrow2<br/>&nbsp; For x = 1 To mrow1<br/>&nbsp;&nbsp; If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>Next x<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(i + k - 1, 1) = arr2(y, 1)<br/>100:<br/>Next y<br/>Range("d2").Resize(i + k - 1, 1) = arr3<br/>&nbsp;MsgBox Timer - t<br/>End Sub<br/><br/>Sub 物品1有物品2没有()<br/>Dim arr1, arr2, arr3(), x As Integer, y As Integer, mrow1 As Integer, mrow2 As Integer, k As Integer<br/>Range("e2:e" &amp; Range("e65536").End(xlUp).Row) = ""<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>mrow1 = UBound(arr1)<br/>mrow2 = UBound(arr2)<br/>ReDim arr3(1 To mrow1 + mrow2, 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>Next<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>Next<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub</p><p><br/>Sub 物品2有物品1没有()<br/>Dim arr1, arr2, arr3(), x As Integer, y As Integer, mrow1 As Integer, mrow2 As Integer, k As Integer<br/>Range("f2:f" &amp; Range("f65536").End(xlUp).Row) = ""<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>mrow1 = UBound(arr1)<br/>mrow2 = UBound(arr2)<br/>ReDim arr3(1 To mrow1 + mrow2, 1 To 1)<br/>For y = 1 To UBound(arr2)<br/>&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp; If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>Next x<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr2(y, 1)<br/>100:<br/>Next y<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub</p><p>Sub 唯一的编号字典方法()<br/>'Set d = CreateObject("Scripting.Dictionary")<br/>Dim d As New Dictionary<br/>Dim arr1, arr2, x As Integer, y As Integer, t<br/>t = Timer<br/>Range("d2:d" &amp; Range("d65536").End(xlUp).Row) = ""<br/>arr1 = Range("a2:b11")<br/>arr2 = Range("b2:b11")<br/>mrow1 = UBound(arr1)<br/>mrow2 = UBound(arr2)<br/>ReDim arr3(1 To mrow1 + mrow2, 1 To 1)<br/>For x = 1 To mrow1<br/>&nbsp; d(arr1(x, 1)) = ""<br/>Next x<br/>For y = 1 To mrow2<br/>&nbsp; d(arr2(y, 1)) = ""<br/>Next y<br/>Range("d2").Resize(d.Count, 1) = Application.Transpose(d.Keys)<br/>MsgBox Timer - t<br/>End Sub<br/></p>
[此贴子已经被作者于2008-1-17 17:36:58编辑过]
回复

使用道具 举报

发表于 2008-1-9 22:53 | 显示全部楼层

<p>rivate Sub CommandButton1_Click()&nbsp;&nbsp;&nbsp; '物品2有1没<br/>Dim arr1(), arr2(), arr3(), temp<br/>Dim i, r As Integer<br/>arr1 = WorksheetFunction.Transpose(Range("a2:a11"))<br/>arr2 = WorksheetFunction.Transpose(Range("b2:b11"))<br/>For i = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; temp = Filter(arr1, arr2(i))<br/>&nbsp;&nbsp;&nbsp; If UBound(temp) = -1 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = r + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReDim Preserve arr3(1 To r)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(r) = arr2(i)<br/>&nbsp;&nbsp;&nbsp; End If<br/>Next<br/>Range("f2").Resize(r, 1) = WorksheetFunction.Transpose(arr3)<br/>End Sub</p><p>rivate Sub CommandButton2_Click()&nbsp;&nbsp; '物品1有2没<br/>Dim arr1(), arr2(), arr3(), temp<br/>Dim i, r As Integer<br/>arr1 = Application.Transpose(Range("a2:a11"))<br/>arr2 = Application.Transpose(Range("b2:b11"))<br/>For i = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; temp = Filter(arr2, arr1(i))<br/>&nbsp;&nbsp;&nbsp; If UBound(temp) = -1 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = r + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReDim Preserve arr3(1 To r)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(r) = arr1(i)<br/>&nbsp;&nbsp;&nbsp; End If<br/>Next<br/>Range("E2").Resize(UBound(arr3)) = Application.Transpose(arr3)<br/>End Sub</p><p>rivate Sub CommandButton3_Click()&nbsp;&nbsp; '唯一编号<br/>Dim arr1(), arr2(), arr3(), temp<br/>Dim i, r As Integer<br/>arr1 = WorksheetFunction.Transpose(Range("a2:a11"))<br/>arr2 = WorksheetFunction.Transpose(Range("b2:b11"))<br/>arr3 = arr1<br/>r = UBound(arr3)<br/>For i = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; temp = Filter(arr1, arr2(i))<br/>&nbsp;&nbsp;&nbsp; If UBound(temp) = -1 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = r + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReDim Preserve arr3(1 To r)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(r) = arr2(i)<br/>&nbsp;&nbsp;&nbsp; End If<br/>Next<br/>Range("d2").Resize(r, 1) = WorksheetFunction.Transpose(arr3)<br/>End Sub</p><p></p>
回复

使用道具 举报

发表于 2008-1-9 23:02 | 显示全部楼层

Sub cn()<br/>&nbsp;&nbsp;&nbsp; Dim arr1(), arr2(), arr3(), arr4(), arr5()<br/>&nbsp;&nbsp;&nbsp; t = Timer<br/>&nbsp;&nbsp;&nbsp; Range(Range("D2"), Range("F" &amp; Range("d65536").End(xlUp).Row + 1)).ClearContents<br/>&nbsp;&nbsp;&nbsp; k = 1<br/>&nbsp;&nbsp;&nbsp; l = 1<br/>&nbsp;&nbsp;&nbsp; m = 1<br/>&nbsp;&nbsp;&nbsp; x = Range("a65536").End(xlUp).Row<br/>&nbsp;&nbsp;&nbsp; y = Range("b65536").End(xlUp).Row<br/>&nbsp;&nbsp;&nbsp; arr1 = Range("A2:A" &amp; x)<br/>&nbsp;&nbsp;&nbsp; arr2 = Range("B2:B" &amp; y)<br/>&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; ReDim arr4(1 To UBound(arr2), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; ReDim arr5(1 To UBound(arr2), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; For i = 1 To x - 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For j = 1 To y - 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(i, 1) = arr2(j, 1) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr5(m, 1) = arr1(i, 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr1(i, 1) = ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr2(j, 1) = ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; m = m + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit For<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next j<br/>&nbsp;&nbsp;&nbsp; Next i<br/>&nbsp;&nbsp;&nbsp; For i = 1 To x - 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(i, 1) &lt;&gt; "" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(i, 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr2(i, 1) &lt;&gt; "" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr4(l, 1) = arr2(i, 1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; l = l + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; Sheet1.Range("D2").Resize(k - 1, 1) = arr3<br/>&nbsp;&nbsp;&nbsp; Sheet1.Range("D2").Offset(k - 1, 0).Resize(m - 1, 1) = arr5<br/>&nbsp;&nbsp;&nbsp; Sheet1.Range("D2").Offset(k + m - 2, 0).Resize(l - 1, 1) = arr4<br/>&nbsp;&nbsp;&nbsp; Sheet1.Range("e2").Resize(k - 1, 1) = arr3<br/>&nbsp;&nbsp;&nbsp; Sheet1.Range("f2").Resize(l - 1, 1) = arr4<br/>&nbsp;&nbsp;&nbsp; MsgBox Timer - t<br/>End Sub
[此贴子已经被作者于2008-1-9 23:02:06编辑过]
回复

使用道具 举报

发表于 2008-1-9 23:07 | 显示全部楼层

<p>Sub 物品1有()<br/>Range("e2:e" &amp; Range("e65536").End(xlUp).Row) = ""<br/>Dim arr1, arr2, ar3(), i, y, z, k As Integer<br/>arr1 = Range("a2:a" &amp; Range("b65536").End(xlUp).Row)<br/>arr2 = Range("b2:b" &amp; Range("b65536").End(xlUp).Row)<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For y = 1 To UBound(arr1)<br/>&nbsp; For z = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp; If arr1(y, 1) = arr2(z, 1) Then GoTo 100<br/>&nbsp; Next<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr1(y, 1)<br/>100:<br/>&nbsp; Next y<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub</p><p><br/>Sub 物品2有()<br/>Range("f2:f" &amp; Range("f65536").End(xlUp).Row) = ""<br/>Dim arr1, arr2, ar3(), i, y, z, k As Integer<br/>arr1 = Range("a2:a" &amp; Range("b65536").End(xlUp).Row)<br/>arr2 = Range("b2:b" &amp; Range("b65536").End(xlUp).Row)<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For y = 1 To UBound(arr2)<br/>&nbsp; For z = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; If arr2(y, 1) = arr1(z, 1) Then GoTo 100<br/>&nbsp; Next<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr2(y, 1)<br/>100:<br/>&nbsp; Next y<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub<br/></p>
[此贴子已经被作者于2008-1-9 23:12:08编辑过]
回复

使用道具 举报

发表于 2008-1-9 23:08 | 显示全部楼层

<p>Sub 物品1有()<br/>&nbsp;Dim d As New Dictionary<br/>&nbsp;Dim arr1, arr2<br/>&nbsp;arr1 = Range("a2:a11")<br/>&nbsp;arr2 = Range("b2:b11")<br/>&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp; d(arr1(x, 1)) = ""<br/>&nbsp; Next x<br/>&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp; If d.Exists(arr2(y, 1)) Then d.Remove (arr2(y, 1))<br/>&nbsp; Next y<br/>&nbsp; Range("e2").Resize(d.Count) = Application.Transpose(d.Keys)<br/>End Sub</p><p>Sub 物品2有()<br/>&nbsp;Dim d As New Dictionary<br/>&nbsp;Dim arr1, arr2<br/>&nbsp;arr1 = Range("a2:a11")<br/>&nbsp;arr2 = Range("b2:b11")<br/>&nbsp;For x = 1 To UBound(arr2)<br/>&nbsp; d(arr2(x, 1)) = ""<br/>&nbsp;Next x<br/>&nbsp;For y = 1 To UBound(arr1)<br/>&nbsp; If d.Exists(arr1(y, 1)) Then d.Remove (arr1(y, 1))<br/>&nbsp;Next y<br/>&nbsp;Range("f2").Resize(d.Count) = Application.Transpose(d.Keys)<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-9 23:22 | 显示全部楼层

<p>我想学习,请问兰老师在哪里能上课呀。</p>
回复

使用道具 举报

发表于 2008-1-9 23:30 | 显示全部楼层

<p>Sub 物品1有物品2没有()<br/>Range("e2:e" &amp; Range("e2").End(xlDown).Row) = ""<br/>Dim arr1, arr2, x%, y%<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>For x = 1 To UBound(arr1)<br/>For y = 1 To UBound(arr2)<br/>If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>Next y<br/>Range("E" &amp; Range("E65536").End(xlUp).Row).Offset(1, 0) = arr1(x, 1)<br/>100:<br/>Next x<br/>End Sub</p><p><br/>Sub 物品2有物品1没有()<br/>Range("f2:f" &amp; Range("f2").End(xlDown).Row) = ""<br/>Dim arr1, arr2, y%, x%<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>For y = 1 To UBound(arr2)<br/>For x = 1 To UBound(arr1)<br/>If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>Next x<br/>Range("f" &amp; Range("f65536").End(xlUp).Row).Offset(1, 0) = arr2(y, 1)<br/>100:<br/>Next y<br/>End Sub</p>
[此贴子已经被作者于2008-1-9 23:34:18编辑过]
回复

使用道具 举报

发表于 2008-1-9 23:43 | 显示全部楼层

<p>7楼以上的高手动作好快啊!![em17][em17]</p><p>我还得好好琢磨琢磨才行!</p><p>第8题后2问的答案贴在下面:</p><p>Sub 物品1有2没有() <font color="#48a76e">'物品1有物品2没有<br/></font>Range("e2:e" &amp; Range("e65536").End(xlUp).Row) = ""<br/>&nbsp;Dim arr1, arr2, arr3(), k%, y%, z%<br/>&nbsp;arr1 = Range("a2:a11")<br/>&nbsp;arr2 = Range("b2:b11")<br/>&nbsp;ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; For z = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(z, 1) = arr2(y, 1) Then GoTo l:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(z, 1)<br/>l:<br/>&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp;&nbsp; Range("e2").Resize(k, 1) = arr3<br/>End Sub</p><p>Sub 物品2有1没有() <font color="#439b89">'物品2有物品1没有</font><br/>Range("f2:f" &amp; Range("f65536").End(xlUp).Row) = ""<br/>&nbsp;Dim arr1, arr2, arr3(), k%, y%, z%<br/>&nbsp;arr1 = Range("a2:a11")<br/>&nbsp;arr2 = Range("b2:b11")<br/>&nbsp;ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For z = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr2(y, 1) = arr1(z, 1) Then GoTo l:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(y, 1)<br/>l:<br/>&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; Range("f2").Resize(k, 1) = arr3<br/>End Sub</p><p>&nbsp;</p>
[此贴子已经被作者于2008-1-11 14:17:45编辑过]
回复

使用道具 举报

发表于 2008-1-9 23:56 | 显示全部楼层

<p>哇,已经这么多人了!</p><p>VBA入门班<font size="6">2组</font>:<font color="#0909f7" size="5">zgwei050</font>交作业:</p><p><font color="#ff3300">Sub 查找唯一()</font><br/>Dim arr1, arr2, arr3()<br/>Dim x As Integer, y As Integer, z As Integer, k As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Range("d2:d65536") = ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr1 = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr2 = Range("B2:b" &amp; Range("B65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x, 1) = arr1(x, 1)<br/>&nbsp;&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp; For z = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr2(z, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x + k - 1, 1) = arr2(z, 1)<br/>100:<br/>&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp;&nbsp; Range("D2").Resize(x + k, 1) = arr3<br/>End Sub</p><p><br/><font color="#ff3300">Sub 物品1有物品2没有()<br/></font>Dim arr1, arr2, arr3()<br/>Dim x As Integer, y As Integer, z As Integer, k As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Range("E2:E65536") = ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr1 = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr2 = Range("B2:b" &amp; Range("B65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x, 1) = arr1(x, 1)<br/>&nbsp;&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp; For z = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr2(y, 1) = arr1(z, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(z, 1)<br/>100:<br/>&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp;&nbsp; Range("E2").Resize(k, 1) = arr3<br/>End Sub</p><p><br/><font color="#ff3300">Sub 物品2有物品1没有()</font><br/>Dim arr1, arr2, arr3()<br/>Dim x As Integer, y As Integer, z As Integer, k As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Range("F2:F65536") = ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr1 = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr2 = Range("B2:b" &amp; Range("B65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x, 1) = arr1(x, 1)<br/>&nbsp;&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp; For z = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(y, 1) = arr2(z, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(z, 1)<br/>100:<br/>&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp;&nbsp; Range("F2").Resize(k, 1) = arr3<br/>End Sub</p><p><br/><font color="#ff3300">Sub 物品1有物品2也有()</font><br/>Dim arr1, arr2, arr3()<br/>Dim x As Integer, y As Integer, z As Integer, k As Integer<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Range("G2:G65536") = ""<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr1 = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr2 = Range("B2:b" &amp; Range("B65536").End(xlUp).Row)<br/>&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x, 1) = arr1(x, 1)<br/>&nbsp;&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp; For z = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(y, 1) &lt;&gt; arr2(z, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(z, 1)<br/>100:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp;&nbsp; Range("G2").Resize(k, 1) = arr3<br/>End Sub</p><p><br/>&nbsp;</p> TvHYzLHs.rar (10.21 KB, 下载次数: 8)
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-11-11 03:40 , Processed in 0.349197 second(s), 6 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表