excel中这个宏是什么意思?

来源:百度知道 编辑:UC知道 时间:2024/06/05 02:56:44
Sub Macro2()

' Macro2 Macro
' Macro recorded 12/1/2008 by localuser
'

Dim n As Integer

n = Cells(102, 9).Value

'
For i = 0 To n - 1

Cells(18, 3).Value = Cells(103 + i, 1).Value
Cells(65, 3).Value = Cells(103 + i, 1).Value
Cells(103 + i, 3).Value = Cells(28, 3).Value
Cells(103 + i, 4).Value = Cells(46, 6).Value
Cells(103 + i, 5).Value = Cells(75, 3).Value
Cells(103 + i, 6).Value = Cells(96, 6).Value

Next i

End Sub

希望高手解释一下!本人看的别人的excel文件中有个这个宏,好不容易查到代码!

Sub Macro2() 【宏的名称】

' Macro2 Macro
' Macro recorded 12/1/2008 by localuser
'

Dim n As Integer 【定义 n 为整数】

n = Cells(102, 9).Value 【n=单元格I102的值,我这里假设i102的值是3;则n=3】

'
For i = 0 To n - 1 【循环语句的开始,i 从0 到2(因为n=3,即3-1)的循环;所以共循环3次,分别是i=0,1,2】
【等号的意思就是把等号后面的值赋予等号前面的变量】
Cells(18, 3).Value = Cells(103 + i, 1).Value 【单元格C18的值等于A103的值,当执行i=1的循环时,C18=A(103+1)即A104;如此类推,所以单元格C18最终的值由i最后一次循环时指向的单元格决定,本例是A105】
Cells(65, 3).Value = Cells(103 + i, 1).Value【最终C65=A105,道理同上一句】
Cells(103 + i, 3).Value = Cells(28, 3).Value 【第一次循环时,i=0,所以C103=C28;第2次循环时,i=1,所以C104=C28;第3次循环时,i=2,所以C105=C28;】
Cells(103 + i, 4).Value = Cells(46, 6).Value 【原理同上,只是C103变成D103了,c28变成F46了。】
Cells(103 + i, 5).Value = Cells(75, 3).Value
Cells(103 + i, 6).Value = Cells(96, 6).Value

Next i 【这里指每次执行到这里时回到For i=0。。。那句重新开始循环】
End Sub 【宏结束】

一个很基本的宏

Dim n As Integer
定义“n”这个数值型变量