Sub HelloWorld() '过程的名称不能以数字开头,不能有空格,可以是中文
MsgBox "Hello, World!"
End Sub
' 这是一个注释
Dim age As Integer
age = 25
Dim name As String
name = "MediaTea"
Const PI As Single = 3.1415926
Dim arr(5) '若数组下标从 0 开始时,它可以保持 6 个值
Dim arr1(0 To 3, 0 To 4) As Variant '定义一个三行四列的二维数组
arr2 = Array(1, 2, 3, 4, 5) '使用 Array 方法定义数组
arrStr = Split("apple,banana,orange", ",") '使用Split函数将字符串转换为数组
arr(0) = "MediaTea"
arr(1) = 100
arr(2) = 3.14
arr(3) = True '布尔型
arr1(0, 1) = #6/18/2023# '日期
arr1(2, 3) = #12:45:00 PM# '时间
If arr(3) Then
MsgBox arrStr(0) '显示 apple
End If
If arr2(3) < 5 Then
MsgBox arr(0) '显示 MediaTea
End If
Dim a, b As Integer
a = 15
5 =
MsgBox (a + b) * 2 '结果为 40
MsgBox ("15" + "5") * 2 '结果为 310
MsgBox a & b '结果为 155
Dim score As Integer
score = 85
If score >= 90 Then
MsgBox "优秀"
ElseIf score >= 80 Then
MsgBox "良好"
ElseIf score >= 70 Then
MsgBox "中等"
Else
MsgBox "不及格"
End If
Dim dayOfWeek As String
dayOfWeek = "Monday"
Select Case dayOfWeek
Case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"
MsgBox "工作日"
Case "Saturday", "Sunday"
MsgBox "周末"
Case Else
MsgBox "无效的输入"
End Select
Dim i As Integer
For i = 1 To 5 Step 1
i '在立即窗口显示 i 值
Next i
'常用于列举数组或集合中的每个元素
fruits = Array("苹果", "香蕉", "雪梨")
For Each Item In fruits
MsgBox Item
Next
Dim x As Integer
x = 2
Do While x <= 10
5).Value = 100 '将 E2:E10 单元格填充为 100
x = x + 1
Loop
Function AddNumbers(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
AddNumbers = num1 + num2
End Function
Sub Greet(ByVal name As String)
MsgBox "Hello, " & name & "!"
End Sub
8.1 单元格对象相关
a = Range("A1") '表示 A1 单元格
Range("C1") = Application.Sum(Range("A1:A9")) '对 A1 到 A9 单元格求和,并将结果放在 C1 单元格
[A1] = "MediaTea" '对 A1 单元格填充字符串
d = [A1:A9] '表示 A1 到 A9 单元格
e = Cells(2, 3) '表达第 2 行第 3 列的单元格,即 C2 单元格
Dim x As Integer, y As Integer
x = Range("B2").End(xlDown).Row '返回 B2 下方有内容的单元格的行数。
Range("B2").End(xlDown).Offset(1, 0).Value = 100 '将 B2 下方最后一个有内容的单元格下面的单元格赋值。
'对 B2 下方所有有内容的单元格右侧的 E 列单元格赋值
For x = 2 To Range("B2").End(xlDown).Row
Cells(x, 5).Value = 100
Next x
'对当前工作簿中的 Sheet1 工作表中的 A1 单元格赋值
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = "MediaTea"
'显示当前工作表中的 A1 单元格的值
MsgBox Range("A1")
'设置单元格的填充色
Range("A1").Interior.ColorIndex = 24
'设置单元格的字体大小
Range("A1").Font.Size = 12
'为当前选中的单元格(区域)设置字体颜色
Selection.Font.Color = RGB(255, 0, 0)
'清除单元格内容
Range("A1").ClearContents
'将 B3 的值复制到 C3 单元格
Range("A1").Copy Range("C3")
'将 C3 的值剪切到 D3 单元格
Range("C3").Cut Range("D3")
发表评论