Excel精英培训网

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

[已解决]麻烦高手帮忙改一下程序

[复制链接]
发表于 2016-8-16 12:49 | 显示全部楼层 |阅读模式
Sub 按钮3_Click()
    Dim x As Integer
    Dim y As Integer
    Dim i As Long
    Dim j As Long
y = 6
While Sheet1.Cells(y, 1) <> ""
x = 6
While Sheet2.Cells(x, 1) <> ""
If Sheet2.Cells(x, 5) = Sheet1.Cells(y, 4) Then
j = Sheet3.Cells.Rows.Count
i = Sheet3.Range("a" & j).End(xlUp).Row + 1
Sheet3.rang("a" & i) = Sheet1.Cells(y, 3)
Sheet3.rang("b" & i) = Sheet1.Cells(y, 4)
Sheet3.rang("c" & i) = Sheet1.Cells(y, 5)
Sheet3.rang("d" & i) = Sheet1.Cells(y, 6)
Sheet3.rang("e" & i) = Sheet1.Cells(y, 7)
Sheet3.rang("f" & i) = Sheet2.Cells(x, 5)
Sheet3.rang("g" & i) = Sheet1.Cells(y, 10)
End If
x = x + 1
Wend
y = y + 1
Wend
End Sub
这段程序运行不了,不知是什么原因?

最佳答案
2016-8-16 14:00
你的sheet1和sheet2中没有配号编码相同的,代码如下:
  1. Sub 配号()
  2.     Dim d, arr(), i%, n%
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     With Sheet1
  5.         n = .Cells(.Rows.Count, 4).End(xlUp).Row
  6.         For i = 6 To n
  7.             d.Add .Cells(i, 4).Value, i - 5
  8.             ReDim Preserve arr(1 To 7, 1 To i - 5)
  9.             arr(1, i - 5) = .Cells(i, 3)
  10.             arr(2, i - 5) = .Cells(i, 4)
  11.             arr(3, i - 5) = .Cells(i, 5)
  12.             arr(4, i - 5) = .Cells(i, 6)
  13.             arr(5, i - 5) = .Cells(i, 7)
  14.             arr(6, i - 5) = .Cells(i, 10)
  15.         Next
  16.     End With
  17.     With Sheet2
  18.         n = .Cells(.Rows.Count, 2).End(xlUp).Row
  19.         For i = 6 To n
  20.             If d.Exists(.Cells(i, 2).Value) Then arr(7, d(.Cells(i, 2).Value)) = .Cells(i, 5)
  21.         Next
  22.     End With
  23.     x = 1
  24.     For Each k In d.keys
  25.         If arr(7, d(k)) <> "" Then
  26.             x = x + 1
  27.             Sheet3.Cells(x, 1).Resize(1, 7) = Application.WorksheetFunction.Transpose(Application.Index(arr, 0, d(k)))
  28.         End If
  29.     Next
  30. End Sub
复制代码

提问.rar

177.17 KB, 下载次数: 18

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2016-8-16 14:00 | 显示全部楼层    本楼为最佳答案   
你的sheet1和sheet2中没有配号编码相同的,代码如下:
  1. Sub 配号()
  2.     Dim d, arr(), i%, n%
  3.     Set d = CreateObject("Scripting.Dictionary")
  4.     With Sheet1
  5.         n = .Cells(.Rows.Count, 4).End(xlUp).Row
  6.         For i = 6 To n
  7.             d.Add .Cells(i, 4).Value, i - 5
  8.             ReDim Preserve arr(1 To 7, 1 To i - 5)
  9.             arr(1, i - 5) = .Cells(i, 3)
  10.             arr(2, i - 5) = .Cells(i, 4)
  11.             arr(3, i - 5) = .Cells(i, 5)
  12.             arr(4, i - 5) = .Cells(i, 6)
  13.             arr(5, i - 5) = .Cells(i, 7)
  14.             arr(6, i - 5) = .Cells(i, 10)
  15.         Next
  16.     End With
  17.     With Sheet2
  18.         n = .Cells(.Rows.Count, 2).End(xlUp).Row
  19.         For i = 6 To n
  20.             If d.Exists(.Cells(i, 2).Value) Then arr(7, d(.Cells(i, 2).Value)) = .Cells(i, 5)
  21.         Next
  22.     End With
  23.     x = 1
  24.     For Each k In d.keys
  25.         If arr(7, d(k)) <> "" Then
  26.             x = x + 1
  27.             Sheet3.Cells(x, 1).Resize(1, 7) = Application.WorksheetFunction.Transpose(Application.Index(arr, 0, d(k)))
  28.         End If
  29.     Next
  30. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-8-16 16:14 | 显示全部楼层
刚才拷贝代码试运行了一下,很好用。谢谢“老司机带带我”。
回复

使用道具 举报

 楼主| 发表于 2016-9-4 15:05 | 显示全部楼层
请问高手, d.Add .Cells(i, 4).Value, i - 5这句后面的“i - 5”参数的作用?谢谢!
回复

使用道具 举报

发表于 2016-9-8 22:39 | 显示全部楼层
老司机带带我 发表于 2016-8-16 14:00
你的sheet1和sheet2中没有配号编码相同的,代码如下:

用VBA字典提速这个速度太慢

http://www.excelpx.com/thread-424313-1-1.html




评分

参与人数 1 -3 收起 理由
小新De和尚头 -3

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-26 15:17 , Processed in 0.352071 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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