Lindsay0385
New Member
- Joined
- Dec 21, 2016
- Messages
- 30
Hello,
I found a macro a long while ago online that creates a summary worksheet by pulling all info from the other sheets in a workbook. It works great, but the issue I'm having is that when I use references to that sheet, they change to #REF when the sheet is updated. Is there a way to change this so the sheet isn't deleted and replaced, but just cleared and reused so the references to the sheet continue to work?
Thanks,
Lindsay
I found a macro a long while ago online that creates a summary worksheet by pulling all info from the other sheets in a workbook. It works great, but the issue I'm having is that when I use references to that sheet, they change to #REF when the sheet is updated. Is there a way to change this so the sheet isn't deleted and replaced, but just cleared and reused so the references to the sheet continue to work?
Thanks,
Lindsay
Code:
Function LastRow(sh As Worksheet)
On Error Resume Next
LastRow = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
On Error GoTo 0
End Function
Function LastCol(sh As Worksheet)
On Error Resume Next
LastCol = sh.Cells.Find(What:="*", _
After:=sh.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
On Error GoTo 0
End Function
Sub SummaryMacro()
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 summary sheet if it exists.
Application.DisplayAlerts = False
On Error Resume Next
ActiveWorkbook.Worksheets("Summary").Delete
On Error GoTo 0
Application.DisplayAlerts = True
' Add a new summary worksheet.
Set DestSh = ActiveWorkbook.Worksheets.Add
DestSh.Name = "Summary"
' Fill in the start row.
StartRow = 2
' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
' Exclude certain sheets from the loop
Select Case sh.Name
Case Is = "Dashboard"
' Leave this section blank to exclude the above sheets
Case Else
' Code for all sheets NOT exlcuded
If sh.Name <> DestSh.Name Then
' Find the last row with data on the summary
' and source worksheets.
Last = LastRow(DestSh)
shLast = LastRow(sh)
' If source worksheet is not empty and if the last
' row >= StartRow, copy the range.
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 to see whether there are enough rows in the summary
' worksheet to copy all the data.
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the " & _
"summary worksheet to place the data."
End If
' This statement copies values and formats.
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
End If
End Select
Next sh
Application.Goto DestSh.Cells(1)
'A BUNCH OF OTHER CODE HERE FOR FORMATTING THE SHEET
End Sub