PippaThePointer
New Member
- Joined
- Sep 21, 2023
- Messages
- 31
- Office Version
- 2016
- Platform
- Windows
Hi,
I have modified an existing VBA code to merge multiple sheets into one master sheet and also remove all rows that have blank in QTY. The next step i would like (ideally written into the this VBA) is for the source data to copy/consolodate into certain columns of the destination based on a row 1 title text. Is this possible?
For example my source data has 14 columns (A:N) and my destination has about 15 but i need only some of the source columns and I want them to go into particular columns. On top of that if possible i want the existing formulas in the destination sheet to auto fill down as it consolidates.
Below image is the consolidated source sheet (RDBMergeSheet) and the columns in Red are required to match up with certain Columns in the destination sheet (Data). Further down is the code im using to consolidate the collected sheets into one clean list (RDBMergeSheet). need to either edit this macro to sort columns or have a new macro to sort into sheet 'Data'.
Sub CopyDataWithoutHeaders()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"
'Fill in the start row
StartRow = 2
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the last row with data on the DestSh and sh
Last = LastRow(DestSh)
shLast = LastRow(sh)
'If sh is not empty and if the last row >= StartRow copy the CopyRng
If shLast > 0 And shLast >= StartRow Then
'Set the range that you want to copy
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'remove blank qty in the DestSh sheet
Sheets("RDBMergeSheet").Select
If ActiveSheet.Name <> "RDBMergeSheet" Then Exit Sub
On Error Resume Next
Columns("M").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
I have modified an existing VBA code to merge multiple sheets into one master sheet and also remove all rows that have blank in QTY. The next step i would like (ideally written into the this VBA) is for the source data to copy/consolodate into certain columns of the destination based on a row 1 title text. Is this possible?
For example my source data has 14 columns (A:N) and my destination has about 15 but i need only some of the source columns and I want them to go into particular columns. On top of that if possible i want the existing formulas in the destination sheet to auto fill down as it consolidates.
Below image is the consolidated source sheet (RDBMergeSheet) and the columns in Red are required to match up with certain Columns in the destination sheet (Data). Further down is the code im using to consolidate the collected sheets into one clean list (RDBMergeSheet). need to either edit this macro to sort columns or have a new macro to sort into sheet 'Data'.
Sub CopyDataWithoutHeaders()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim shLast As Long
Dim CopyRng As Range
Dim StartRow As Long
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the sheet "RDBMergeSheet" if it exist
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
On Error GoTo 0
Application.DisplayAlerts = True
'Add a worksheet with the name "RDBMergeSheet"
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "RDBMergeSheet"
'Fill in the start row
StartRow = 2
'loop through all worksheets and copy the data to the DestSh
For Each sh In ActiveWorkbook.Worksheets
If sh.Name <> DestSh.Name Then
'Find the last row with data on the DestSh and sh
Last = LastRow(DestSh)
shLast = LastRow(sh)
'If sh is not empty and if the last row >= StartRow copy the CopyRng
If shLast > 0 And shLast >= StartRow Then
'Set the range that you want to copy
Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
'Test if there enough rows in the DestSh to copy all the data
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the Destsh"
GoTo ExitTheSub
End If
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
'AutoFit the column width in the DestSh sheet
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
'remove blank qty in the DestSh sheet
Sheets("RDBMergeSheet").Select
If ActiveSheet.Name <> "RDBMergeSheet" Then Exit Sub
On Error Resume Next
Columns("M").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub