仮置き1
https://chatgpt.com/share/693861bd-6ff4-8006-8546-006142cf135d
WinMergeU.exe /u /noninteractive /or”C:\diff\report.html” “A.txt” “B.txt”
仮置き2
Sub WriteSheetNameToAllSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Range("C1").Value = ws.Name
Next ws
End Sub
仮置き
TeraMacroとかPythonでは無く、MSオフィスのマクロ集です。(GPT4に書いて貰ったものを含みます。)
エクセルの1シートに沢山のcsvファイルを張り付ける(1ファイル1シート)
Option Explicit
Sub ImportCsvFilesToColumns()
Dim fdlg As FileDialog
Dim folderPath As String
Dim fileName As String
Dim colIndex As Long
Dim rowIndex As Long
Dim fnum As Integer
Dim lineText As String
‘ フォルダ選択ダイアログを表示
Set fdlg = Application.FileDialog(msoFileDialogFolderPicker)
With fdlg
.Title = “CSVファイルが入っているフォルダを選択してください”
If .Show <> -1 Then
MsgBox “フォルダが選択されませんでした。処理を中止します。”, vbExclamation
Exit Sub
End If
folderPath = .SelectedItems(1)
End With
‘ パスの末尾に \ が無ければ付ける
If Right(folderPath, 1) <> “\” Then
folderPath = folderPath & “\”
End If
Application.ScreenUpdating = False
‘ C列から開始(C=3, D=4, …)
colIndex = 3
‘ 最初のCSVファイルを取得
fileName = Dir(folderPath & “*.csv”)
If fileName = “” Then
MsgBox “指定フォルダにCSVファイルが見つかりませんでした。”, vbInformation
Application.ScreenUpdating = True
Exit Sub
End If
Do While fileName <> “”
‘ 行番号を1にリセット
rowIndex = 1
' テキストファイルとして開く
fnum = FreeFile
Open folderPath & fileName For Input As #fnum
' 1行目からEOFまで読み込み
Do While Not EOF(fnum)
Line Input #fnum, lineText
' ActiveSheetの rowIndex 行, colIndex 列へ書き込み
ThisWorkbook.ActiveSheet.Cells(rowIndex, colIndex).Value = lineText
rowIndex = rowIndex + 1
Loop
Close #fnum
' 次の列へ(C→D→E→F…)
colIndex = colIndex + 1
' 次のCSVファイル
fileName = Dir()
Loop
Application.ScreenUpdating = True
MsgBox “CSVの取り込みが完了しました。”, vbInformation
End Sub
ファイル名と共通の文字列を含むエクセルシートにファイルを貼るVBA
Option Explicit
Sub ImportCsvToRandomNameSheets()
Dim folderPath As String
Dim fd As FileDialog
Dim fileName As String
Dim fullPath As String
Dim baseName As String
Dim parts() As String
Dim sheetName As String
Dim wbCsv As Workbook
Dim wsCsv As Worksheet
Dim wsTarget As Worksheet
Dim lastRow As Long
Dim nextCol As Long
'=== CSVフォルダを選択 ===
Set fd = Application.FileDialog(msoFileDialogFolderPicker)
With fd
.Title = "CSVファイルが入っているフォルダを選択してください"
If .Show <> -1 Then
MsgBox "キャンセルされました。", vbInformation
Exit Sub
End If
folderPath = .SelectedItems(1)
End With
If Right(folderPath, 1) <> "\" Then
folderPath = folderPath & "\"
End If
Application.ScreenUpdating = False
Application.DisplayAlerts = False
'=== フォルダ内のCSVファイルを順番に処理 ===
fileName = Dir(folderPath & "*.csv")
Do While fileName <> ""
fullPath = folderPath & fileName
' 拡張子を除いたファイル名(例: Kyoten020_L9bXw_config_001)
baseName = Left(fileName, InStrRev(fileName, ".") - 1)
' "_" で分割 → 0:Kyoten020, 1:L9bXw, 2:config, 3:001
parts = Split(baseName, "_")
' 安全のため要素数チェック
If UBound(parts) >= 1 Then
sheetName = parts(1) ' 2番目の部分がシート名 (例: L9bXw)
On Error Resume Next
Set wsTarget = ThisWorkbook.Worksheets(sheetName)
On Error GoTo 0
If Not wsTarget Is Nothing Then
'=== CSVファイルを開く ===
Set wbCsv = Workbooks.Open(fileName:=fullPath, Local:=True)
Set wsCsv = wbCsv.Worksheets(1) ' CSVは通常1シートだけ
' CSV側の最終行(A列前提)
lastRow = wsCsv.Cells(wsCsv.Rows.Count, "A").End(xlUp).Row
'=== 貼り付け先シートの次の空き列を探す ===
' A1 が空なら 1列目を使用
If IsEmpty(wsTarget.Cells(1, 1)) Then
nextCol = 1
Else
' 一番右の使用列を探して +1
nextCol = wsTarget.Cells(1, wsTarget.Columns.Count).End(xlToLeft).Column + 1
End If
'=== 値を貼り付け(A列から縦にコピー) ===
wsTarget.Range(wsTarget.Cells(1, nextCol), wsTarget.Cells(lastRow, nextCol)).Value = _
wsCsv.Range("A1:A" & lastRow).Value
' CSVブックを閉じる(保存しない)
wbCsv.Close SaveChanges:=False
Else
' 対応するシートが存在しない場合はスキップ
' 必要なら MsgBox 等で通知してもOK
End If
' オブジェクトの解放
Set wsTarget = Nothing
Set wsCsv = Nothing
Set wbCsv = Nothing
End If
' 次のファイルへ
fileName = Dir()
Loop
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "取り込みが完了しました。", vbInformation
End Sub
エクセルで沢山のシートを作成する
Sub CreateSheetsFromList()
Dim sheetNames As Variant
Dim i As Long
Dim ws As Worksheet
sheetNames = Array( _
"config_001", "config_002", "config_003", "config_004", "config_005", _
"config_006", "config_007", "config_008", "config_009", "config_010", _
"config_011", "config_012", "config_013", "config_014", "config_015", _
"config_016", "config_017", "config_018", "config_019", "config_020", _
"config_021", "config_022", "config_023", "config_024", "config_025", _
"config_026", "config_027", "config_028", "config_029", "config_030", _
"config_031", "config_032", "config_033", "config_034", "config_035", _
"config_036", "config_037", "config_038", "config_039", "config_040", _
"config_041", "config_042", "config_043", "config_044", "config_045", _
"config_046", "config_047", "config_048", "config_049", "config_050", _
"config_051", "config_052", "config_053", "config_054", "config_055", _
"config_056", "config_057", "config_058", "config_059", "config_060", _
"config_061", "config_062", "config_063", "config_064", "config_065", _
"config_066", "config_067", "config_068", "config_069", "config_070", _
"config_071", "config_072", "config_073", "config_074", "config_075", _
"config_076", "config_077", "config_078", "config_079", "config_080", _
"config_081", "config_082", "config_083", "config_084", "config_085", _
"config_086", "config_087", "config_088", "config_089", "config_090", _
"config_091", "config_092", "config_093", "config_094", "config_095", _
"config_096", "config_097", "config_098", "config_099", "config_100")
'--- シート作成 ---
For i = LBound(sheetNames) To UBound(sheetNames)
Worksheets.Add after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = sheetNames(i)
Next i
MsgBox "100枚のシートを作成しました。"
End Sub
Option Explicit
Sub ImportCsvFilesToColumns()
Dim fdlg As FileDialog
Dim folderPath As String
Dim fileName As String
Dim colIndex As Long
Dim rowIndex As Long
Dim fnum As Integer
Dim lineText As String
' フォルダ選択ダイアログを表示
Set fdlg = Application.FileDialog(msoFileDialogFolderPicker)
With fdlg
.Title = "CSVファイルが入っているフォルダを選択してください"
If .Show <> -1 Then
MsgBox "フォルダが選択されませんでした。処理を中止します。", vbExclamation
Exit Sub
End If
folderPath = .SelectedItems(1)
End With
' パスの末尾に \ が無ければ付ける
If Right(folderPath, 1) <> "\" Then
folderPath = folderPath & "\"
End If
Application.ScreenUpdating = False
' C列から開始(C=3, D=4, …)
colIndex = 3
' 最初のCSVファイルを取得
fileName = Dir(folderPath & "*.csv")
If fileName = "" Then
MsgBox "指定フォルダにCSVファイルが見つかりませんでした。", vbInformation
Application.ScreenUpdating = True
Exit Sub
End If
Do While fileName <> ""
' 行番号を1にリセット
rowIndex = 1
' テキストファイルとして開く
fnum = FreeFile
Open folderPath & fileName For Input As #fnum
' 1行目からEOFまで読み込み
Do While Not EOF(fnum)
Line Input #fnum, lineText
' ActiveSheetの rowIndex 行, colIndex 列へ書き込み
ThisWorkbook.ActiveSheet.Cells(rowIndex, colIndex).Value = lineText
rowIndex = rowIndex + 1
Loop
Close #fnum
' 次の列へ(C→D→E→F…)
colIndex = colIndex + 1
' 次のCSVファイル
fileName = Dir()
Loop
Application.ScreenUpdating = True
MsgBox "CSVの取り込みが完了しました。", vbInformation
End Sub
Wordで、フォントとサイズを英語、日本語、数値でそれぞれ別のものに統一する。
Option Explicit
Sub ImportCsvFilesToColumns()
Dim fdlg As FileDialog
Dim folderPath As String
Dim fileName As String
Dim colIndex As Long
Dim rowIndex As Long
Dim fnum As Integer
Dim lineText As String
' フォルダ選択ダイアログ
Set fdlg = Application.FileDialog(msoFileDialogFolderPicker)
With fdlg
.Title = "CSVファイルが入っているフォルダを選択してください"
If .Show <> -1 Then
MsgBox "フォルダが選択されませんでした。", vbExclamation
Exit Sub
End If
folderPath = .SelectedItems(1)
End With
If Right(folderPath, 1) <> "\" Then
folderPath = folderPath & "\"
End If
Application.ScreenUpdating = False
' C列から開始
colIndex = 3
' CSVファイル探索
fileName = Dir(folderPath & "*.csv")
If fileName = "" Then
MsgBox "指定フォルダにCSVファイルが見つかりませんでした。", vbInformation
Application.ScreenUpdating = True
Exit Sub
End If
Do While fileName <> ""
' ① 1行目にファイル名を書き込む
ThisWorkbook.ActiveSheet.Cells(1, colIndex).Value = fileName
' 2行目からデータを開始
rowIndex = 2
fnum = FreeFile
Open folderPath & fileName For Input As #fnum
Do While Not EOF(fnum)
Line Input #fnum, lineText
ThisWorkbook.ActiveSheet.Cells(rowIndex, colIndex).Value = lineText
rowIndex = rowIndex + 1
Loop
Close #fnum
' 次の列へ
colIndex = colIndex + 1
fileName = Dir()
Loop
' ③ 列幅を自動調整(C列〜最後の列)
ThisWorkbook.ActiveSheet.Columns("C:" & Split(Cells(1, colIndex - 1).Address, "$")(1)).AutoFit
Application.ScreenUpdating = True
MsgBox "CSVの取り込みが完了しました。", vbInformation
End Sub
エクセルで一度に500個の置換を行う(そう言えばワードやパワポでも同じこと出来ると便利ですね)
※500までと言う制限は、コードを「親子化」等すると増やせます。
※下記のコードは1個分の置換しか書いていませんが、ちょっとした工夫でカンタンに500個分書けます。ヒントは「エクセルの表でエクセルのマクロを書く」です。日常的にコンフィグ書いている人なら知っているテクニックだと思います。
Cells.Replace What:=”置換前①”, Replacement:=”置換後①”, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
パワーポイントで障害ポイント図を作成する(「星形オブジェクトに数字を載せたもの」の番号が抜けたり飛んだりした事ありませんか?)
※「番号の飛びの無い星形オブジェクト」をマクロで生成し、カット&ペーストで図に載せれば番号の抜け飛びが発生しません。
Sub CreateStarShapes()
Dim slide As slide
Dim shape As shape
Dim i As Integer
Dim xPos As Single
Dim yPos As Single
Dim size As Single
' 初期位置とサイズ
xPos = 100
yPos = 100
size = 32
' アクティブなスライドを取得
Set slide = ActivePresentation.Slides(1)
' 20個の星形を作成
For i = 1 To 20
' 星形を追加
Set shape = slide.Shapes.AddShape(msoShape16pointStar, xPos, yPos, size, size)
' 枠線と塗りつぶしの設定
With shape
.Line.ForeColor.RGB = RGB(255, 0, 0) ' 赤色の枠線
.Fill.ForeColor.RGB = RGB(255, 0, 0) ' 赤色の塗りつぶし
' 内部のテキスト設定
With .TextFrame.TextRange
.Text = ChrW(&H2460 + (i - 1)) ' ①からの番号付け
.Font.size = 14 ' フォントサイズ
.Font.Color = RGB(255, 255, 255) ' 白色の文字
End With
End With
' 位置の更新
xPos = xPos + 5
yPos = yPos + 5
Next i
End Sub
