Philip1957
Board Regular
- Joined
- Sep 30, 2014
- Messages
- 185
- Office Version
- 365
- Platform
- Windows
Greetings,
I have a macro and 2 functions that work together to consolidate multiple worksheets from the same workbook onto one new sheet by appending the data to the next empty column that works just fine.
Maybe not the most elegant coding, but it works.
I am trying to modify the macro to do the same thing, but by appending the data to the next empty row.
This compiles and generates no errors (even with error handling commented blocked). The macro runs and loops the correct number of times, but it only adds the first sheet's data to the new sheet. It doesn't append the rest of the sheets, it just goes through the motions.
Any help at all with this would be appreciated.
I haven't gotten that far yet but I'm also unsure as to how to eliminate the duplicate header rows this will create. I can either try searching for the header rows and delete them, or select the data to copy after the first sheet from row 2 down. Any help here would also be appreciated (and would avoid a second new thread).
Thanks in advance for any and all help on this problem. I'm sure I've simply forgotten or missed something stupid, and so I thank you for your patience as well.
Thanks,
~ Phil
I have a macro and 2 functions that work together to consolidate multiple worksheets from the same workbook onto one new sheet by appending the data to the next empty column that works just fine.
Code:
Sub ConShtsClmn()
On Error GoTo IfError
Dim Sht As Worksheet, DstSht As Worksheet
Dim LstRow As Long, LstCol As Long, DstCol As Long
Dim i As Integer, EnRange As String
Dim SrcRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the Append_Data WorkSheet if it exists
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Sheets("Append_Data").Delete
Application.DisplayAlerts = True
'Add a new WorkSheet and name as 'Append_Data'
With ActiveWorkbook
Set DstSht = .Sheets.Add(After:=.Sheets(.Sheets.Count))
DstSht.Name = "Append_Data"
End With
'Loop through each WorkSheet in the workbook and copy the data to the 'Append_Data' WorkSheet
For Each Sht In ActiveWorkbook.Worksheets
If Sht.Name <> DstSht.Name Then
'Find the last row on the 'Append_Data' sheet
DstCol = fn_LastColumn(DstSht)
If DstCol = 1 Then DstCol = 0
'Find Input data range
LstRow = fn_LastRow(Sht)
LstCol = fn_LastColumn(Sht)
EnRange = Sht.Cells(LstRow, LstCol).Address
Set SrcRng = Sht.Range("A1:" & EnRange)
'Check whether there are enough columns in the 'Append_Data' Worksheet
If DstCol + SrcRng.Columns.Count > DstSht.Columns.Count Then
MsgBox "There are not enough columns to place the data in the Append_Data worksheet."
GoTo IfError
End If
'Copy data to the 'Append_Data' WorkSheet
SrcRng.Copy Destination:=DstSht.Cells(1, DstCol + 1)
End If
Next
IfError:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
Code:
Function fn_LastColumn(ByVal Sht As Worksheet)
Dim lastCol As Long
lastCol = Sht.Cells.SpecialCells(xlLastCell).Column
lCol = Sht.Cells.SpecialCells(xlLastCell).Column
Do While Application.CountA(Sht.Columns(lCol)) = 0 And lCol <> 1
lCol = lCol - 1
Loop
fn_LastColumn = lCol
End Function
Code:
Function fn_LastRow(ByVal Sht As Worksheet)
Dim lastRow As Long
lastRow = Sht.Cells.SpecialCells(xlLastCell).Row
lRow = Sht.Cells.SpecialCells(xlLastCell).Row
Do While Application.CountA(Sht.Rows(lRow)) = 0 And lRow <> 1
lRow = lRow - 1
Loop
fn_LastRow = lRow
End Function
Maybe not the most elegant coding, but it works.
I am trying to modify the macro to do the same thing, but by appending the data to the next empty row.
Code:
Sub ConShtsRow()
On Error GoTo IfError
Dim Sht As Worksheet, DstSht As Worksheet
Dim LstRow As Long, LstCol As Long, DstRow As Long
Dim i As Integer, EnRange As String
Dim SrcRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
'Delete the Append_Data WorkSheet if it exists
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Sheets("Append_Data").Delete
Application.DisplayAlerts = True
'Add a new WorkSheet and name as 'Append_Data'
With ActiveWorkbook
Set DstSht = .Sheets.Add(After:=.Sheets(.Sheets.Count))
DstSht.Name = "Append_Data"
End With
'Loop through each WorkSheet in the workbook and copy the data to the 'Append_Data' WorkSheet
For Each Sht In ActiveWorkbook.Worksheets
If Sht.Name <> DstSht.Name Then
'Find the last row on the 'Append_Data' sheet
DstRow = fn_LastRow(DstSht)
If DstRow = 1 Then DstRow = 0
'Find Input data range
LstRow = fn_LastRow(Sht)
LstCol = fn_LastColumn(Sht)
EnRange = Sht.Cells(LstRow, LstCol).Address
Set SrcRng = Sht.Range("A1:" & EnRange)
'Check whether there are enough rows in the 'Append_Data' Worksheet
If DstRow + SrcRng.Rows.Count > DstSht.Rows.Count Then
MsgBox "There are not enough rows to place the data in the Append_Data worksheet."
GoTo IfError
End If
'Copy data to the 'Append_Data' WorkSheet
SrcRng.Copy Destination:=DstSht.Cells(1, DstRow + 1) 'This is where it pastes to next empty column.
End If
Next
IfError:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
This compiles and generates no errors (even with error handling commented blocked). The macro runs and loops the correct number of times, but it only adds the first sheet's data to the new sheet. It doesn't append the rest of the sheets, it just goes through the motions.
Any help at all with this would be appreciated.
I haven't gotten that far yet but I'm also unsure as to how to eliminate the duplicate header rows this will create. I can either try searching for the header rows and delete them, or select the data to copy after the first sheet from row 2 down. Any help here would also be appreciated (and would avoid a second new thread).
Thanks in advance for any and all help on this problem. I'm sure I've simply forgotten or missed something stupid, and so I thank you for your patience as well.
Thanks,
~ Phil