0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Excel VBA [備忘録]FreeFile関数について

Posted at

はじめに

FreeFile関数についての備忘録です。

プロシジャーの説明

サンプル1 ExcelファイルやCSVファイルを開く

ExcelファイルやCSVファイルを操作する際には、Workbooks.OpenメソッドやOpenステートメントを使用。ファイル番号は必要ありません。

' Excelファイルを開く例
Dim wb As Workbook
Set wb = Workbooks.Open("C:\path\to\your\file.xlsx")

' CSVファイルを開く例
Dim csvWs As Worksheet
Set csvWs = Workbooks.Open("C:\path\to\your\file.csv").Sheets(1)

サンプル2 テキストファイルを操作する

VBAでは、FreeFile関数はテキストファイルやバイナリファイルを操作する際に使用

Dim fileNum As Integer
fileNum = FreeFile
Open "C:\path\to\your\file.txt" For Output As #fileNum
Print #fileNum, "Hello, World!"
Close #fileNum
#fileNumについて

#fileNumと書くことで、VBAに対して「このファイル番号のファイルを操作する」ということを明示しています。「#」はファイル番号であることを明示するためのプレフィックスであり、他の変数や数値と区別するために使われます。Print #fileNum, "Hello, World!"というステートメントは、指定されたファイル番号を持つファイルに対して文字列を書き込むことを意味します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?