Excel精英培训网

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

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

[复制链接]
发表于 2008-1-14 22:07 | 显示全部楼层

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-14 22:09:13编辑过]
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2008-1-15 18:09 | 显示全部楼层

Sub 商品A有()<br/>Dim arr1, arr2, arr3, x As Integer, y As Integer<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr2) + 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 200<br/>Next y<br/>k = k + 1<br/>arr3(k, 1) = arr1(x, 1)<br/>200:<br/>Next x<br/>Range("e2").Resize(k, 1) = arr3<br/>End Sub<br/>Sub 商品B有()<br/>Dim arr1, arr2, arr3, x As Integer, y As Integer<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr2) + 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 200<br/>Next y<br/>k = k + 1<br/>arr3(k, 1) = arr2(x, 1)<br/>200:<br/>Next x<br/>Range("f2").Resize(k, 1) = arr3<br/>End Sub<br/>
回复

使用道具 举报

发表于 2008-1-15 18:52 | 显示全部楼层

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

使用道具 举报

发表于 2008-1-15 18:57 | 显示全部楼层

<p>没全用数组,速度慢点 。3问都放到一起了</p><p>Sub dddd()<br/>t = Timer<br/>Range("d2:f22").ClearContents<br/>Dim arr1<br/>Dim arr2<br/>Dim cell1 As Range, cell2 As Range, cell3 As Range<br/>Set cell1 = [d12]<br/>Set cell2 = [e2]<br/>Set cell3 = [f2]<br/>arr1 = Range("a2:a11")<br/>&nbsp;arr2 = Range("b2:b11")<br/>For i = 1 To 10<br/>&nbsp;&nbsp;&nbsp; For j = 1 To 10<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(i, 1) = arr2(j, 1) Then k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr2(i, 1) = arr1(j, 1) Then m = m + 1<br/>&nbsp;&nbsp;&nbsp; Next<br/>&nbsp;&nbsp;&nbsp; Cells(i + 1, 4) = arr1(i, 1)<br/>&nbsp;&nbsp;&nbsp; If k = 0 Then cell2 = arr1(i, 1):&nbsp; Set cell2 = cell2.Offset(1, 0)<br/>&nbsp;&nbsp;&nbsp; If m = 0 Then cell3 = arr2(i, 1):&nbsp;&nbsp; Set cell3 = cell3.Offset(1, 0)<br/>&nbsp;&nbsp;&nbsp; If m = 0 Then cell1 = arr2(i, 1):&nbsp;&nbsp; Set cell1 = cell1.Offset(1, 0)<br/>&nbsp;&nbsp;&nbsp; m = 0<br/>&nbsp;&nbsp;&nbsp; k = 0<br/>Next<br/>MsgBox Timer - t<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-15 20:04 | 显示全部楼层

唉,真有点跟不上了啊

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><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-15 23:59 | 显示全部楼层

<p><font color="#ff0000">Sub 物品1有物品2没有()<br/></font>&nbsp; Dim arr1, arr2, arr3<br/>&nbsp; Dim a, b, m<br/>&nbsp; m = 1<br/>&nbsp;&nbsp;&nbsp;&nbsp; arr1 = Range("a2:a11")<br/>&nbsp;&nbsp;&nbsp;&nbsp; arr2 = Range("b2:b11")<br/>&nbsp;&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)</p><p>&nbsp; For a = 1 To UBound(arr1)</p><p>&nbsp;&nbsp;&nbsp; For b = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(a, 1) = arr2(b, 1) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next b<br/>&nbsp;&nbsp;&nbsp; arr3(m, 1) = arr1(a, 1)<br/>&nbsp;&nbsp;&nbsp; m = m + 1<br/>100:<br/>&nbsp; Next a<br/>&nbsp; Range("E2").Resize(a) = arr3<br/>End Sub</p><p></p><p><br/><font color="#ff0000">Sub 物品2有物品1没有()<br/></font>&nbsp; Dim arr1, arr2, arr3<br/>&nbsp; Dim a, b, m<br/>&nbsp; m = 1<br/>&nbsp;&nbsp;&nbsp;&nbsp; arr1 = Range("b2:b11")<br/>&nbsp;&nbsp;&nbsp;&nbsp; arr2 = Range("a2:a11")<br/>&nbsp;&nbsp;&nbsp;&nbsp; ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)</p><p>&nbsp; For a = 1 To UBound(arr1)</p><p>&nbsp;&nbsp;&nbsp; For b = 1 To UBound(arr2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(a, 1) = arr2(b, 1) Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; Next b<br/>&nbsp;&nbsp;&nbsp; arr3(m, 1) = arr1(a, 1)<br/>&nbsp;&nbsp;&nbsp; m = m + 1<br/>100:<br/>&nbsp; Next a<br/>&nbsp; Range("F2").Resize(a) = arr3<br/>End Sub</p>
回复

使用道具 举报

发表于 2008-1-16 00:27 | 显示全部楼层

<p>Sub 一有二没有()<br/>&nbsp;Dim arr1, arr2, arr3(), x as Integer&nbsp;, y as Integer, k as Integer<br/>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;&nbsp; For y = 1 To UBound(arr2)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>&nbsp;Next x<br/>&nbsp; Range("e2").Resize(k) = arr3<br/>&nbsp;End Sub<br/>&nbsp;<br/>Sub 二有一没有()<br/>Dim arr1, arr2, arr3(), x as Integer, y as Integer, k as Integer<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>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;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(x, 1)<br/>100:<br/>&nbsp;&nbsp; Next x<br/>Range("f2").Resize(k) = arr3<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-16 08:30 | 显示全部楼层

<p>Sub 物品有_1()<br/>&nbsp;Dim dic 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; dic(arr1(x, 1)) = ""<br/>&nbsp; Next x<br/>&nbsp; For y = 1 To UBound(arr2)<br/>&nbsp;&nbsp; If dic.Exists(arr2(y, 1)) Then dic.Remove (arr2(y, 1))<br/>&nbsp; Next y<br/>&nbsp; Range("e2").Resize(dic.Count) = Application.Transpose(dic.Keys)<br/>End Sub</p><p>Sub 物品有_2()<br/>&nbsp;Dim dic 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; dic(arr2(x, 1)) = ""<br/>&nbsp;Next x<br/>&nbsp;For y = 1 To UBound(arr1)<br/>&nbsp; If dic.Exists(arr1(y, 1)) Then dic.Remove (arr1(y, 1))<br/>&nbsp;Next y<br/>&nbsp;Range("f2").Resize(dic.Count) = Application.Transpose(dic.Keys)<br/>End Sub<br/></p>
回复

使用道具 举报

发表于 2008-1-16 09:11 | 显示全部楼层

只有好好学习一下,还没有记住其中的格式。
回复

使用道具 举报

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

<p>Sub 一有二没有()<br/>&nbsp;Dim arr1, arr2, arr3(), x%, y%, k%<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;&nbsp; For y = 1 To UBound(arr2)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If arr1(x, 1) = arr2(y, 1) Then GoTo 100<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr1(x, 1)<br/>100:<br/>&nbsp;Next x<br/>&nbsp; Range("e2").Resize(k) = arr3<br/>&nbsp;End Sub<br/>&nbsp;<br/>Sub 二有一没有()<br/>Dim arr1, arr2, arr3(), x%, y%, k%<br/>arr1 = Range("a2:a11")<br/>arr2 = Range("b2:b11")<br/>ReDim arr3(1 To UBound(arr1) + UBound(arr2), 1 To 1)<br/>For x = 1 To UBound(arr2)<br/>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;&nbsp; Next y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; k = k + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; arr3(k, 1) = arr2(x, 1)<br/>100:<br/>&nbsp;&nbsp; Next x<br/>Range("f2").Resize(k) = arr3<br/>End Sub</p>
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-26 09:16 , Processed in 0.296677 second(s), 4 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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