'変数を宣言
Dim str() As String
'10個のレコードを作成
For i = 0 To 9
'多次元配列を作成
ReDim Preserve str(2, i)
str(0, i) = "test0" & i
str(1, i) = "test1" & i
str(2, i) = "test2" & i
Next
'表示
Debug.Print str(0, 0)
Debug.Print str(1, 1)
Debug.Print str(0, 2)
Sub main()
'使用している変数を宣言
Dim FileArray() As String
Dim FilePath As Variant
'使用している定数を宣言
Const DirPath As String = フォルダパス
Const ExtName As String = "*.*"
'指定フォルダのファイルを取得
'関数呼び出し(Functionを参照)
FileArray = FileArrayResult(DirPath, ExtName)
'取得したファイル名を表示
For Each FilePath In FileArray
Debug.Print FilePath
Next
End Sub
Function FileArrayResult(ByVal DirPath As String, ByVal ExtName As String)
'使用している変数を宣言
Dim buf As String
Dim FileArray() As String
Dim fileCnt As Integer
'ファイルがあるかチェック
buf = Dir(DirPath & ExtName)
'初期化
fileCnt = 0
'配列にファイルパスを代入
Do While buf <> ""
ReDim Preserve FileArray(fileCnt)
FileArray(fileCnt) = DirPath & buf
fileCnt = fileCnt + 1
buf = Dir()
Loop
FileArrayResult = FileArray()
End Function
Sub main()
'変数の宣言
Dim BookExt As Boolean
'定数の宣言
Const BookName As String = "ブック名"
'ブックが開かれているかチェック
'関数呼び出し(Functionを参照)
BookExt = BookCheck(BookName)
'ブックがあるかを出力
If BookExt = True Then
Debug.Print BookName & "ブックが見つかりました."
Else
Debug.Print BookName & "ブックが見つかりませんでした."
End If
End Sub
Function BookCheck(ByVal BookName As String)
'初期化
BookCheck = False
'全ブックのオブジェクトを取得
For Each bk In Workbooks
'ブック名(ファイル名)を取得
If bk.Name = BookName Then
BookCheck = True
Exit For
End If
Next bk
End Function
Sub main()
'変数の宣言
Dim SheetExt As Boolean
'定数の宣言
Const SheetName As String = "Sheet1"
'シート名があるかチェック
'関数呼び出し(Functionを参照)
SheetExt = SheetCheck(SheetName)
'シート名があるかを出力
If SheetExt = True Then
Debug.Print SheetName & "シートが見つかりました"
Else
Debug.Print SheetName & "シートが見つかりませんでした"
End If
End Sub
Function SheetCheck(ByVal SheetName As String)
'初期化
SheetCheck = False
'全シートのオブジェクトを取得
For Each ws In Worksheets
'シート名を取得
'ifがTrueならSheetCheckにTrueを代入
If ws.Name = SheetName Then
SheetCheck = True
Exit For '該当のシート名があれば Exit Forで処理終了
End If
Next ws
End Function
Sub main()
'変数の宣言
Dim CellExt As Boolean
'定数の宣言
Const CellName As String = "セル名"
'セル名があるかチェック
'関数呼び出し(Functionを参照)
CellExt = CellCheck(CellName)
'セル名があるかを出力
If CellExt = True Then
Debug.Print CellName & "セルが見つかりました."
Else
Debug.Print CellName & "セルが見つかりませんでした."
End If
End Sub
Function CellCheck(ByVal CellName As String)
'初期化
CellCheck = False
'全セル名のオブジェクトを取得
For Each nm In Names
'セル名を取得
If nm.Name = CellName Then
CellCheck = True
Exit For '該当のセル名があればExit Forで処理終了
End If
Next nm
End Function
Sub main()
'日時を表示
'独自関数(好きなフォーマット)
Debug.Print DtTime()
'Now関数
Debug.Print Now()
End Sub
Function DtTime()
'変数の宣言
Dim DtYear As String
Dim DtMonth As String
Dim DtDay As String
Dim DtHour As String
Dim DtMinute As String
Dim DtSecond As String
'日時の取得
DtYear = Year(Date)
DtMonth = "0" & Month(Date)
DtDay = "0" & Day(Date)
DtHour = "0" & Hour(Time)
DtMinute = "0" & Minute(Time)
DtSecond = "0" & Second(Time)
'0を削除
DtMonth = Right(DtMonth, 2)
DtDay = Right(DtDay, 2)
DtHour = Right(DtHour, 2)
DtMunute = Right(DtMinute, 2)
DtSecond = Right(DtSecond, 2)
DtTime = DtYear & "/" & DtMonth & "/" & DtDay & " – " & DtHour & ":" & DtMunute & ":" & DtSecond
End Function
ActiveWorkbook.Worksheets(SheetName).Activate
Workbooks(BookName).Activate
Application.ScreenUpdating = False
Application.ScreenUpdating = True
Open filepath For Output As #1
Print #1, "sample"
Close #1
Dim buf As String
Open "FilePath" For Input As #1
Line Input #1, buf
debug.print buf
Close #1
