Flora Edwards
asked on
related question to modify VBA, how to get it from workbooks instead of worksheets
Hi, i had a question which was answered https://www.experts-exchange.com/questions/29000556/Consolidate-multiple-worksheets-from-different-workbooks-into-single-sheet-where-columns-are-differently-structured.html
this time, i have another challenge, i was trying to figure out how to modify the the code so that instead of worksheets in thisworkbook
it prompts me to select a folder and then from folder it deletes blank worksheets and if worksheet with data found then copies the relevant data and pastes into Resultsheet of masterfile. i did not know how to modify the code. i would appreciate if someone could also comment the code so that i understand what part of the code does what. thanks very much.
this time, i have another challenge, i was trying to figure out how to modify the the code so that instead of worksheets in thisworkbook
it prompts me to select a folder and then from folder it deletes blank worksheets and if worksheet with data found then copies the relevant data and pastes into Resultsheet of masterfile. i did not know how to modify the code. i would appreciate if someone could also comment the code so that i understand what part of the code does what. thanks very much.
Option Explicit
Sub Consolidate()
Dim wsResult As Worksheet, ws As Worksheet
Dim rwResultStart As Long, rwResultEnd As Long, rwSheet As Long, rwSheetMax As Long
Dim colResult As Integer, colResultMax As Integer, col As Integer, colSheet As Integer
Application.ScreenUpdating = False
Set wsResult = ThisWorkbook.Worksheets("Result")
If wsResult.Range("A1").CurrentRegion.Rows.Count > 1 Then
wsResult.Range("A2", wsResult.Cells.SpecialCells(xlCellTypeLastCell)).ClearContents
End If
colResultMax = wsResult.Range("A1").CurrentRegion.Columns.Count
rwResultEnd = 1
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> wsResult.Name And ws.Name <> "Collectionshouldlooklikethis" Then
rwSheetMax = ws.Range("A1").CurrentRegion.Rows.Count
rwResultStart = rwResultEnd + 1
rwResultEnd = rwResultEnd + rwSheetMax - 1
For colSheet = 1 To ws.Range("A1").CurrentRegion.Columns.Count
For col = 1 To colResultMax
If wsResult.Cells(1, col) = ws.Cells(1, colSheet) Then
colResult = col
End If
Next col
For rwSheet = 2 To rwSheetMax
wsResult.Cells(rwResultStart + rwSheet - 2, colResult) = ws.Cells(rwSheet, colSheet)
Next rwSheet
Next colSheet
wsResult.Range(Cells(rwResultStart, colResultMax), Cells(rwResultEnd, colResultMax)) = "From " & ws.Name
End If
Next ws
End Sub
Financial-Sample.xlsm
ASKER
Thank you Ejgil Hedegaard.
i get the following error.
i have attached the masterfile with the source files.
1.xlsx
2.xlsx
3.xlsx
4.xlsx
Financial-Sample.xlsm
i get the following error.
i have attached the masterfile with the source files.
1.xlsx
2.xlsx
3.xlsx
4.xlsx
Financial-Sample.xlsm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
mistake
ASKER
Thanks.
Folder selection added.
Worksheets deleted if no data.
If Header in Source sheet is not found in Result sheet, the column is skipped.
Comments added in code.
Here is the code
Open in new window
Financial-Sample.xlsm