michelernqm
New Member
- Joined
- Jun 19, 2020
- Messages
- 11
- Office Version
- 365
- Platform
- Windows
Hi, I am new to VBA and I am wondering if someone could help guide me. I have a xlsm workbook w/ a sheet called "Consolidate" (this is where the data will consolidate to). All files I need to pull data from are in the same folder as the xlsm workbook.
All xlsx files within the folder have a tab called "main".
The macro works for pulling the data I need into the xlsm workbook, but I would like to expand on the macro below. What I am looking for is the data that's being pulled to repeat but with different cells selected and move down the sheet. Cells A1, B2 & C2 are pulled into one row (looping through all the files in the folder)- I want the macro to also pull additional cells from the original source sheets but have it continue to move down the master sheet ("consolidate"). I am not sure how to have the macro pull additional cells- for example cells A22, B23, C23 (from the original sheets) but continue down the master sheet.
If someone has another method or better VBA I am all ears.
Sub Consolidate()
Dim wkbkorigin As Workbook
Dim originsheet As Worksheet
Dim destsheet As Worksheet
Dim ResultRow As Long
Dim Fname As String
Dim RngDest As Range
Set destsheet = ThisWorkbook.Worksheets("Consolidate")
Set RngDest = destsheet.Cells(Rows.Count, 1).End(xlUp) _
.Offset(1, 0).EntireRow
Fname = Dir(ThisWorkbook.Path & "/*.xlsx")
'loop thropen each file in folder (excluding this one)
Do While Fname <> "" And Fname <> ThisWorkbook.Name
If Fname <> ThisWorkbook.Name Then
Set wkbkorigin = Workbooks.Open(ThisWorkbook.Path & "/" & Fname)
Set originsheet = wkbkorigin.Worksheets("main")
With RngDest
.Cells(1).Value = originsheet.Range("A1").Value
.Cells(2).Value = originsheet.Range("B2").Value
.Cells(3).Value = originsheet.Range("C2").Value
End With
wkbkorigin.Close savechanges:=False 'close current file
Set RngDest = RngDest.Offset(1, 0)
End If
Fname = Dir() 'get next file
Loop
End Sub
All xlsx files within the folder have a tab called "main".
The macro works for pulling the data I need into the xlsm workbook, but I would like to expand on the macro below. What I am looking for is the data that's being pulled to repeat but with different cells selected and move down the sheet. Cells A1, B2 & C2 are pulled into one row (looping through all the files in the folder)- I want the macro to also pull additional cells from the original source sheets but have it continue to move down the master sheet ("consolidate"). I am not sure how to have the macro pull additional cells- for example cells A22, B23, C23 (from the original sheets) but continue down the master sheet.
If someone has another method or better VBA I am all ears.
Sub Consolidate()
Dim wkbkorigin As Workbook
Dim originsheet As Worksheet
Dim destsheet As Worksheet
Dim ResultRow As Long
Dim Fname As String
Dim RngDest As Range
Set destsheet = ThisWorkbook.Worksheets("Consolidate")
Set RngDest = destsheet.Cells(Rows.Count, 1).End(xlUp) _
.Offset(1, 0).EntireRow
Fname = Dir(ThisWorkbook.Path & "/*.xlsx")
'loop thropen each file in folder (excluding this one)
Do While Fname <> "" And Fname <> ThisWorkbook.Name
If Fname <> ThisWorkbook.Name Then
Set wkbkorigin = Workbooks.Open(ThisWorkbook.Path & "/" & Fname)
Set originsheet = wkbkorigin.Worksheets("main")
With RngDest
.Cells(1).Value = originsheet.Range("A1").Value
.Cells(2).Value = originsheet.Range("B2").Value
.Cells(3).Value = originsheet.Range("C2").Value
End With
wkbkorigin.Close savechanges:=False 'close current file
Set RngDest = RngDest.Offset(1, 0)
End If
Fname = Dir() 'get next file
Loop
End Sub