I have a data supply worksheet and in that are my two key identifying columns "address" (unique values) and "name" (each name has multiple assigned addresses).
Each name is assigned its own worksheet which is constantly edited and all information collected in a master at the end.
What I need to do: Copy and paste over addresses, assigned to the name to each worksheet if that address is not in that worksheet, at the bottom.
Things I tried:
- Clear contents of sheets and paste in info unable to do so as updated information would be lost.
- Match Name to sheetname and paste over complete rows but this is just added to the bottom. Pasting all values that match not only new rows.
- Query to add new addresses - Issues arise when query is refreshed as all information is just overwritten and updated information now doesn't match with address any more.
Sub new_cases()
Dim cell As Range
Dim cmt As Comment
Dim bolFound As Boolean
Dim sheetnames() As String
Dim lngitem As Long, lnglastrow As Long
Dim sht As Worksheet, shtmaster As Worksheet
Dim MatchRow As Variant
Set shtmaster = ThisWorkbook.Worksheets("data_supply")
'collect names for all other sheets
ReDim sheetnames(0)
For Each sht In ThisWorkbook.Worksheets
If sht.Name <> shtmaster.Name Then
sheetnames(UBound(sheetnames)) = sht.Name
ReDim Preserve sheetnames(UBound(sheetnames) + 1)
End If
Next sht
ReDim Preserve sheetnames(UBound(sheetnames) - 1)
For Each cell In shtmaster.Range("P2:P" & shtmaster.Cells(shtmaster.Rows.Count, "P").End(xlUp).Row)
bolFound = False
If Not IsError(Application.Match(cell.Value2, sheetnames, 0)) Then
bolFound = True
Set sht = ThisWorkbook.Worksheets(sheetnames(Application.Match(cell.Value2, sheetnames, 0)))
' Tried finding a way to do unique match for column E
MatchRow = Application.Match(?????????)
If Not IsError(MatchRow) Then
shtmaster.Rows(cell.Row).EntireRow.Copy Destination:=sht.Cells(MatchRow, 1)
Else 'no match in sheet, add the record at the end
On Error GoTo SetFirst
lnglastrow = sht.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row + 1
On Error GoTo 0
shtmaster.Rows(cell.Row).EntireRow.Copy Destination:=sht.Cells(lnglastrow, 1)
End If
End If
If bolFound = False Then
For Each cmt In shtmaster.Comments
If cmt.Parent.Address = cell.Address Then cmt.Delete
Next cmt
cell.AddComment "no sheet found for this row"
ActiveSheet.EnableCalculation = False
ActiveSheet.EnableCalculation = True
End If
Set sht = Nothing
Next
Exit Sub
SetFirst:
lnglastrow = 1
Resume Next
End Sub