12. セルの指定
Sub
セルの値を取得するセルに値をセットする()
MsgBox
(Cells(1,
"A").Value)
Cells(1,
"A").Value
=
Cells(1,
"A").Value
*
2
End
Sub
Sub
型エラー()
MsgBox
(Cells(1,
"C").Value
+
"["
+
Cells(1,
"A").Value
+
"]")
End
Sub
Sub
数値を文字列に変換する、文字列を結合する()
MsgBox
(Cells(1,
"C").Value
+
"["
+
CStr(Cells(1,
"A").Value)
+
"]")
Cells(1,
"D").Value
=
Cells(1,
"C").Value
+
"["
+
CStr(Cells(1,
"A").Value)
+
"]"
End
Sub
Sub
数式をつかう()
Cells(1,
"E").Formula
=
"=B1
-‐
A1"
End
Sub
2012年2月17日金曜日 12
13. 文字列の置換
Sub 初期値() Sub セル内改行を消す()
Cells(1, "A").Value = "ATTATTA" Dim str As String
End Sub str = Cells(3, "A").Value
Cells(3, "A").Value = Replace(str, vbCr, " ")
Sub 置換() End Sub
Dim str As String
str = Cells(1, "A").Value Sub LFはセル内改行しない()
MsgBox (str) Dim str As String
Cells(2, "A").Value = Replace(str, "A", "a") str = Cells(1, "A").Value
End Sub MsgBox (str & vbLf & str)
Cells(3, "A").Value = str & vbLf & str
Sub タブ文字列() End Sub
Dim str As String
str = Cells(1, "A").Value Sub CRLF()
MsgBox (str & vbTab & str) Dim str As String
End Sub str = Cells(1, "A").Value
MsgBox (str & vbCrLf & str)
Sub CRとセル内改行() Cells(3, "A").Value = str & vbCrLf & str
Dim str As String End Sub
str = Cells(1, "A").Value
Cells(3, "A").Value = str & vbCr & str & vbCr & str
End Sub
2012年2月17日金曜日 13
14. くりかえし、For文
Sub
くりかえし()
Dim
i
As
Integer
For
i
=
1
To
10
Cells(i,
"A")
=
i
Cells(i,
"B")
=
i
*
i
Cells(i,
"C")
=
i
+
i
Next
i
End
Sub
2012年2月17日金曜日 14
15. IF文、論理式
Sub
IF文()
Dim
i
As
Integer
For
i
=
1
To
10
If
(Cells(i,
"A").Value
Mod
2)
=
0
Then
Cells(i,
"D").Value
=
"偶数"
End
If
Next
i
End
Sub
2012年2月17日金曜日 15
16. 応用1:SQL文を作成する
Sub
INSERT文生成()
Dim
i
As
Integer
For
i
=
1
To
5
Cells(i,
"G").Value
=
"INSERT
INTO
genes
VALUES
('"
&
_
Cells(i,
"A").Value
&
"',
"
&
_
Cells(i,
"B").Value
&
",
"
&
_
Cells(i,
"C").Value
&
",
"
&
_
"'"
&
Cells(i,
"D").Value
&
"',
"
&
_
Cells(i,
"E").Value
&
",
"
&
_
"'"
&
Cells(i,
"F").Value
&
"')"
Next
i
End
Sub
2012年2月17日金曜日 16