legalhustler
Well-known Member
- Joined
- Jun 5, 2014
- Messages
- 1,214
- Office Version
- 365
- Platform
- Windows
The macro below combines several source sheets to a report sheet called "Combined." The macro works fairly well, except the value results in column A and B of the "Combined" sheet is returning the wrong range size based on each of the respective source sheets it pastes from. The range size from column A & B should be the same as columns C:H of the "Combined" sheet.
I will first explain where the values of column B of the "Combined" sheet comes from. The values from column B comes from copying the value of cell A4 of each source sheet. To determine how many rows cell A4 should be pasted to the "Combined" sheet, the code should determine the range, (always) starting at A8 to the last row and subtract 3 rows (which are not needed) of each respective source sheet, then pasting them starting from B3 of the "Combined" sheet.
The values from column A of the "Combined" sheet is taking left four characters from the value results of column B (mentioned above), starting at A3. Therefore the range in column A should be the same as column B, respective of each source sheet.
Here is what the code currently is doing. The first source sheet has the value "9800 - Workers Compensation" in cell A4. The data starts from A8:A94 (total of 87 rows), however the "Combined" sheet, pasted the value of cell A4 "9800 - Workers Compensation" from B3:B94 (total of 92 rows) instead of from B3:B89.
The second source sheet has the value "6500 - Claims Account" in cell A4. The data range is from A8:A97 (total of 90 rows), however the "Combined" sheet, pasted the value of cell A4 "6500 - Claims Account" from B95:B189 instead of from B90:B179.
Similarly, column A (starting at A3) of the "Combined" sheet which takes the 4 left characters of column B (starting at B3) has extra rows of data. The "Combined" sheet shows the pasted values for the first source sheet from A3:A94 and from A95:A189 for the second source sheet; however they should be from A3:A89 and A90:A179, for each sheet respectively.
Can someone assist in modifying the macro below to meet the correct requirements mentioned above?
I will first explain where the values of column B of the "Combined" sheet comes from. The values from column B comes from copying the value of cell A4 of each source sheet. To determine how many rows cell A4 should be pasted to the "Combined" sheet, the code should determine the range, (always) starting at A8 to the last row and subtract 3 rows (which are not needed) of each respective source sheet, then pasting them starting from B3 of the "Combined" sheet.
The values from column A of the "Combined" sheet is taking left four characters from the value results of column B (mentioned above), starting at A3. Therefore the range in column A should be the same as column B, respective of each source sheet.
Here is what the code currently is doing. The first source sheet has the value "9800 - Workers Compensation" in cell A4. The data starts from A8:A94 (total of 87 rows), however the "Combined" sheet, pasted the value of cell A4 "9800 - Workers Compensation" from B3:B94 (total of 92 rows) instead of from B3:B89.
The second source sheet has the value "6500 - Claims Account" in cell A4. The data range is from A8:A97 (total of 90 rows), however the "Combined" sheet, pasted the value of cell A4 "6500 - Claims Account" from B95:B189 instead of from B90:B179.
Similarly, column A (starting at A3) of the "Combined" sheet which takes the 4 left characters of column B (starting at B3) has extra rows of data. The "Combined" sheet shows the pasted values for the first source sheet from A3:A94 and from A95:A189 for the second source sheet; however they should be from A3:A89 and A90:A179, for each sheet respectively.
Can someone assist in modifying the macro below to meet the correct requirements mentioned above?
Code:
Sub CombineReports()
Dim sh As Worksheet, nsh As Worksheet, lr As Long, c As Range
Dim x As Long, nRng As Range
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
Set nsh = Sheets.Add(before:=Sheets(1))
nsh.Name = "Combined"
With nsh
.Range("A2") = "Account #": .Range("B2") = "Account Name": .Range("C2") = "Fund": .Range("D2") = "Debit": .Range("E2") = "Credit"
.Range("F2") = "Debit": .Range("G2") = "Credit": .Range("H2") = "Total"
.Range("D1:E1").Merge
.Range("D1") = "Pre-Closing"
.Range("F1:G1").Merge
.Range("F1") = "Post-Closing"
.Range("D1:G1").HorizontalAlignment = xlCenter
.Range("D1:G1").Font.Bold = True
.Columns("D:H").NumberFormat = "#,##0.00 ;(#,##0.00)"
End With
For Each sh In ThisWorkbook.Sheets
If sh.Name <> "Combined" Then
lr = sh.Cells.Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
sh.Range("A8", sh.Cells(lr - 3, 6)).Copy
nsh.Cells(Rows.Count, "C").End(xlUp)(2).PasteSpecial xlPasteValues
Application.CutCopyMode = False
x = sh.Range("A3:A" & sh.Range("A" & Rows.Count).End(xlUp).Row).Rows.Count - 3
If nsh.Range("B" & Rows.Count).End(xlUp).Row < 3 Then
nsh.Range("B3").Resize(x).Value = sh.Range("A4").Value
Else
nsh.Range("B" & Rows.Count).End(xlUp)(2).Resize(x).Value = sh.Range("A4").Value
End If
Set nRng = nsh.Range("B3", nsh.Cells(Rows.Count, "B").End(xlUp))
For Each c In nRng
c.Offset(, -1) = Left(c.Value, 4)
Next
nRng.Offset(, -1).NumberFormat = "0000"
End If
Next
ActiveWindow.DisplayGridlines = False
Columns.AutoFit
Range("A1").Select
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Last edited: