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?

More than 1 year has passed since last update.

ascii読み出し加工してcsvに吐き出し

Posted at

VBA(Visual Basic for Applications)を使用してASCファイルから特定の文字列を抜き出し、CSVに書き出す例を以下に示します。この例では、ASCファイルから各行の特定の文字列を抜き出して、それをCSVに書き出す簡単なプロシージャです。

Sub ExtractDataFromASCAndExportToCSV()
    Dim ascFilePath As String
    Dim csvFilePath As String
    Dim inputData As String
    Dim outputData As String
    Dim fileNumber As Integer
    
    ' ASCファイルとCSVファイルのパスを指定
    ascFilePath = "C:\Path\To\Your\File.asc"
    csvFilePath = "C:\Path\To\Your\Output\File.csv"
    
    ' ASCファイルを開く
    fileNumber = FreeFile
    Open ascFilePath For Input As fileNumber
    
    ' CSVファイルを作成または既存のファイルを上書き
    Open csvFilePath For Output As fileNumber
    
    ' ASCファイルから行ごとにデータを読み込み、特定の文字列を抜き出してCSVに書き込む
    Do Until EOF(fileNumber)
        Line Input #fileNumber, inputData
        ' 適切な処理を追加して特定の文字列を抜き出す
        ' 例: outputData = Mid(inputData, 1, 10) ' 最初の10文字を抜き出す
        ' CSVにデータを書き込む
        Print #fileNumber, outputData
    Loop
    
    ' ファイルを閉じる
    Close fileNumber
End Sub

このVBAマクロは、指定したASCファイルからデータを読み込み、特定の文字列を抜き出してCSVファイルに書き込む基本的な例です。適切な文字列の抽出ロジックを outputData の値を設定する部分に追加してください。

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?