Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
楼主: 兰色幻想

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

[复制链接]
发表于 2008-1-10 15:47 | 显示全部楼层

<p>Sub test1()<br/>Range("e2:e" &amp; Range("e65536").End(xlUp).Row) = ""<br/>&nbsp;Dim arr1, arr2, arr3()<br/>&nbsp;Dim x, y, k As Integer<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;For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>&nbsp; Next x<br/>&nbsp;Range("e2").Resize(k) = arr3<br/>End Sub<br/></p><p>Sub test2()<br/>Range("f2:f" &amp; Range("f65536").End(xlUp).Row) = ""<br/>&nbsp;Dim arr1, arr2, arr3()<br/>&nbsp;Dim x, y, k As Integer<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;For x = 1 To UBound(arr2)<br/>&nbsp;&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp; If arr2(x, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp; arr3(k, 1) = arr2(x, 1)<br/>100:<br/>&nbsp; Next x<br/>&nbsp;Range("f2").Resize(k) = arr3<br/>End Sub</p><p>其实把取唯一值的改一下就可以实现了吧!</p>
[此贴子已经被作者于2008-1-10 16:02:04编辑过]
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2008-1-10 16:26 | 显示全部楼层

<p>Sub 唯一的编号()<br/>&nbsp;Dim x%, y%, arr, arr1(), i%, k%<br/>&nbsp; arr = Range("a2:b" &amp; Range("b65536").End(xlUp).Row)<br/>&nbsp; ReDim arr1(1 To UBound(arr) * 2, 1 To 1)<br/>&nbsp; arr1(1, 1) = arr(1, 1)<br/>&nbsp; k = k + 1<br/>&nbsp;&nbsp; For x = 1 To 2<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr)<br/>&nbsp;&nbsp;&nbsp;&nbsp; For i = 1 To k<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr(y, x) = arr1(i, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp; Next i<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr1(k, 1) = arr(y, x)<br/>100:<br/>&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; Next x<br/>Range("d2:d" &amp; UBound(arr1) + 1) = arr1<br/>End Sub</p><p></p><p><br/>Sub 物1有物2无()<br/>Dim arr, arr1, arr2(), x%, y%, k%<br/>arr = Range("a2:a" &amp; Range("a65536").End(xlUp).Row)<br/>arr1 = Range("b2:b" &amp; Range("b65536").End(xlUp).Row)<br/>ReDim arr2(1 To UBound(arr) + UBound(arr1), 1 To 1)<br/>For x = 1 To UBound(arr)<br/>&nbsp;For y = 1 To UBound(arr1)<br/>&nbsp; If arr(x, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;Next y<br/>&nbsp; k = k + 1<br/>&nbsp; arr2(k, 1) = arr(x, 1)<br/>100:<br/>Next x<br/>Range("e2").Resize(k) = arr2<br/>End Sub</p><p></p><p><br/>Sub 物2有物1无()<br/>Dim arr1, arr2, arr3(), x%, y%, k%<br/>&nbsp;arr1 = Range("a2:a" &amp; Range("a65536").End(xlUp).Row)<br/>&nbsp;arr2 = Range("b2:b" &amp; Range(b65536).End(xlUp).Row)<br/>&nbsp;ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>&nbsp;For x = 1 To UBound(arr2)<br/>&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp; If arr2(x, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp; arr3(k, 1) = arr2(x, 1)<br/>100:<br/>Next x<br/>Range("f2").Resize(k) = arr3<br/>End Sub<br/></p>
[此贴子已经被作者于2008-1-10 16:26:30编辑过]
回复

使用道具 举报

发表于 2008-1-10 17:18 | 显示全部楼层

<p>Sub weiyi()<br/>Dim arr1, arr2, arr3<br/>Dim x As Long, y As Long, z As Long, t As Long<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For t = 1 To UBound(arr1)<br/>燼rr3(t, 1) = arr1(t, 1)<br/>Next t<br/>For x = 1 To UBound(arr2)<br/>?For y = 1 To UBound(arr1)<br/>牋 If arr2(x, 1) = arr1(y, 1) Then<br/>牋牋 GoTo 100<br/>牋 End If<br/>?Next y<br/>?z = z + 1<br/>?arr3(t + z - 1, 1) = arr2(x, 1)<br/>100:<br/>Next x<br/>Range("d2").Resize(x - 1 + z) = arr3<br/>End Sub<br/>Sub wp1()<br/>Dim arr1, arr2, arr3<br/>Dim x As Long, y As Long, z As Long<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1), 1 To 1)<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/>?z = z + 1<br/>?arr3(z, 1) = arr1(x, 1)<br/>100:<br/>Next x<br/>Range("e2").Resize(UBound(arr3), 1) = arr3<br/>End Sub<br/>Sub wp2()<br/>Dim arr1, arr2, arr3<br/>Dim x As Long, y As Long, z As Long<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>?For y = 1 To UBound(arr1)<br/>牋?If arr2(x, 1) = arr1(y, 1) Then GoTo 100<br/>?Next y<br/>?z = z + 1<br/>?arr3(z, 1) = arr2(x, 1)<br/>100:<br/>Next x<br/>Range("f2").Resize(UBound(arr3), 1) = arr3<br/>End Sub</p><p></p>
回复

使用道具 举报

发表于 2008-1-10 18:03 | 显示全部楼层

Sub OKOKOK一有()<br/>Dim arr1, arr2, arr3()<br/>Range("e2").Resize(Range("e65536").End(xlUp).Row - 1, 1) = ""<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>&#160;For y = 1 To UBound(arr2)<br/>&#160;If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&#160;Next y<br/>&#160;k = k + 1<br/>&#160;arr3(k, 1) = arr1(x, 1)<br/>100:<br/>Next x<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub<br/><br/><br/><p>---------------------------------------------------------<br/></p><p>Sub OKOKOK二有()<br/>Dim arr1, arr2, arr3()<br/>Range("f2").Resize(Range("e65536").End(xlUp).Row - 1, 1) = ""<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>&#160;For y = 1 To UBound(arr1)<br/>&#160;If arr2(x, 1) = arr1(y, 1) Then GoTo 100<br/>&#160;Next y<br/>&#160;k = k + 1<br/>&#160;arr3(k, 1) = arr2(x, 1)<br/>100:<br/>Next x<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub<br/><br/></p>
回复

使用道具 举报

发表于 2008-1-10 18:27 | 显示全部楼层

<p>Sub 唯一的编号()<br/>t = Timer<br/>Range("D2:D" &amp; Range("D65536").End(xlUp).Row) = ""<br/>Dim arr1(), arr2(), arr3()<br/>arr1() = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>arr2() = Range("B2:B" &amp; Range("B65536").End(xlUp).Row)<br/>k = 0<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For i = 1 To UBound(arr1)<br/>&nbsp; arr3(i, 1) = arr1(i, 1)<br/>Next<br/>For x = 1 To UBound(arr2)<br/>For y = 1 To UBound(arr1)<br/>If arr1(y, 1) = arr2(x, 1) Then GoTo 100<br/>Next y<br/>arr3(i + k, 1) = arr2(x, 1)<br/>k = k + 1<br/>100:<br/>Next x<br/>Range("D2").Resize(i + k - 1, 1) = arr3<br/>MsgBox Timer - t<br/>End Sub<br/>Sub 物品1有物品2没有()</p><p>t = Timer<br/>Range("E2:E" &amp; Range("E65536").End(xlUp).Row) = ""<br/>Dim arr1(), arr2(), arr3()<br/>arr1() = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>arr2() = Range("B2:B" &amp; Range("B65536").End(xlUp).Row)<br/>k = 1<br/>ReDim arr3(1 To UBound(arr1), 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>For y = 1 To UBound(arr2)<br/>If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>Next y<br/>arr3(k, 1) = arr1(x, 1)<br/>k = k + 1<br/>100:<br/>Next x<br/>Range("E2").Resize(k - 1, 1) = arr3<br/>MsgBox Timer - t<br/>End Sub</p><p>Sub 物品2有物品1没有()<br/>t = Timer<br/>Range("F2:F" &amp; Range("F65536").End(xlUp).Row) = ""<br/>Dim arr1(), arr2(), arr3()<br/>arr1() = Range("A2:A" &amp; Range("A65536").End(xlUp).Row)<br/>arr2() = Range("B2:B" &amp; Range("B65536").End(xlUp).Row)<br/>k = 1<br/>ReDim arr3(1 To UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>For y = 1 To UBound(arr1)<br/>If arr1(y, 1) = arr2(x, 1) Then GoTo 100<br/>Next y<br/>arr3(k, 1) = arr2(x, 1)<br/>k = k + 1<br/>100:<br/>Next x<br/>Range("F2").Resize(k - 1, 1) = arr3<br/>MsgBox Timer - t<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-10 18:55 | 显示全部楼层

<p><br/>Sub 唯一编号()<br/>&nbsp;Dim arr1, arr2, arr3()<br/>&nbsp;Dim x, y, z, k<br/>&nbsp;Range("d2:d65536").ClearContents<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;<br/>&nbsp;For x = 1 To UBound(arr1)<br/>&nbsp;arr3(x, 1) = arr1(x, 1)<br/>&nbsp;Next<br/>&nbsp;<br/>&nbsp;For y = 1 To UBound(arr2)<br/>&nbsp; For z = 1 To UBound(arr1)<br/>&nbsp;&nbsp; If arr3(z, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp; Next z<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp; arr3(k + x - 1, 1) = arr2(y, 1)<br/>100<br/>&nbsp; Next y<br/>&nbsp; <br/>&nbsp; Range("d2").Resize(k + x - 1, 1) = arr3<br/>End Sub</p><p>Sub 物品2中仅有()<br/>&nbsp; Dim arr1, arr2, arr3()<br/>&nbsp; Dim x, y, z<br/>&nbsp; Range("f2:f65536").ClearContents<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; <br/>&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp; If arr2(x, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp; z = z + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp; arr3(z, 1) = arr2(x, 1)<br/>100<br/>&nbsp;&nbsp;&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; Range("f2").Resize(z, 1) = arr3<br/>End Sub</p><p>Sub 物品1中仅有()<br/>&nbsp; Dim arr1, arr2, arr3()<br/>&nbsp; Dim x, y, z<br/>&nbsp; Range("e2:e65536").ClearContents<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; <br/>&nbsp; For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp; z = z + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp; arr3(z, 1) = arr1(x, 1)<br/>100<br/>&nbsp;&nbsp;&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; Range("e2").Resize(z, 1) = arr3<br/>End Sub</p><p></p>
回复

使用道具 举报

发表于 2008-1-10 19:27 | 显示全部楼层

<div class="msgheader">QUOTE:</div><div class="msgborder"><b>以下是引用<i>hhzjxss</i>在2008-1-10 13:24:00的发言:</b><br/><p>原来至尊宝_98_76做了。</p><p>&nbsp; Dim d As New Dictionary, x, c, y</p><p>是不是能够一次可以申明几个字典呀?<br/></p></div><p>dim x,y,z,d as New Dictionary 这句定义只有d是字典变量,一次定义多个每个都要加as New Dictionary </p>
回复

使用道具 举报

发表于 2008-1-10 19:43 | 显示全部楼层

<p>用字典做:</p><p>Sub dict()&nbsp; '唯一的编号<br/>t = Timer<br/>Range("D2:D" &amp; Range("D65536").End(xlUp).Row) = ""<br/>Dim d As New Dictionary<br/>Dim arr1, arr2, x As Integer<br/>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>For x = 1 To UBound(arr1)<br/>d(arr1(x, 1)) = x<br/>Next<br/>For x = 1 To UBound(arr2)<br/>d(arr2(x, 1)) = x * 2<br/>Next<br/>Range("d1").Resize(d.Count) = Application.Transpose(d.Keys)<br/>MsgBox Timer - t<br/>End Sub</p><p><br/>Sub dict2()&nbsp; '物品2有物品1没有()<br/>t = Timer<br/>Range("F2:F" &amp; Range("F65536").End(xlUp).Row) = ""<br/>Dim d As New Dictionary<br/>Dim arr1, arr2, arr3, x As Integer<br/>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>For x = 1 To UBound(arr1)<br/>d(arr1(x, 1)) = x<br/>Next<br/>ReDim arr3(1 To UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>If Not d.Exists(arr2(x, 1)) Then<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr2(x, 1)<br/>End If<br/>Next<br/>Range("F2").Resize(k, 1) = arr3<br/>MsgBox Timer - t<br/>End Sub</p><p></p>
回复

使用道具 举报

发表于 2008-1-10 21:30 | 显示全部楼层

Sub 物品1有物品2没有()<br/>Range("e2:e65536") = ""<br/>Dim arr1, arr2, arr3()<br/>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<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; If arr2(z, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp; Next z<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr1(y, 1)<br/>100:<br/>Next y<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub<br/>Sub 物品2有物品1没有()<br/>Range("f2:f65536") = ""<br/>Dim arr1, arr2, arr3()<br/>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For z = 1 To UBound(arr2)<br/>&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp; If arr2(z, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp; Next y<br/>&nbsp; k = k + 1<br/>&nbsp; arr3(k, 1) = arr2(z, 1)<br/>100:<br/>Next z<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub<br/>
回复

使用道具 举报

发表于 2008-1-10 22:26 | 显示全部楼层

<p>sub ff4()<br/>Range("e2:e" &amp; Range("e65536").End(xlUp).Row) = ""<br/>Dim arr1, arr2, arr3()<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1), 1 To 1)<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/>k = k + 1<br/>arr3(k, 1) = arr1(x, 1)<br/>100:<br/>Next x<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub</p><p>Sub ff5()<br/>Range("f2:f" &amp; Range("f65536").End(xlUp).Row) = ""<br/>Dim arr1, arr2, arr3()<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr2), 1 To 1)<br/>For y = 1 To UBound(arr2)<br/>For x = 1 To UBound(arr1)<br/>If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>Next x<br/>k = k + 1<br/>arr3(k, 1) = arr2(y, 1)<br/>100:<br/>Next y<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub</p>
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-18 18:19 , Processed in 0.270851 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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