LoginSignup
0
0

標準モジュール

Posted at

Option Explicit

Public Sub リセット()

''Sheet2の2行目以降を削除するボタンをsheet1に作成

Dim ws As Worksheet

'対象シートを設定'

Set ws = ThisWorkbook.Sheets("sheet2")


'データが入力されている最後のセル(最も右下のセル)を取得'

Dim lastcell As Range
Dim x As Long
Dim y As Long

Set lastcell = ws.Cells.SpecialCells(xlCellTypeLastCell)


'そのセルの行番号と列番号を取得'

x = lastcell.Row
y = lastcell.Column

Debug.Print "最終行: " & x & ", 最終列: " & y

'clearcontentsは、値のみ削除'

ws.Range(ws.Cells(2, 1), ws.Cells(x, y)).ClearContents

End Sub

Public Sub date_collect( _
folderPath As String, _
destinationFile As String, _
destinationSheet As String)

'###①定義
Dim file As String
Dim wb_output As Workbook
Dim ws_output As Worksheet
Dim wb_refer As Workbook
Dim ws_refer As Worksheet
Dim rng As Range
Dim lastrow As Long
Dim lastcell As Range
Dim x As Long
Dim y As Long

'###②出力先のファイルを開く
Application.ScreenUpdating = False
Application.EnableEvents = False

Set wb_output = Workbooks.Open(destinationFile)
Set ws_output = wb_output.Sheets(destinationSheet)

'###③対象フォルダ内のファイルをループ
file = Dir(folderPath & "*.xlsx")

Do While file <> ""
    Set wb_refer = Workbooks.Open(folderPath & file) ' "Workbook.Open" を "Workbooks.Open" に修正
    Set ws_refer = wb_refer.Sheets(1)

    'データが入力されている最後のセル(最も右下のセル)を取得
    Set lastcell = ws_refer.Cells.SpecialCells(xlCellTypeLastCell)

    'そのセルの行番号と、列番号を取得
    x = lastcell.Row
    y = lastcell.Column

    'コピーする範囲を設定
    Set rng = ws_refer.Range(ws_refer.Cells(2, 1), ws_refer.Cells(x, y))

    '出力先の最終行を取得
    lastrow = ws_output.Cells(Rows.Count, 1).End(xlUp).Row + 1

    'コピー&ペースト
    rng.Copy
    ws_output.Cells(lastrow, 1).PasteSpecial Paste:=xlPasteValues ' "pastspecial" を "PasteSpecial" に修正

    'ソースファイルを閉じる
    wb_refer.Close SaveChanges:=False

    file = Dir
Loop

'出力先ファイルを保存
wb_output.Save

Application.ScreenUpdating = True
Application.EnableEvents = True

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