5、[VBA入门到放弃笔记]单元格和变量

2017-06-07  本文已影响59人  叶知行
Dim i As Integer
For i = 1 To 10 
        Range("a" & i).Select
 Next
Dim i As Integer
    For i = 1 To 10
        Cells(i, 1).Select
    Next
For x = 1 To 10
        y = x + 2                     
        Cells(x, y) = x             
 Next
运行结果
'又如下面代码,F8演示
Sub tetet()
    Dim i As Integer
    Dim j  As Integer
    For i = 1 To 5
        j = i + 1
        Range("h" & i & ":" & "k" & j).Select          '
    Next
End Sub
'上面语句可以改为
Sub fdfd()
    Dim i As Integer
    Dim j  As Integer
    For i = 1 To 5
        j = i + 1
        Range(Cells(i, "h"), Cells(j, "k")).Select
    Next
End Sub
111.gif

无论Range或Cells,括号里面的参数都是可以使用变量的,那个方便选用那个。


Paste_Image.png
Sub 查找()
    Dim i As Integer
    For i = 2 To 10 '循环遍历部门
        If Cells(i, 1) = Cells(1, 5) Then '如果等于E1单元格的部门,那么
            Cells(1, 6) = Cells(i, 2) '输出实发工资到F1单元格
            Exit For '因为只有唯一的一个数值,所以找到需要的数据后,就退出当前的循环,不再往下遍历了。
'如这里在单元格A6找到E部门,获得数据后,就退出循环了,A6后面的单元格不再遍历,节省资源。如木有Exit For则代码一直运行到A10单元格才结束。
        End If
    Next
End Sub
上一篇 下一篇

猜你喜欢

热点阅读