keiranwyllie
New Member
- Joined
- May 12, 2017
- Messages
- 47
Hello gurus, I've been trying to nut this one out but it's got me stumped.
Scenario: I have two worksheets. The 2nd (System Security Plan Annex Template (March 2023)) is considered a new version of the first (System Security Plan Annex Template (December 2022)) therefore it will mostly have the same data on each row however there may be more, or may be less rows depending on what the revision commits.
The code I've tried working on, which is run from the original sheet, endeavours to do the following:
1. Create 'newSheet' and copy data from Sheet1 (up to column N down to lastrow), on to newSheet and converts it to a table - This works
2. Opens the updated workbook and imports sheet1 (up to column N down to lastrow(which is most likely different to original workbook)) to newSheet, pasting at cell P1, then converts to table - this also works
3. Retains any comments as the comparison completes that may have existed in columns O,P and Q (these will align with the value from column D)
4. Compares the two tables with the intent of meeting the following criteria:
a. Firstly compares each row entry in column D of the two tables
b. Then compares by revision date (if revision date of same items from 3a is a later revision in the 2nd workbook, it should keep that row)
c. Spits out a new list on a new sheet that retains anything from the original worksheet that was NOT different and also adds anything new (meaning new revision dates as well). It should also remove anything, based on column D, that is not in the updated worksheet as it's expected that it has been removed as part of the update.
The pièce de résistance would be that the final product then replace the original data on sheet1 of the original workbook - essentially completing the import of an updated dataset.
Workbook original - Original Data
Updated workbook - New Version
Any guidance on how to modify the code below would be great.
Scenario: I have two worksheets. The 2nd (System Security Plan Annex Template (March 2023)) is considered a new version of the first (System Security Plan Annex Template (December 2022)) therefore it will mostly have the same data on each row however there may be more, or may be less rows depending on what the revision commits.
The code I've tried working on, which is run from the original sheet, endeavours to do the following:
1. Create 'newSheet' and copy data from Sheet1 (up to column N down to lastrow), on to newSheet and converts it to a table - This works
2. Opens the updated workbook and imports sheet1 (up to column N down to lastrow(which is most likely different to original workbook)) to newSheet, pasting at cell P1, then converts to table - this also works
3. Retains any comments as the comparison completes that may have existed in columns O,P and Q (these will align with the value from column D)
4. Compares the two tables with the intent of meeting the following criteria:
a. Firstly compares each row entry in column D of the two tables
b. Then compares by revision date (if revision date of same items from 3a is a later revision in the 2nd workbook, it should keep that row)
c. Spits out a new list on a new sheet that retains anything from the original worksheet that was NOT different and also adds anything new (meaning new revision dates as well). It should also remove anything, based on column D, that is not in the updated worksheet as it's expected that it has been removed as part of the update.
The pièce de résistance would be that the final product then replace the original data on sheet1 of the original workbook - essentially completing the import of an updated dataset.
Workbook original - Original Data
Updated workbook - New Version
Any guidance on how to modify the code below would be great.
VBA Code:
Sub CompareAndCreateNewTable()
Dim ws, srcBook, srcSheet As Worksheet
Dim lastRow, lastCol, rng, lastRowTable1, lastRowTable2 As Range
Set ws = Sheets(1)
Set NewSheet = ThisWorkbook.Worksheets.Add
ActiveSheet.Name = "newSheet"
lastRowTable1 = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastCol = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set rng = ws.Range("A1").Resize(lastRowTable1, lastCol - 3) 'Exclude the last 4 columns
rng.Copy NewSheet.Range("A1")
'ActiveSheet.ListObjects.Add(xlSrcRange, ActiveSheet.UsedRange, , xlYes).Name = "Table1"
ActiveSheet.ListObjects.Add(xlSrcRange, Range("A1", Range("A1").End(xlToRight).End(xlDown)), , xlYes).Name = "Table1"
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight2"
'Prompt the user to select a file
Dim fileName As String
fileName = Application.GetOpenFilename("Excel files (*.xlsx), *.xlsx")
'Open the file and get a reference to the source sheet
Set srcBook = Workbooks.Open(fileName)
Set srcSheet = srcBook.Worksheets(1)
lastRow = srcSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
lastCol = srcSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Set rng = srcSheet.Range("A1").Resize(lastRow, lastCol - 3)
rng.Copy NewSheet.Range("P1")
srcBook.Close False
ActiveSheet.ListObjects.Add(xlSrcRange, Range("P1", Range("P1").End(xlToRight).End(xlDown)), , xlYes).Name = "Table2"
ActiveSheet.ListObjects("Table2").TableStyle = "TableStyleLight2"
' Get the last row of Table1
'lastRowTable1 = Range("Table1").Rows.Count
' Get the last row of Table2
'lastRowTable2 = Range("Table2").Rows.Count
' Initialize the new data array
ReDim newData(1 To lastRowTable1, 1 To 5)
' Compare the tables based on column 4
newRow = 1 ' Initialize the row counter for the new table
For i = 1 To lastRowTable1
Set cellTable1 = Range("Table1").Cells(i, 14)
foundMatch = False ' Reset the flag for each row in Table1
For j = 1 To lastRow
Set cellTable2 = Range("Table2").Cells(j, 14)
' If the values in column 4 are equal, compare the other columns
If cellTable1.Value = cellTable2.Value Then
foundMatch = True
' Compare the other columns
If cellTable1.Offset(0, -2).Value <> cellTable2.Offset(0, -2).Value _
Or cellTable1.Offset(0, -1).Value <> cellTable2.Offset(0, -1).Value _
Or cellTable1.Offset(0, 0).Value <> cellTable2.Offset(0, 0).Value _
Or cellTable1.Offset(0, 1).Value <> cellTable2.Offset(0, 1).Value _
Or cellTable1.Offset(0, 2).Value <> cellTable2.Offset(0, 2).Value _
Or cellTable1.Offset(0, 3).Value <> cellTable2.Offset(0, 3).Value _
Or cellTable1.Offset(0, 4).Value <> cellTable2.Offset(0, 4).Value _
Or cellTable1.Offset(0, 5).Value <> cellTable2.Offset(0, 5).Value _
Or cellTable1.Offset(0, 6).Value <> cellTable2.Offset(0, 6).Value _
Or cellTable1.Offset(0, 7).Value <> cellTable2.Offset(0, 7).Value _
Or cellTable1.Offset(0, 8).Value <> cellTable2.Offset(0, 8).Value _
Or cellTable1.Offset(0, 9).Value <> cellTable2.Offset(0, 9).Value _
Or cellTable1.Offset(0, 10).Value <> cellTable2.Offset(0, 10).Value _
Or cellTable1.Offset(0, 11).Value <> cellTable2.Offset(0, 11).Value _
Or cellTable1.Offset(0, 12).Value <> cellTable2.Offset(0, 12).Value _
Or cellTable1.Offset(0, 13).Value <> cellTable2.Offset(0, 13).Value Then
' Skip this row if there are differences
Exit For
End If
' If there are no differences, add the row to the new table
newData(newRow, 1) = cellTable1.Offset(0, -3).Value ' Column A
newData(newRow, 2) = cellTable1.Offset(0, -2).Value ' Column B
newData(newRow, 3) = cellTable1.Offset(0, -1).Value ' Column C
newData(newRow, 4) = cellTable1.Value ' Column D
newData(newRow, 5) = cellTable1.Offset(0, 1).Value ' Column E
newData(newRow, 6) = cellTable1.Offset(0, 2).Value ' Column F
newData(newRow, 7) = cellTable1.Offset(0, 3).Value ' Column G
newData(newRow, 8) = cellTable1.Offset(0, 4).Value ' Column H
newData(newRow, 9) = cellTable1.Offset(0, 5).Value ' Column I
newData(newRow, 10) = cellTable1.Offset(0, 6).Value ' Column J
newData(newRow, 11) = cellTable1.Offset(0, 7).Value ' Column K
newData(newRow, 12) = cellTable1.Offset(0, 8).Value ' Column L
newData(newRow, 13) = cellTable1.Offset(0, 9).Value ' Column M
newData(newRow, 14) = cellTable1.Offset(0, 10).Value ' Column N
newRow = newRow + 1 ' Increment the row counter for the new table
Exit For ' Move on to the next row in Table1
End If
Next j
' If no match was found for this row in Table1, skip the row
If Not foundMatch Then
' Skip this row
End If
Next i
' Write the new table to a new sheet
Set NewSheet = ThisWorkbook.Sheets.Add ' Create a new sheet
' Write the headers
NewSheet.Range("A1:N1").Value = Array("Guideline", "Section", "Topic", "Identifier", "Revision", "Updated", "All", "O", "P", "S", "TS", "ML2", "ML3", "Description")
' Write the data
NewSheet.Range("A2").Resize(UBound(newData, 1), UBound(newData, 2)).Value = newData
' Convert the dates in Column F to Date format
NewSheet.Range("E:E").NumberFormat = "MMM-YY"
' Autofit the columns
NewSheet.Columns.AutoFit
' Notify the user
MsgBox "Comparison completed. Please check the new sheet for the results."
End Sub