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.

【ChatGPT】 VBAでコマンド文生成マクロを実装

0
Posted at

vbaで下記を実装したい
①セルの値をもとにコマンド文を生成する
②①で生成したコマンド文をテキストファイルに出力(ダイアログで保存先指定)

Sub SaveCommandToFile()
    Dim cellValue As String
    Dim commandText As String
    Dim filePath As String
    Dim fileNum As Integer
    Dim dialog As FileDialog
    
    ' A1セルの値を取得
    cellValue = ThisWorkbook.Sheets(1).Range("A1").Value
    
    ' コマンド文の生成
    commandText = "echo " & cellValue & " > output.txt"
    
    ' 保存先を選択するダイアログを表示
    Set dialog = Application.FileDialog(msoFileDialogSaveAs)
    
    With dialog
        .Title = "保存先を指定してください"
        .FilterIndex = 2
        .DefaultExt = "txt"
        .InitialFileName = "default_command.txt" ' デフォルトのファイル名を設定
        
        ' ダイアログが開かれ、ユーザーが保存先を指定した場合
        If .Show = -1 Then
            filePath = .SelectedItems(1)
            
            ' テキストファイルにコマンド文を出力
            fileNum = FreeFile
            Open filePath For Output As fileNum
            Print #fileNum, commandText
            Close fileNum
            
            MsgBox "コマンド文を保存しました: " & filePath
        Else
            MsgBox "保存がキャンセルされました"
        End If
    End With
End Sub
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?