Kaushik Joshi
Member
hi Macro Ninjas,
I am using the below macro to consolidate data from 3 different files Source files into a Single Consolidated file.
Each source file has variable number of sheets with a standard number of Data columns.
In column B of each file in each sheet, there appears name of the file....AdAstra, HRFort, Manushya.
Before the Macro proceeds with copying and consolidating data from all source files....
I want it to copy the file name of each Source file and paste it in front of each record in the Source file itself (at column B) on every sheet and then Consolidate.
In Consolidated file, copied data will look like column B (attached ).
Since the data in column B is nothing but the File name of Source file, I want the Source file users to avoid manually populating data in column B in each sheet.
I feel this should be easily possible for someone very used to VBA coding...
Please help..
My consolidation code is as follows:
Sub Joining()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open "C:\Users\K6246948\Desktop\DEL-2016-06\GNO-HRIS Reports Automated\HR Analytics\AMG_Recruit_Analysis\Ad Astra.xlsx"
Workbooks.Open "C:\Users\K6246948\Desktop\DEL-2016-06\GNO-HRIS Reports Automated\HR Analytics\AMG_Recruit_Analysis\HRFort.xlsx"
Workbooks.Open "C:\Users\K6246948\Desktop\DEL-2016-06\GNO-HRIS Reports Automated\HR Analytics\AMG_Recruit_Analysis\Manushhya.xlsx"
Dim lrow, r As Integer
Dim ws As Worksheet
On Error Resume Next
ThisWorkbook.Sheets("Consolidation").[Table1].Delete shift:=xlUp
With Workbooks("Ad Astra.xlsx")
For Each ws In .Sheets
lrow = ws.Columns("A").Cells(Rows.Count).End(xlUp).Row
r = ThisWorkbook.Sheets("Consolidation").[Table1].SpecialCells(xlCellTypeConstants).Count
If lrow > 1 Then
ws.Range("A2:AJ" & lrow).Copy
If r > 0 Then
ThisWorkbook.Sheets("Consolidation").Cells(ThisWorkbook.Sheets("Consolidation").[Table1].Rows.Count + 2, 1).PasteSpecial xlPasteValues
Else
ThisWorkbook.Sheets("Consolidation").[Table1].PasteSpecial xlPasteValues
End If
End If
'End If
Next ws
.Close False
End With
With Workbooks("HRFort.xlsx")
For Each ws In .Sheets
lrow = ws.Columns("A").Cells(Rows.Count).End(xlUp).Row
r = ThisWorkbook.Sheets("Consolidation").[Table1].SpecialCells(xlCellTypeConstants).Count
If lrow > 1 Then
ws.Range("A2:AJ" & lrow).Copy
If r > 0 Then
ThisWorkbook.Sheets("Consolidation").Cells(ThisWorkbook.Sheets("Consolidation").[Table1].Rows.Count + 2, 1).PasteSpecial xlPasteValues
Else
ThisWorkbook.Sheets("Consolidation").[Table1].PasteSpecial xlPasteValues
End If
End If
'End If
Next ws
.Close False
End With
With Workbooks("Manushhya.xlsx")
For Each ws In .Sheets
lrow = ws.Columns("A").Cells(Rows.Count).End(xlUp).Row
r = ThisWorkbook.Sheets("Consolidation").[Table1].SpecialCells(xlCellTypeConstants).Count
If lrow > 1 Then
ws.Range("A2:AJ" & lrow).Copy
If r > 0 Then
ThisWorkbook.Sheets("Consolidation").Cells(ThisWorkbook.Sheets("Consolidation").[Table1].Rows.Count + 2, 1).PasteSpecial xlPasteValues
Else
ThisWorkbook.Sheets("Consolidation").[Table1].PasteSpecial xlPasteValues
End If
End If
'End If
Next ws
.Close False
End With
ThisWrkbk.Sheets("Analysis").PivotTables("PivotTable1").PivotCache.Refresh
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
I am using the below macro to consolidate data from 3 different files Source files into a Single Consolidated file.
Each source file has variable number of sheets with a standard number of Data columns.
In column B of each file in each sheet, there appears name of the file....AdAstra, HRFort, Manushya.
Before the Macro proceeds with copying and consolidating data from all source files....
I want it to copy the file name of each Source file and paste it in front of each record in the Source file itself (at column B) on every sheet and then Consolidate.
In Consolidated file, copied data will look like column B (attached ).
Since the data in column B is nothing but the File name of Source file, I want the Source file users to avoid manually populating data in column B in each sheet.
I feel this should be easily possible for someone very used to VBA coding...
Please help..
My consolidation code is as follows:
Sub Joining()
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Workbooks.Open "C:\Users\K6246948\Desktop\DEL-2016-06\GNO-HRIS Reports Automated\HR Analytics\AMG_Recruit_Analysis\Ad Astra.xlsx"
Workbooks.Open "C:\Users\K6246948\Desktop\DEL-2016-06\GNO-HRIS Reports Automated\HR Analytics\AMG_Recruit_Analysis\HRFort.xlsx"
Workbooks.Open "C:\Users\K6246948\Desktop\DEL-2016-06\GNO-HRIS Reports Automated\HR Analytics\AMG_Recruit_Analysis\Manushhya.xlsx"
Dim lrow, r As Integer
Dim ws As Worksheet
On Error Resume Next
ThisWorkbook.Sheets("Consolidation").[Table1].Delete shift:=xlUp
With Workbooks("Ad Astra.xlsx")
For Each ws In .Sheets
lrow = ws.Columns("A").Cells(Rows.Count).End(xlUp).Row
r = ThisWorkbook.Sheets("Consolidation").[Table1].SpecialCells(xlCellTypeConstants).Count
If lrow > 1 Then
ws.Range("A2:AJ" & lrow).Copy
If r > 0 Then
ThisWorkbook.Sheets("Consolidation").Cells(ThisWorkbook.Sheets("Consolidation").[Table1].Rows.Count + 2, 1).PasteSpecial xlPasteValues
Else
ThisWorkbook.Sheets("Consolidation").[Table1].PasteSpecial xlPasteValues
End If
End If
'End If
Next ws
.Close False
End With
With Workbooks("HRFort.xlsx")
For Each ws In .Sheets
lrow = ws.Columns("A").Cells(Rows.Count).End(xlUp).Row
r = ThisWorkbook.Sheets("Consolidation").[Table1].SpecialCells(xlCellTypeConstants).Count
If lrow > 1 Then
ws.Range("A2:AJ" & lrow).Copy
If r > 0 Then
ThisWorkbook.Sheets("Consolidation").Cells(ThisWorkbook.Sheets("Consolidation").[Table1].Rows.Count + 2, 1).PasteSpecial xlPasteValues
Else
ThisWorkbook.Sheets("Consolidation").[Table1].PasteSpecial xlPasteValues
End If
End If
'End If
Next ws
.Close False
End With
With Workbooks("Manushhya.xlsx")
For Each ws In .Sheets
lrow = ws.Columns("A").Cells(Rows.Count).End(xlUp).Row
r = ThisWorkbook.Sheets("Consolidation").[Table1].SpecialCells(xlCellTypeConstants).Count
If lrow > 1 Then
ws.Range("A2:AJ" & lrow).Copy
If r > 0 Then
ThisWorkbook.Sheets("Consolidation").Cells(ThisWorkbook.Sheets("Consolidation").[Table1].Rows.Count + 2, 1).PasteSpecial xlPasteValues
Else
ThisWorkbook.Sheets("Consolidation").[Table1].PasteSpecial xlPasteValues
End If
End If
'End If
Next ws
.Close False
End With
ThisWrkbk.Sheets("Analysis").PivotTables("PivotTable1").PivotCache.Refresh
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub