good evening all. I have a code that was created and it works perfectly. the only modification, i need is the ability to select the sheet I would like to consolidate. let me give you a brief background.
i have multiple workbooks by state with 12 tabs in each workbook. the tabs are the same because it is based off template. the tabs are named "Week 1", "Week 2", "Week 3", until "Week 12"
Each week, i have to consolidate that weeks data and then do some complicated lookups.
The vba works perfectly but there is a code in my macro where i have to activate that week tab by changing this line" Sheets("week 1").Activate" . I am wondering is there a way for me to create a userform that will ask me which sheet I would like to consolidate and then have it automatically consolidate that sheet from all the workbooks.
I have attached the workbook. the name of the macro is called "consolidate macro"
I have also copied the entire macro below" thank you for all your help.
------
Sub mod_consolidate()
Dim strListSheet As String, sh As Worksheet, TargetSh As Worksheet
Dim DestCell As Range, LastRow As Long, i As Integer, strFileNamePath As String
Dim strFileName As String, currentWB As Workbook, dataWB As Workbook, filecount As Integer
Dim prctProgress As Single
strListSheet = "Report File Path"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
filecount = Range("FILE_COUNT_LEVEL") ' the number of files to consolidate
On Error Resume Next
Set TargetSh = Worksheets("Master")
On Error GoTo 0
Sheets("Master").Activate
Rows("2:" & Rows.Count).ClearContents
Set DestCell = TargetSh.Range("A1")
Set DestCell = DestCell.Offset(1, 0)
'On Error GoTo ErrH
Sheets(strListSheet).Activate
Range("b2").Select
ProgressBox.Show 'displays progress bar
'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
Set currentWB = ActiveWorkbook
For i = 1 To filecount
strFileNamePath = Range("strFileName").Offset(i, 1)
strFileName = Range("strFileName").Offset(i, 0)
'this displays the status in percentage value in the progress bar of the PSR file being generated and the name of file
'being generated
Application.StatusBar = "Generating " & strFileName & " Consolidation....." & i & " of " & filecount
prctProgress = i / filecount * 100
ProgressBox.Increment prctProgress, "Consolidating for " & strFileName & "- " & i & " out of " & filecount
Application.Workbooks.Open strFileNamePath, UpdateLinks:=False, ReadOnly:=True
Set dataWB = ActiveWorkbook
ActiveSheet.Unprotect "ops"
Set sh = ActiveSheet
ActiveSheet.AutoFilterMode = False
'select usedrange to copy
Sheets("week 1").Activate
LastRow = ActiveSheet.Range("B55").End(xlUp).Row
If LastRow > 1 Then
sh.Range("B7:O" & LastRow).Copy
'activate generator workbook
currentWB.Activate
'activate master worksheet
TargetSh.Activate
TargetSh.Range(DestCell.Address).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set DestCell = DestCell.Offset(LastRow - 6)
dataWB.Close False
End If
Next i
Application.StatusBar = False
ProgressBox.Hide
currentWB.Activate
Sheets("Master").Activate
ActiveSheet.UsedRange.EntireColumn.AutoFit 'AutoFit the column width
Columns("E:E").Select
Selection.Style = "Percent"
MsgBox "Reports have been generated succussfully!", vbInformation
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
i have multiple workbooks by state with 12 tabs in each workbook. the tabs are the same because it is based off template. the tabs are named "Week 1", "Week 2", "Week 3", until "Week 12"
Each week, i have to consolidate that weeks data and then do some complicated lookups.
The vba works perfectly but there is a code in my macro where i have to activate that week tab by changing this line" Sheets("week 1").Activate" . I am wondering is there a way for me to create a userform that will ask me which sheet I would like to consolidate and then have it automatically consolidate that sheet from all the workbooks.
I have attached the workbook. the name of the macro is called "consolidate macro"
I have also copied the entire macro below" thank you for all your help.
------
Sub mod_consolidate()
Dim strListSheet As String, sh As Worksheet, TargetSh As Worksheet
Dim DestCell As Range, LastRow As Long, i As Integer, strFileNamePath As String
Dim strFileName As String, currentWB As Workbook, dataWB As Workbook, filecount As Integer
Dim prctProgress As Single
strListSheet = "Report File Path"
Application.ScreenUpdating = False
Application.DisplayAlerts = False
filecount = Range("FILE_COUNT_LEVEL") ' the number of files to consolidate
On Error Resume Next
Set TargetSh = Worksheets("Master")
On Error GoTo 0
Sheets("Master").Activate
Rows("2:" & Rows.Count).ClearContents
Set DestCell = TargetSh.Range("A1")
Set DestCell = DestCell.Offset(1, 0)
'On Error GoTo ErrH
Sheets(strListSheet).Activate
Range("b2").Select
ProgressBox.Show 'displays progress bar
'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
Set currentWB = ActiveWorkbook
For i = 1 To filecount
strFileNamePath = Range("strFileName").Offset(i, 1)
strFileName = Range("strFileName").Offset(i, 0)
'this displays the status in percentage value in the progress bar of the PSR file being generated and the name of file
'being generated
Application.StatusBar = "Generating " & strFileName & " Consolidation....." & i & " of " & filecount
prctProgress = i / filecount * 100
ProgressBox.Increment prctProgress, "Consolidating for " & strFileName & "- " & i & " out of " & filecount
Application.Workbooks.Open strFileNamePath, UpdateLinks:=False, ReadOnly:=True
Set dataWB = ActiveWorkbook
ActiveSheet.Unprotect "ops"
Set sh = ActiveSheet
ActiveSheet.AutoFilterMode = False
'select usedrange to copy
Sheets("week 1").Activate
LastRow = ActiveSheet.Range("B55").End(xlUp).Row
If LastRow > 1 Then
sh.Range("B7:O" & LastRow).Copy
'activate generator workbook
currentWB.Activate
'activate master worksheet
TargetSh.Activate
TargetSh.Range(DestCell.Address).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Set DestCell = DestCell.Offset(LastRow - 6)
dataWB.Close False
End If
Next i
Application.StatusBar = False
ProgressBox.Hide
currentWB.Activate
Sheets("Master").Activate
ActiveSheet.UsedRange.EntireColumn.AutoFit 'AutoFit the column width
Columns("E:E").Select
Selection.Style = "Percent"
MsgBox "Reports have been generated succussfully!", vbInformation
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub