※String
Dim str As Stringstr = "abcdefg"
Cells(1, "A").Value = Now
Cells(1, "B").Value = Left(str, 4) 'abcd
Cells(2, "B").Value = Right(str, 4) 'defg
Cells(3, "B").Value = Mid(str, 4, 2) 'de
Cells(4, "B").Value = Len(str) '7
Cells(5, "B").Value = InStr(str, "cd") '3
※VBA 的 index 是從 1 開始的
※日期
Dim d1 As DateDim d2 As Date
d1 = DateValue("Jan 21, 1999")
Cells(1, "A").Value = d1 '1999/1/21
Cells(2, "A").Value = Year(d1) '1999
Cells(3, "A").Value = Month(d1) '1
Cells(4, "A").Value = Day(d1) '21
d2 = TimeValue("17:25:37") '等同 TimeValue("5:25:37 pm")
Cells(5, "A").Value = Hour(d2) '17
Cells(6, "A").Value = Minute(d2) '25
Cells(7, "A").Value = Second(d2) '37
.增加
Dim d As Date
d = DateValue("Jan 21, 1999")
d = DateAdd("m", -1, d)
MsgBox (d)
※第一個參數如下:
yyyy - 年
q - 季度
m - 月
y - 当年的第几天
d - 日
w - 当周的第几天
ww - 周
h - 小时
n - 分钟
s - 秒
※Function 和 Sub
Sub hello() '我是註解'MsgBox 3 * fun1()
'MsgBox fun2(3, 2)
'Call s1
's1
Call s2(3, 2)
s2 3, 2
End Sub
Function fun1() As Integer
fun1 = 2
End Function
Function fun2(a As Integer, b As Integer) As Integer
fun2 = a * b
End Function
Sub s1()
MsgBox fun2(3, 2)
End Sub
Sub s2(a As Integer, b As Integer)
MsgBox fun2(a, b)
End Sub
※Function 有沒有回傳值都可以,Sub 不能有回傳值
※呼叫時,Sub 可用 Call,如果不想用 Call,後面一定不能有圓括號
沒有留言:
張貼留言