Excel精英培训网

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

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

[复制链接]
发表于 2008-1-13 18:32 | 显示全部楼层

Sub 物品1有物品2没有()<br/>Dim arr, arr1, arr2, x As Integer, y As Integer, k As Integer<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) '考虑编号1与编号2物品不一样多<br/>For x = 1 To UBound(arr)<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr(x, 1) = arr1(y, 1) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; arr2(k, 1) = arr(x, 1)<br/>100:<br/>Next x<br/>Range("E2").Resize(UBound(arr2), 1) = arr2<br/>End Sub<br/>Sub 物品2有物品1没有()<br/>Dim arr, arr1, arr2, x As Integer, y As Integer, k As Integer<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) '考虑编号1与编号2物品不一样多<br/>For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr(y, 1) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; arr2(k, 1) = arr1(x, 1)<br/>100:<br/>Next x<br/>Range("F2").Resize(UBound(arr2), 1) = arr2<br/>End Sub<br/>
回复

使用道具 举报

发表于 2008-1-13 18:38 | 显示全部楼层

在大家的帮助下,完成了。<br/>Sub 物品1有物品2没有()<br/>Dim arr1, arr2, arr3(), x, y, row1, row2, k, i, t<br/>t = Timer<br/>Range("e2:e" &amp; Range("d65536").End(xlUp).Row + 1) = ""&nbsp;此行应加1,否则第一次运行时表头被清空<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>row1 = UBound(arr1)<br/>row2 = UBound(arr2)<br/>ReDim arr3(1 To row1 + row2, 1 To 1)<br/>&nbsp; For x = 1 To row1<br/>&nbsp;&nbsp; For y = 1 To row2<br/>&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>&nbsp; Next x<br/>Range("e2").Resize(k, 1) = arr3<br/>MsgBox Timer - t<br/>End Sub<br/>Sub 物品2有物品1没有()<br/>Dim arr1, arr2, arr3(), x, y, row1, row2, k, i, t<br/>t = Timer<br/>Range("f2:f" &amp; Range("d65536").End(xlUp).Row + 1) = "" 此行应加1,否则第一次运行时表头被清空<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>row1 = UBound(arr1)<br/>row2 = UBound(arr2)<br/>ReDim arr3(1 To row1 + row2, 1 To 1)<br/>&nbsp; For y = 1 To row2<br/>&nbsp;&nbsp; For x = 1 To row1<br/>&nbsp;&nbsp;&nbsp; If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next x<br/>&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(y, 1)<br/>100:<br/>&nbsp; Next y<br/>Range("f2").Resize(k, 1) = arr3<br/>MsgBox Timer - t<br/>End Sub<br/>
[此贴子已经被作者于2008-1-13 18:40:17编辑过]
回复

使用道具 举报

发表于 2008-1-13 20:02 | 显示全部楼层

<p>Sub 唯一值()<br/>Dim x, y, z<br/>Dim arr1, arr2, arr3<br/>Range("d2:D" &amp; Range("D65536").End(xlUp).Row + 1) = ""</p><p>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>ReDim arr3(1 To UBound(arr1, 1) + UBound(arr2, 1), 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; arr3(x, 1) = arr1(x, 1)<br/>Next x<br/>For z = 1 To UBound(arr2, 1)<br/>For y = 1 To UBound(arr1, 1)<br/>&nbsp;&nbsp; If arr2(z, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x + k - 1, 1) = arr2(z, 1)<br/>100:<br/>Next z<br/>Range("d2").Resize(k + x) = arr3</p><p>End Sub</p><p>----------------------------------------------------------------------------------------<br/>Sub 物品1有物品2没有()<br/>Dim x, y, z<br/>Dim arr1, arr2, arr3<br/>Range("E2:E" &amp; Range("E65536").End(xlUp).Row + 1) = ""</p><p>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>ReDim arr3(1 To UBound(arr1, 1), 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>For y = 1 To UBound(arr2, 1)<br/>&nbsp;&nbsp; If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>Next x<br/>Range("E2").Resize(k) = arr3</p><p>End Sub</p><p>--------------------------------------------------------------------------------------------------</p><p>Sub 物品2有物品1没有()<br/>Dim x, y, z<br/>Dim arr1, arr2, arr3<br/>Range("F2:F" &amp; Range("E65536").End(xlUp).Row + 1) = ""</p><p>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>ReDim arr3(1 To UBound(arr2, 1), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>For y = 1 To UBound(arr1, 1)<br/>&nbsp;&nbsp; If arr1(y, 1) = arr2(x, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(x, 1)<br/>100:<br/>Next x<br/>Range("F2").Resize(k) = arr3</p><p>End Sub</p>
[此贴子已经被作者于2008-1-13 21:28:16编辑过]
回复

使用道具 举报

发表于 2008-1-13 20:14 | 显示全部楼层

<p><br/>Sub 唯一值字典方法()<br/>t = Timer()<br/>Range("d2:D" &amp; Range("D65536").End(xlUp).Row + 1) = ""<br/>Set d = CreateObject("Scripting.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/>&nbsp;&nbsp;&nbsp; d(arr1(x, 1)) = ""<br/>Next x<br/>For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp; d(arr2(y, 1)) = ""<br/>Next y<br/>Range("d2").Resize(d.Count) = Application.Transpose(d.Keys)<br/>MsgBox Timer() - t<br/>End Sub</p><p>---------------------------------------------------------------------------------------<br/>Sub 物品1有2无字典方法()<br/>t = Timer()<br/>Range("E2:E" &amp; Range("D65536").End(xlUp).Row + 1) = ""<br/>Set d = CreateObject("Scripting.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/>For y = 1 To UBound(arr2, 1)<br/>&nbsp;&nbsp; If arr2(y, 1) = arr1(x, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; d(arr1(x, 1)) = ""<br/>100:<br/>Next x<br/>Range("E2").Resize(d.Count) = Application.Transpose(d.Keys)<br/>MsgBox Timer() - t<br/>End Sub</p><p>-----------------------------------------------------------------------------------------------------------</p><p>Sub 物品2有1无字典方法()<br/>t = Timer()<br/>Range("F2:F" &amp; Range("D65536").End(xlUp).Row + 1) = ""<br/>Set d = CreateObject("Scripting.Dictionary")<br/>Dim arr1, arr2, x As Integer<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>For x = 1 To UBound(arr2)<br/>For y = 1 To UBound(arr1, 1)<br/>&nbsp;&nbsp; If arr1(y, 1) = arr2(x, 1) Then GoTo 100<br/>&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; d(arr2(x, 1)) = ""<br/>100:<br/>Next x<br/>Range("F2").Resize(d.Count) = Application.Transpose(d.Keys)<br/>MsgBox Timer() - t<br/>End Sub</p>
[此贴子已经被作者于2008-1-13 21:27:19编辑过]
回复

使用道具 举报

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

<p>Sub b()<br/>Dim arr1, arr2, arr3(), x As Integer, y As Integer<br/>Range("e2:e" &amp; Range("a65536").End(xlUp).Row) = ""<br/>arr1 = Range("a2:a20")<br/>arr2 = Range("b2:b20")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>arr3(x, 1) = arr1(x, 1)<br/>Next x<br/>For z = 1 To UBound(arr2)<br/>For y = 1 To UBound(arr1)<br/>If arr1(z, 1) = arr2(y, 1) Then GoTo 100<br/>Next y<br/>k = k + 1<br/>arr3(k, 1) = arr1(z, 1)<br/>100:<br/>Next z<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub</p><p>Sub bc()<br/>Dim arr1, arr2, arr3(), x As Integer, y As Integer<br/>Range("e2:e" &amp; Range("a65536").End(xlUp).Row) = ""<br/>arr1 = Range("a2:a20")<br/>arr2 = Range("b2:b20")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr1)<br/>arr3(x, 1) = arr2(x, 1)<br/>Next x<br/>For z = 1 To UBound(arr2)<br/>For y = 1 To UBound(arr1)<br/>If arr2(z, 1) = arr1(y, 1) Then GoTo 100<br/>Next y<br/>k = k + 1<br/>arr3(k, 1) = arr2(z, 1)<br/>100:<br/>Next z<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub</p>
回复

使用道具 举报

发表于 2008-1-13 22:00 | 显示全部楼层

<p>Sub 物品1有物品2没有()<br/>Dim x%, y%, k%<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/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>Next x<br/>Range("E2").Resize(k, 1) = arr3<br/>End Sub<br/></p><p>Sub 物品2有物品1没有()<br/>Dim x%, y%, k%<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 x = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp; For y = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr2(x, 1) = arr1(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(x, 1)<br/>100:<br/>Next x<br/>Range("F2").Resize(k, 1) = arr3<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-14 17:36 | 显示全部楼层

Sub 唯一值()<br/>t = Timer()<br/>Dim x, y, z<br/>Dim arr1, arr2, arr3<br/>Range("d2:D" &amp; Range("D65536").End(xlUp).Row + 1) = ""<br/>arr1 = Range("A2:A11")<br/>arr2 = Range("B2:B11")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2))<br/>For x = 1 To UBound(arr1)<br/>&nbsp;&nbsp;&nbsp; arr3(x) = arr1(x, 1)<br/>Next x<br/>For y = 1 To UBound(arr2, 1)<br/>If UBound(Filter(arr3, arr2(y, 1))) &lt; 0 Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(x + k - 1) = arr2(y, 1)<br/>End If<br/>Next y<br/>Range("d2").Resize(x + k - 1) = Application.Transpose(arr3)<br/>MsgBox Timer() - t<br/>End Sub
回复

使用道具 举报

发表于 2008-1-14 20:03 | 显示全部楼层

<p><strong><font color="#b3764d" size="5">11组 yss5178</font></strong><br/>Sub 唯一值()<br/>k = 1<br/>Range("d2:d" &amp; Range("d65536").End(xlUp).Row) = ""<br/>For y = 1 To 2<br/>&nbsp;For x = 2 To 11<br/>&nbsp;l = Application.WorksheetFunction.CountIf(Range("a2:b11"), Cells(x, y))<br/>&nbsp;If l = 1 Then<br/>&nbsp;k = k + 1<br/>&nbsp;Cells(k, 4) = Cells(x, y)<br/>&nbsp;End If<br/>&nbsp;Next x<br/>&nbsp;Next y<br/>End Sub</p><p>Sub 物品1有物品2没有()<br/>j = 1<br/>Range("e2:e20") = ""<br/>For y = 1 To 2<br/>&nbsp;For x = 2 To 11<br/>&nbsp;l = Application.WorksheetFunction.CountIf(Range("a2:a11"), Cells(x, y))<br/>&nbsp;m = Application.WorksheetFunction.CountIf(Range("b2:b11"), Cells(x, y))<br/>&nbsp;If l &gt;= 1 And m = 0 Then<br/>&nbsp;j = j + 1<br/>&nbsp;Cells(j, 5) = Cells(x, y)<br/>&nbsp;End If<br/>&nbsp;Next x<br/>&nbsp;Next y<br/>End Sub</p><p><br/>Sub 物品2有物品1没有()<br/>j = 1<br/>Range("f2:f20") = ""<br/>For y = 1 To 2<br/>&nbsp;For x = 2 To 11<br/>&nbsp;l = Application.WorksheetFunction.CountIf(Range("a2:a11"), Cells(x, y))<br/>&nbsp;m = Application.WorksheetFunction.CountIf(Range("b2:b11"), Cells(x, y))<br/>&nbsp;If m &gt;= 1 And l = 0 Then<br/>&nbsp;j = j + 1<br/>&nbsp;Cells(j, 6) = Cells(x, y)<br/>&nbsp;End If<br/>&nbsp;Next x<br/>&nbsp;Next y<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-14 20:33 | 显示全部楼层

<p>Sub 物品1有2没有()<br/>Dim ARR1, ARR2, ARR3, X%, Y%<br/>ARR1 = Range("A2:A11")<br/>ARR2 = Range("B2:B11")<br/>ReDim ARR3(1 To UBound(ARR1) + UBound(ARR2), 1 To 1)<br/>&nbsp; For X = 1 To UBound(ARR1)<br/>&nbsp; For Y = 1 To UBound(ARR2)<br/>&nbsp;&nbsp;&nbsp; If ARR1(X, 1) = ARR2(Y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next Y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; K = K + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ARR3(K, 1) = ARR1(X, 1)<br/>100:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next X<br/>&nbsp;&nbsp;&nbsp; Range("E2").Resize(K) = ARR3<br/>End Sub</p><p>Sub 物品2有1没有()<br/>Dim ARR1, ARR2, ARR3, X%, Y%<br/>ARR1 = Range("A2:A11")<br/>ARR2 = Range("B2:B11")<br/>ReDim ARR3(X 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;&nbsp; If ARR2(X, 1) = ARR1(Y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next Y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; K = K + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ARR3(K, 1) = ARR2(X, 1)<br/>100:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next X<br/>&nbsp;&nbsp;&nbsp; Range("F2").Resize(K) = ARR3</p><p>End Sub<br/>物品2有1没有:好像有点问题不明,请老师指点。</p><p>ReDim ARR3(X To UBound(ARR1) + UBound(ARR2), 1 To 1)这句 "X" 的位置放1时A7会上去,换上"X"没A7了,可是,是在F3的位置填入。不明白怎么回事?</p>
回复

使用道具 举报

发表于 2008-1-14 21:23 | 显示全部楼层

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-26 09:22 , Processed in 0.250139 second(s), 3 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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