fork download
  1. Option Explicit
  2.  
  3. Const Folder1 As String = "d:\goo\data10\IN" '転記元ブック格納フォルダ
  4. Const Folder2 As String = "d:\goo\data10\OUT" '転記先ブック格納フォルダ
  5. Const Book2 As String = "★【PGB】2024~2029年度 電源国内_PGB 投資予測_帳票"
  6. Dim RE As Object '正規表現オブジェクト
  7. Dim ks1 As Worksheet '管理シート
  8. Dim ks2 As Worksheet '変換結果シート
  9. Dim k_maxrow1 As Long '管理シート最大行番号
  10. Dim k_maxrow2 As Long '変換結果シート最大行番号
  11. Dim ows1 As Worksheet '投資用シート
  12. Dim ows2 As Worksheet '計上用シート
  13. Dim dicT As Object 'ディクショナリ キー:装置名 値:予測の行番号
  14. Public Sub 月度対応転記()
  15. Dim krow1 As Long '管理シート行番号
  16. Dim sname As String '装置名称
  17. Dim bname As String
  18. Dim bname2 As String '転記先ブック名
  19. Dim bpath2 As String '転記先ブックのパス名
  20. Dim wb1 As Workbook '転記元ブック
  21. Dim wb2 As Workbook '転記先ブック
  22. Dim out_mm As Long '出力月数
  23. Application.ScreenUpdating = False
  24.  
  25. Set dicT = CreateObject("Scripting.Dictionary")
  26. Set RE = CreateObject("VBScript.RegExp")
  27. RE.Pattern = "^\d{4}/\d{2}$"
  28. RE.Global = True
  29. Set ks1 = Worksheets("管理")
  30. Set ks2 = Worksheets("変換結果")
  31. out_mm = 0
  32. If IsNumeric(ks1.Range("E2").Value) = True Then
  33. out_mm = CLng(ks1.Range("E2").Value)
  34. End If
  35. If out_mm < 1 Or out_mm > 12 Then
  36. MsgBox ("管理シートの出力月が不正です")
  37. Exit Sub
  38. End If
  39. k_maxrow2 = 2
  40. '転記先ブックオープン
  41. bname2 = Book2 & ".xlsx"
  42. bpath2 = Folder2 & "\" & bname2
  43. Set wb2 = Workbooks.Open(bpath2)
  44. Set ows1 = wb2.Worksheets("国内_連結修正後")
  45. Set ows2 = wb2.Worksheets("国内_計上連結修正後")
  46. '投資用シート、計上用シート読込
  47. If check_out_sheet() = False Then Exit Sub
  48. '管理シート読込及び転記先名称のチェック
  49. k_maxrow1 = ks1.Cells(Rows.Count, "A").End(xlUp).Row
  50. For krow1 = 2 To k_maxrow1
  51. sname = ks1.Cells(krow1, "B").Value
  52. If dicT(sname) = False Then
  53. MsgBox ("管理シートの[" & sname & "]は転記先シートに存在しません")
  54. Exit Sub
  55. End If
  56. Next
  57.  
  58. '転記元ブックの読込
  59. bname = Dir(Folder1 & "\*.xlsx")
  60. Application.Calculation = xlCalculationManual
  61. Do While bname <> ""
  62. Set wb1 = Workbooks.Open(Folder1 & "\" & bname)
  63. If read_all_sheets(wb1) = False Then
  64. Application.Calculation = xlCalculationAutomatic
  65. Exit Sub
  66. End If
  67. wb1.Close
  68. bname = Dir()
  69. Loop
  70. '転記先ブックの名称を変えて保存する
  71. Application.Calculation = xlCalculationAutomatic
  72. bname2 = Book2 & "_" & out_mm & "月予測.xlsx"
  73. bpath2 = Folder2 & "\" & bname2
  74. Application.DisplayAlerts = False
  75. wb2.SaveAs (bpath2)
  76. Application.DisplayAlerts = True
  77. wb2.Close
  78. Application.ScreenUpdating = True
  79. MsgBox ("完了")
  80. End Sub
  81. '転記先ブックのシートチェック
  82. Private Function check_out_sheet() As Boolean
  83. Dim eflag As Boolean: eflag = False
  84. Dim maxrow1 As Long
  85. Dim maxrow2 As Long
  86. check_out_sheet = False
  87. maxrow1 = ows1.Cells(Rows.Count, "C").End(xlUp).Row
  88. maxrow2 = ows2.Cells(Rows.Count, "C").End(xlUp).Row
  89. If maxrow1 <> maxrow2 Then
  90. MsgBox ("転記先ブックの2つのシートの行数が不一致です")
  91. Exit Function
  92. End If
  93. If maxrow1 < 6 Then eflag = True
  94. If (maxrow1 - 2) Mod 4 <> 0 Then eflag = True
  95. If eflag = True Then
  96. MsgBox ("転記先ブックの2つのシートの行数が正しくありません")
  97. Exit Function
  98. End If
  99. Dim wrow As Long
  100. Dim key1 As String
  101. Dim key2 As String
  102. For wrow = 3 To maxrow1 Step 4
  103. key1 = ows1.Cells(wrow, "B").Value
  104. key2 = ows2.Cells(wrow, "B").Value
  105. If key1 <> key2 Then
  106. MsgBox ("転記先ブックの2つのシートの装置名の並びが不一致です")
  107. Exit Function
  108. End If
  109. dicT(key1) = wrow + 2 '予測の行を設定
  110. Next
  111. check_out_sheet = True
  112. End Function
  113. '転記元の全シートを処理する
  114. Private Function read_all_sheets(ByRef wb1 As Workbook) As Boolean
  115. read_all_sheets = False
  116. Dim sarray As Variant
  117. Dim elm As Variant
  118. Dim result As Boolean
  119. '処理対象となるシートの一覧
  120. sarray = Array("D6110", "D6120", "D6125", "D6560")
  121. For Each elm In sarray
  122. If exist_sheet(wb1, elm) = True Then
  123. result = read_1_sheet(wb1, elm)
  124. If result = False Then Exit Function
  125. End If
  126. Next
  127. read_all_sheets = True
  128. End Function
  129. '指定シートがブック内にあるかチェックする
  130. Private Function exist_sheet(ByRef wb1, ByVal elm As String)
  131. Dim i As Long
  132. exist_sheet = False
  133. For i = 1 To wb1.Worksheets.Count
  134. If UCase(elm) = UCase(wb1.Worksheets(i).Name) Then
  135. exist_sheet = True
  136. Exit Function
  137. End If
  138. Next
  139. End Function
  140. '1シートを処理する
  141. Private Function read_1_sheet(ByRef wb1, ByVal elm As String)
  142. Dim ws As Worksheet
  143. Dim maxrow As Long
  144. Dim wrow As Long
  145. Dim wmonth As String
  146. Dim sname As String
  147. Dim t_mm As Long
  148. Dim k_mm As Long
  149. Dim tk_row As Long
  150. Dim t_col As Long
  151. Dim k_col As Long
  152. read_1_sheet = False
  153. Set ws = wb1.Worksheets(elm)
  154. maxrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
  155. For wrow = 2 To maxrow
  156. '投資月チェック
  157. wmonth = ws.Cells(wrow, "C").Value
  158. t_mm = get_month(wmonth)
  159. If t_mm = 0 Then
  160. ws.Activate
  161. ws.Cells(wrow, "C").Select
  162. MsgBox ("転記元シート[" & elm & "]投資月エラー:" & wmonth)
  163. Exit Function
  164. End If
  165. '計上月チェック
  166. wmonth = ws.Cells(wrow, "D").Value
  167. k_mm = get_month(wmonth)
  168. If k_mm = 0 Then
  169. ws.Activate
  170. ws.Cells(wrow, "D").Select
  171. MsgBox ("転記元シート[" & elm & "]計上月エラー:" & wmonth)
  172. Exit Function
  173. End If
  174. '装置名チェック
  175. sname = ws.Cells(wrow, "B").Value
  176. If dicT.exists(sname) = False Then
  177. sname = get_sname(ws.Cells(wrow, "A").Value)
  178. End If
  179. '転記先の装置名が存在する場合
  180. If sname <> "" Then
  181. tk_row = dicT(sname)
  182. t_col = get_column(t_mm)
  183. k_col = get_column(k_mm)
  184. ows1.Cells(tk_row, t_col).Value = ws.Cells(wrow, "E").Value '投資用金額
  185. ows2.Cells(tk_row, k_col).Value = ws.Cells(wrow, "E").Value '計上用金額
  186. End If
  187. Next
  188. read_1_sheet = True
  189. End Function
  190. '月の取得 yyyy/mm からmmの数値を取得(0はエラー)
  191. Private Function get_month(ByVal wmonth) As Long
  192. get_month = 0
  193. Dim w_mm As Long
  194. If RE.test(wmonth) = False Then Exit Function
  195. w_mm = CLng(Right(wmonth, 2))
  196. If w_mm < 1 Or w_mm > 12 Then Exit Function
  197. get_month = w_mm
  198. End Function
  199. '管理番号から転記先の装置名を取得する
  200. Private Function get_sname(ByVal kbango As String)
  201. Dim ptn As String
  202. Dim wrow As Long
  203. For wrow = 2 To k_maxrow1
  204. ptn = ks1.Cells(wrow, "A").Value
  205. If kbango Like ptn Then
  206. get_sname = ks1.Cells(wrow, "B").Value
  207. ks2.Cells(k_maxrow2, "A").Value = kbango
  208. ks2.Cells(k_maxrow2, "B").Value = ptn
  209. ks2.Cells(k_maxrow2, "C").Value = ks1.Cells(wrow, "B").Value
  210. k_maxrow2 = k_maxrow2 + 1
  211. Exit Function
  212. End If
  213. Next
  214. get_sname = ""
  215. End Function
  216. '月数からカラム番号を取得する
  217. Private Function get_column(ByVal mm As Long) As Long
  218. Dim bias As Long: bias = 1
  219. If mm < 4 Then mm = mm + 12
  220. If mm > 9 Then bias = bias + 1
  221. get_column = mm + bias
  222. End Function
  223.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty