Originally Posted by
GTO
Greetings Colm,
With some junk copies of the workbooks, try this.
Please note to change the extensions back to 2007 type.
Also - I didn't want to get too far before checking one thing. When determining a record to be new, can we count on AETerm for unique values - and - will these always be in Col E?
[vba]
Option Explicit
Sub CopyCommentsForward()
Dim wbkOld As Workbook, wbkNew As Workbook
Dim varOldWb, varNewWb, varSourceData
Dim wksSource As Worksheet, wksTarget As Worksheet
Dim strSheetName As String, strFirstAddress As String
Dim lngCommentColumn As Long, lngCounter As Long, lngRowCount As Long
Dim blnMismatch As Boolean
Dim rngFinder As Range, rngSourceData As Range, rngCell As Range
Dim rngAETermNew As Range, rngAETermOld As Range, rngFindMatch As Range
'// just cuz I'm in 2000/2003 - CHANGE BACK to .xlsx//
varOldWb = Application.GetOpenFilename( _
"Microsoft Excel Files (*.xls), *.xls", , "Select workbook for last data set")
If varOldWb <> False Then
Set wbkOld = Workbooks.Open(varOldWb)
End If
'// SAA//
varNewWb = Application.GetOpenFilename( _
"Microsoft Excel Files (*.xls), *.xls", , "Select workbook for current data set")
If varNewWb <> False Then
Set wbkNew = Workbooks.Open(varNewWb)
End If
Application.ScreenUpdating = False
For Each wksSource In wbkOld.Worksheets
strSheetName = wksSource.Name
On Error Resume Next
' Check same sheet exists in new workbook
Set wksTarget = wbkNew.Worksheets(strSheetName)
If Not wksTarget Is Nothing Then
lngCommentColumn = wksSource.Range("IV1").End(xlToLeft).Column
'// If Row 1 is always a header, maybe start checking at Row 2
'Set rngSourceData = wksSource.Columns(lngCommentColumn) _
.SpecialCells(xlCellTypeConstants)
Set rngSourceData = _
wksSource.Range(wksSource.Cells(2, lngCommentColumn), _
wksSource.Cells(Rows.Count, lngCommentColumn)) _
.SpecialCells(xlCellTypeConstants)
'// Only if Col 5 (E) can be counted on to be unique identifier for the //
'// record... Set references to these in both the new and old wb's //
Set rngAETermNew = _
wksTarget.Range("E2:E" & wksTarget.Cells(Rows.Count, "E").End(xlUp).Row)
Set rngAETermOld = _
wksSource.Range("E2:E" & wksSource.Cells(Rows.Count, "E").End(xlUp).Row)
'// for each ID in the new wb... //
For Each rngCell In rngAETermNew
'// see if we find the same AETerm in the old wb.//
Set rngFindMatch = rngAETermOld.Find(What:=rngCell.Value, _
After:=rngAETermOld(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext)
'// rngFindMatch will return Nothing if not found in the old wb, so we //
'// know its a new record. //
If rngFindMatch Is Nothing Then
rngCell.Offset(, -4).Resize(, 5).Interior.ColorIndex = 6
End If
Next
For Each rngCell In rngSourceData
Set rngFinder = wksTarget.Columns(1).Find( _
What:=wksSource.Cells(rngCell.Row, 1), LookAt:=xlWhole)
If Not rngFinder Is Nothing Then
strFirstAddress = rngFinder.Address
Do
blnMismatch = False
For lngCounter = 1 To lngCommentColumn - 2
If rngFinder.Offset(0, lngCounter) <> _
wksSource.Cells(rngCell.Row, 1).Offset(0, lngCounter) Then
blnMismatch = True
Exit For
End If
Next lngCounter
If Not blnMismatch Then
wksTarget.Cells(rngFinder.Row, lngCommentColumn) = rngCell
Else
Set rngFinder = wksTarget.Columns(1).FindNext(rngFinder)
End If
Loop While blnMismatch = True _
And rngFinder.Address <> strFirstAddress _
And Not rngFinder Is Nothing
End If
Next rngCell
End If
Next wksSource
wbkNew.Save
wbkNew.Close
wbkOld.Close False
Application.ScreenUpdating = True
Set wksSource = Nothing
Set wksTarget = Nothing
Set wbkNew = Nothing
Set wbkOld = Nothing
End Sub
[/vba]
Hope this helps,
Mark