Excel精英培训网

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

For 循環

[复制链接]
发表于 2015-7-5 23:18 | 显示全部楼层 |阅读模式
本帖最后由 yunyan0805 于 2015-7-5 23:46 编辑

各位大俠好,

小弟是VBA初學者,以下是我的問題點 ..


Sub TEST()
Set d = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
On Error Resume Next
工作表1.Select
ir = Cells(65536, 2).End(xlUp).Row
For i = 2 To ir
d(Cells(i, 2).Value & "") = Range(Cells(i, 3), Cells(i, 3)).Interior.ColorIndex
Next
工作表8.Select
ir = Cells(65536, 2).End(xlUp).Row
For i = 4 To ir
Range(Cells(i, 2), Cells(i, 2)).Interior.ColorIndex = d(Cells(i, 1).Value & "")
Next



工作表1.Select
ir = Cells(65536, 2).End(xlUp).Row
For i = 2 To ir
d(Cells(i, 2).Value & "") = Range(Cells(i, 3), Cells(i, 3)).Interior.ColorIndex
Next
工作表8.Select
ir = Cells(65536, 14).End(xlUp).Row
For i = 4 To ir
Range(Cells(i, 14), Cells(i,14)).Interior.ColorIndex = d(Cells(i,13).Value & "")
Next
End Sub



工作表1.Select
ir = Cells(65536, 2).End(xlUp).Row
For i = 2 To ir
d(Cells(i,2).Value & "") = Range(Cells(i, 3), Cells(i, 3)).Interior.ColorIndex
Next
工作表8.Select
ir = Cells(65536,26).End(xlUp).Row
For i = 4 To ir
Range(Cells(i,26), Cells(i,26)).Interior.ColorIndex = d(Cells(i,25).Value & "")
Next
End Sub

這樣寫的話我要寫很多句,變數就是+12

我要怎麼寫 
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2015-7-6 06:49 | 显示全部楼层
Sub TEST()
Set d = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
On Error Resume Next
工作表1.Select
ir = Cells(65536, 2).End(xlUp).Row
For i = 2 To ir
d(Cells(i, 2).Value & "") = Range(Cells(i, 3), Cells(i, 3)).Interior.ColorIndex
Next
工作表8.Select
for j=2 to 26 step 12  '26可以换成最后的列
ir = Cells(65536, j).End(xlUp).Row
For i = 4 To ir
Range(Cells(i, j), Cells(i, j)).Interior.ColorIndex = d(Cells(i, 1).Value & "")
Next
next
end sub
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-8-16 14:04 , Processed in 0.109408 second(s), 3 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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