fork download
  1. Option Explicit
  2.  
  3. Const Folder As String = "d:\goo\data2"
  4. Const BookName As String = "Book1.xlsx"
  5. Const SheetName As String = "Sheet1"
  6. Const MarginRow As Long = 3 '製品名開始直前迄の行
  7. Public Sub 日付数量抽出()
  8. Dim wb As Workbook '入力ブック
  9. Dim ws As Worksheet '入力ブックの入力シート
  10. Dim ws2 As Worksheet '出力シート
  11. Dim maxrow As Long '入力シート最大行数
  12. Dim maxrow2 As Long '出力シート最大行数
  13. Dim pcount As Long '製品件数
  14. Dim wrow As Long '入力シート行番号
  15. Dim wcol As Long '入力シート列番号
  16. Dim wrow2 As Long '出力シート行番号
  17. Dim wcol2 As Long '出力シート列番号
  18. Dim i As Long '製品番号
  19. Dim ctr As Long '登録数量カウンタ
  20. Dim pname As String '製品名
  21. Dim comment As String 'コメント(W列)
  22. Set ws2 = Worksheets("Sheet2")
  23. '入力ファイルオープン
  24. Set wb = Workbooks.Open(Folder & "\" & BookName)
  25. Set ws = wb.Worksheets(SheetName)
  26. maxrow = ws.Cells(Rows.Count, "G").End(xlUp).Row 'G列の最大行取得
  27. '11行未満の場合
  28. If maxrow < MarginRow + 8 Then
  29. MsgBox ("製品名なし")
  30. Exit Sub
  31. End If
  32. '製品件数算出
  33. pcount = (maxrow - MarginRow) \ 8
  34. If (maxrow - MarginRow) Mod 8 <> 0 Then
  35. MsgBox ("終端行不正")
  36. Exit Sub
  37. End If
  38. '出力シートクリア
  39. maxrow2 = ws2.Cells(Rows.Count, "D").End(xlUp).Row 'D列の最大行取得
  40. If maxrow2 > 2 Then
  41. ws2.Range("D3:D" & maxrow2).ClearContents
  42. ws2.Range("AD3:AG" & maxrow2).ClearContents
  43. End If
  44. wrow2 = 3
  45. '製品件数分繰り返す
  46. For i = 1 To pcount
  47. '数量記入行を決定
  48. wrow = (i - 1) * 8 + MarginRow + 5
  49. pname = ws.Cells(wrow, "G").Value
  50. comment = ws.Cells(wrow, "W").Value
  51. 'W列にExpiryがない場合はエラー
  52. If InStr(1, comment, "Expiry", 1) = 0 Then
  53. MsgBox (wrow & "行 W列 Expiryなし")
  54. Exit Sub
  55. End If
  56. ws2.Cells(wrow2, "D").Value = pname '製品名
  57. ctr = 0
  58. 'Y列~BT列まで繰り返す
  59. For wcol = 25 To 72
  60. '数量が登録されていれば、Sheet2へ転記
  61. If ws.Cells(wrow, wcol) <> "" Then
  62. wcol2 = 30 + ctr * 2
  63. ws2.Cells(wrow2, wcol2).Value = ws.Cells(3, wcol).Value '日付
  64. ws2.Cells(wrow2, wcol2 + 1).Value = ws.Cells(wrow, wcol).Value '数量
  65. ctr = ctr + 1
  66. End If
  67. Next
  68. '数量が1件も登録されていない場合
  69. If ctr = 0 Then
  70. ws2.Cells(wrow2, "AD").Resize(, 4).Value = "-"
  71. End If
  72. wrow2 = wrow2 + 1
  73. Next
  74. wb.Close
  75. MsgBox ("完了")
  76. End Sub
  77.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty