Excel vba 操作

excel2003

vba操作应用快捷键

Alt+F11

Alt+F8

 

1hello_world

code:

Sub Hello_World()
    MsgBox ("hello vba")
End Sub



2光标定位

code:

Sub Select_Cells()
    'ThisWorkbook.Sheets("Sheet1").Range("A1").Select '定位单个光标
    'ThisWorkbook.Sheets("Sheet1").Range("A1", "D3").Select '定位光标范围
    'ThisWorkbook.Sheets("Sheet1").Range("C:C").Select '定位光标列
    ThisWorkbook.Sheets("Sheet1").Range("3:10").Select '定位光标列
End Sub

3固定位置的取值和赋值

code:

Sub Select_Cells()
    'ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = 22 '添加字
    'ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = "" '清空
    
    ThisWorkbook.Sheets("Sheet1").Cells(1, 1).Value = ThisWorkbook.Sheets("Sheet1").Cells(2, 1).Value
    
End Sub

4固定位置的取值计算

code:
Sub Add_Cells()

    With ThisWorkbook.Sheets("Sheet1")
    
        Dim Cell1, Cell2, Cell3 As Integer
        Cell1 = .Cells(8, 1).Value
        Cell2 = .Cells(8, 2).Value
        Cell3 = Cell1 + Cell2
        
        .Cells(8, 3).Value = Cell3
    
    End With
    
End Sub
5固定位置的设置字符串
code:

Sub Add_Cells()

    With ThisWorkbook.Sheets("Sheet1")
    
    Dim Text, Text2 As String
    Text = "Hello ZXL"
    Text2 = "my name is matt"
    
    .Cells(1, 1).Value = Text
    
    End With
    
End Sub

6判断语句(if)+inputbox输入框
code:

Sub Password_Protected()
    
    Dim Password, UserPassword As String
    Password = "Password"
    UserPassword = InputBox("please enther yout password")
    If Password = UserPassword Then
    MsgBox ("access granted")
    Else
    MsgBox ("password incorrect")
    End If
    
End Sub

7 界面交互
1.在excel中加入-自选图形-圆形矩形-右键 设置自选图形格式 设置对齐 颜色...

2.点右键制定宏


code:

Sub Password_Protected()
    
    Dim Password, UserPassword As String
    Password = "Password"
    UserPassword = InputBox("please enther yout password")
    If Password = UserPassword Then
    MsgBox ("access granted")
    Else
    MsgBox ("password incorrect")
    End If
    
End Sub
单击后



8 界面交互应用(年龄判断)


code:

Sub SubmitAge()

Dim Age As Integer
Age = ThisWorkbook.Sheets("Sheet1").Cells(5, 2).Value
If Age > 18 Then
MsgBox ("old")
Else
MsgBox ("young")
End If
End Sub
9 for语句循环添加
code:

Sub Fill_SEries()

For counter = 2 To 6
ThisWorkbook.Sheets("Sheet1").Cells(1, counter).Value
Next

End Sub












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值