I want to compare column A of worksheet 1 and worksheet 2, If there is an extra value in worksheet 2, I insert a blank row in worksheet 1 at the exact same place as worksheet 2. After the worksheets columns A match, I need to subtract all the cells in the columns generally starting from column B to however long the range goes. ( I need a way to find the total range of the sheet with numerical values excluding the headers)
The first part of the code ( to compare), takes two sheets called sheet 1 and sheet 2, can someone tell me how Instead of hardcoding the sheet names as sheet 1 and sheet 2, I can select sheets having keywords (before , after) That is: sheet 1 = any sheet that has "before" in its name and sheet 2 = any sheet that has "after" in its name.
Then I need to subtract sheet2 - sheet1, and put the difference in a new created sheet called variance.
View attachment 74289 This is how a typical sheet would look like ( The numbers here are cost budgets)
The first part of the code ( to compare), takes two sheets called sheet 1 and sheet 2, can someone tell me how Instead of hardcoding the sheet names as sheet 1 and sheet 2, I can select sheets having keywords (before , after) That is: sheet 1 = any sheet that has "before" in its name and sheet 2 = any sheet that has "after" in its name.
Then I need to subtract sheet2 - sheet1, and put the difference in a new created sheet called variance.
View attachment 74289 This is how a typical sheet would look like ( The numbers here are cost budgets)
VBA Code:
Sub Compare()
Dim lastRowE As Integer
Dim lastRowF As Integer
Dim lastRowM As Integer
Dim foundTrue As Boolean
Application.ScreenUpdating = False
LastRow1 = Sheets("sheet1").Cells(Sheets("sheet1").Rows.Count, "A").End(xlUp).Row
lastRow2 = Sheets("sheet2").Cells(Sheets("sheet2").Rows.Count, "A").End(xlUp).Row
For i = 1 To lastRow2
foundTrue = False
For j = 1 To LastRow1
If Sheets("sheet2").Cells(i, 1).Value = Sheets("sheet1").Cells(j, 1).Value Then
foundTrue = True
Exit For
End If
Next j
If Not foundTrue Then
Sheets("Sheet1").Rows(i).EntireRow.Insert Shift:=xlShiftDown
Sheets("sheet2").Cells(i).Copy Destination:= _
Sheets("Sheet1").Cells(i, 1)
End If
Next i
Application.ScreenUpdating = True
End Sub