fork download
  1. Sub 交付用名前変更A4()
  2. Dim TargetFile As String
  3. Dim fpath As String, fname As String
  4. Dim oldfName1 As String
  5. Dim oldfName2 As String
  6. Dim newfName1 As String
  7. Dim newfName2 As String
  8. Dim oldpath1 As String
  9. Dim oldpath2 As String
  10. Dim newpath1 As String
  11. Dim newpath2 As String
  12. Dim ws As Worksheet
  13. Dim rng As Range
  14. Dim rg As Range
  15. Dim names As Variant
  16. Dim name As Variant
  17. Dim key As String
  18. Dim dicT As Object
  19. Set dicT = CreateObject("Scripting.Dictionary")
  20. Set ws = ThisWorkbook.Worksheets("昇降機質疑")
  21. '空白セルチェック
  22. Set rng = ws.Range("A4,v3,A3,V9")
  23. For Each rg In rng
  24. If rg.Value = "" Then
  25. MsgBox ("該当ファイルがありません")
  26. ws.Activate
  27. rg.Select
  28. Exit Sub
  29. End If
  30. Next
  31. 'ファイ名変更1
  32. oldfName1 = ws.Range("A4").Value
  33. oldfName1 = NGNarrowToWide(oldfName1)
  34. newfName1 = ws.Range("V3").Value & ".pdf"
  35. newfName1 = NGNarrowToWide(newfName1)
  36. 'ファイ名変更2
  37. oldfName2 = ws.Range("A3").Value
  38. oldfName2 = NGNarrowToWide(oldfName2)
  39. newfName2 = ws.Range("V9").Value & ".pdf"
  40. newfName2 = NGNarrowToWide(newfName2)
  41. names = Array(oldfName1, newfName1, oldfName2, newfName2)
  42. 'ファイル名重複チェック
  43. For Each name In names
  44. key = LCase(name)
  45. If dicT.exists(key) = True Then
  46. MsgBox ("ファイル名が重複しています。<" & name & ">")
  47. Exit Sub
  48. End If
  49. dicT(key) = True
  50. Next
  51. 'ファイル名の整合性チェック
  52. If check_files(oldfName1, newfName1, oldpath1, newpath1) = False Then Exit Sub
  53. If check_files(oldfName2, newfName2, oldpath2, newpath2) = False Then Exit Sub
  54. ''メッセージを表示し、実施確認する。
  55. If MsgBox("A4・A3の交付用を作成しますか?", vbExclamation + vbOKCancel) <> vbOK Then Exit Sub
  56. 'ファイル名変更
  57. Call change_name(oldpath1, newpath1)
  58. Call change_name(oldpath2, newpath2)
  59. MsgBox ("ファイル名変更完了")
  60. End Sub
  61. Public Function NGNarrowToWide(ByVal stg As String) As String
  62. stg = Replace(Replace(Replace(Replace(stg, "\", "¥"), "/", "/"), ":", ":"), "*", "*")
  63. stg = Replace(Replace(Replace(Replace(stg, "?", "?"), "<", "<"), ">", ">"), "|", "|")
  64. stg = Replace(stg, """", Chr(&H8168))
  65. NGNarrowToWide = stg
  66. End Function
  67. 'ファイル名チェック
  68. Private Function check_files(ByVal oldfname As String, ByVal newfname As String, ByRef oldpath As String, ByRef newpath As String) As Boolean
  69. check_files = False
  70. If LCase(Right(oldfname, 4)) <> ".pdf" Then
  71. MsgBox (oldfname & "の拡張子が不正です")
  72. Exit Function
  73. End If
  74. oldpath = ThisWorkbook.path & "\" & oldfname
  75. If Dir(oldpath) = "" Then
  76. MsgBox (oldfname & "が存在しません")
  77. Exit Function
  78. End If
  79. newpath = ThisWorkbook.path & "\" & newfname
  80. If Dir(newpath) <> "" Then
  81. Dim rc As Integer
  82. rc = MsgBox(newfname & "は既に存在する名前です" & vbCrLf & "はいを押すと既存ファイルは削除され" & vbCrLf & "リネームファイルに置き換えられます", vbYesNo, "置き換え確認")
  83. If rc <> vbYes Then
  84. MsgBox "処理を中止しました"
  85. Exit Function
  86. End If
  87. End If
  88. check_files = True
  89. End Function
  90. 'ファイり名変更
  91. Private Sub change_name(ByVal oldpath As String, ByVal newpath As String)
  92. If Dir(newpath) <> "" Then
  93. Application.DisplayAlerts = False
  94. Kill newpath
  95. Application.DisplayAlerts = True
  96. End If
  97. Name oldpath As newpath
  98. End Sub
  99.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty