I have 4 sheets in total that need to be used.
- ServerList1
- ServerList2
- MachineList1
- MachineList2
The sheet names with a (1) next to them are the reports from last week and the sheet names with a (2) next to them are the reports from this week.
In each sheet, there are multiple columns which I get rid of so that all that remains is the column with either the Server Name or the Machine Name
Essentially, I need to compare last weeks report with this weeks report and see what new servers have been added (if any) and what new machines have been added (if any).
Conversely, I need to do the opposite, check what servers have been removed (if any) and what machines have been removed (if any)..
With the below code, it should be simple to accomplish the second part simply by switching the worksheet names..
I found the below code here:
This code does a comparison and copies the new appearances, but there's two issues I am currently experiencing:
1) The code looks like it gets stuck in an infinite loop - I need to exit the code manually
2) On the New Servers-Machines sheet, the results are pasted from row A2 instead of A1
Sub compareSheets()
ThisWorkbook.RefreshAll
Dim rng As Range, c As Range, cfind As Range
Dim ws1 As Worksheet
Set ws1 = Worksheets("New Servers-Machines")
On Error Resume Next
With Worksheets("Last Week Servers")
Set rng = .Range(.Range("A1"), .Range("c1").End(xlDown))
For Each c In rng
c = Replace(c, " ", "")
With Worksheets("This Week Servers")
Set cfind = .Columns("A:A").Cells.Find(what:=c.Value, lookat:=xlWhole)
If cfind Is Nothing Then
c.Resize(1, 1).EntireRow.Copy
ws1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
End With
Next c
Application.CutCopyMode = False
End With
With Worksheets("This Week Servers")
Set rng = .Range(.Range("A1"), .Range("c1").End(xlDown))
For Each c In rng
c = Replace(c, " ", "")
With Worksheets("Last Week Servers")
Set cfind = .Columns("A:A").Cells.Find(what:=c.Value, lookat:=xlWhole)
If cfind Is Nothing Then
c.Resize(1, 1).EntireRow.Copy
ws1.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
End If
End With
Next c
Application.CutCopyMode = False
End With
End Sub
UPDATE:
Public Sub FindDifferences1()
Dim firstRange As Range
Dim secondRange As Range
Dim myCell As Range
Dim wks1 As Worksheet, wks2 As Worksheet, wks3 As Worksheet
'Find Removed Wintel Servers
Set wks1 = ActiveWorkbook.Sheets("Last Week Servers List")
Set wks2 = ActiveWorkbook.Sheets("This Week Servers List")
Set wks3 = ActiveWorkbook.Sheets("New Servers")
Set firstRange = wks1.Range("A:A")
Set secondRange = wks2.Range("A:A")
For Each myCell In firstRange
If myCell <> secondRange.Range(myCell.Address) Then
myCell.Copy
wks3.Cells(Rows.Count, 2).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
wks3.Cells(Rows.Count, 2).End(xlUp).PasteSpecial xlPasteFormats
End If
Next myCell
End Sub
Format of the sheets is only one column with a row header Server Name