Hi, when I run the code, it somehow deletes the " 2021 Clients" cell E1 on Sheet2. can it be fixed ,please?
thats how first row on Sheet 2 is
but after running the code
WorksheetDummy.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | 2022 Clients | 2022 Data | 2022 Data2 | 2022 Data3 | 2021 Clients | 2021 Data | 2021 Data2 | 2021 Data3 | ||
2 | Michael | 1234 | 1234 | 2345 | James | 2 | 3 | 1 | ||
3 | Sarah | 4321 | 54 | 67 | Sam | 3 | 4 | 3 | ||
4 | Mary | 9876 | 0 | 34 | Peter | 6543 | 5 | 1 | ||
5 | Rachel | 5678 | 12 | 45 | Peter | 13 | 5 | 2 | ||
6 | Anna | 23 | 0 | Shaw | 4 | 4 | 0 | |||
7 | Monica | 2356 | 45 | 45 | Sally | 14 | 345 | 1 | ||
8 | Charles | 1234 | 0 | 0 | Michelle | 13 | 6 | 64 | ||
9 | Peter | 6543 | 23 | 0 | Ivona | 5 | 4 | 3 | ||
10 | Anthony | 1234 | 23 | 0 | Anna | 345 | 3 | 4 | ||
11 | Ben | 1234 | 56 | 34 | Claire | 4 | 6 | 4 | ||
12 | Elizabeth | 6543 | 0 | 78 | Ben | 1234 | 1 | 5 | ||
13 | Wong | 1276 | 23456 | 12 | Ben | 5435 | 2 | 6 | ||
14 | Sally | 9854 | 23 | 56 | Michael | 34346 | 3 | 78 | ||
15 | Ben | 34 | 3 | David | 54654 | 1234 | 3 | |||
16 | Jay | 4325 | 8765 | 89 | Annaleise | 0 | 4 | 87 | ||
17 | Anthony | 1 | 2 | 3 | Russell | 34656 | 4 | 78 | ||
18 | Michelle | 1265 | 1256 | 64 | Chris | 5 | 1 | 5 | ||
19 | David | 1276 | 1234 | 3 | Beth | 4 | 1 | 56 | ||
20 | Jennifer | 1234 | 8765 | 56 | John | 3 | 2 | 35 | ||
21 | Anthony | 98 | 9 | 7 | Bob | 3 | 2 | 456 | ||
22 | Sue | 34 | 78 | Anthony | 1 | 23 | 567 | |||
23 | Ruba | 3254 | 23 | 9 | Anthony | 98 | 5 | 67 | ||
24 | Henry | 9876 | 78 | 0 | Josh | 3 | 5 | 67 | ||
25 | Chloe | 3245 | 34567 | 6 | Sue | 3 | 34 | 78 | ||
26 | Anna | 5 | 6 | 7 | Choung | 4 | 7 | 7 | ||
27 | Candy | 2389 | 356 | 5 | Robin | |||||
28 | Monica | 4656 | 1 | 2 | ||||||
29 | Peter | 7 | 6 | 8 | ||||||
30 | Joe | 2345 | 34 | 1 | ||||||
31 | Peter | 56 | 6 | 4 | ||||||
Sheet1 |
thats how first row on Sheet 2 is
WorksheetDummy.xlsm | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | 2022 Clients | 2022 Data | 2022 Data2 | 2022 Data3 | 2021 Clients | 2021 Data | 2021 Data2 | 2021 Data3 | ||
Sheet2 |
but after running the code
WorksheetDummy.xlsm | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | 2022 Clients | 2022 Data | 2022 Data2 | 2022 Data3 | 2021 Data | 2021 Data2 | 2021 Data3 | ||
2 | Peter | 6543 | 23 | 0 | |||||
3 | Peter | 7 | 6 | 8 | |||||
4 | Peter | 56 | 6 | 4 | |||||
5 | Sally | 9854 | 23 | 56 | |||||
6 | Michelle | 1265 | 1256 | 64 | |||||
Sheet2 |
VBA Code:
Sub CopyOldNewClients()
Dim shtOld As Worksheet, shtNew As Worksheet
Dim rngOld As Range, rngLookup As Range, rngNew As Range
Dim lrowOld As Long, lrowLookup As Long, lcolNew As Long
Application.ScreenUpdating = False
Set shtOld = Worksheets("Sheet1")
Set shtNew = Worksheets("Sheet2")
lrowOld = shtOld.Range("A" & Rows.Count).End(xlUp).Row
Set rngOld = shtOld.Range("A2:D" & lrowOld)
lrowLookup = shtOld.Range("E" & Rows.Count).End(xlUp).Row
Set rngLookup = shtOld.Range("E2:E" & lrowLookup)
rngOld.Copy
shtNew.Range("A2").PasteSpecial
lcolNew = rngOld.Columns.Count + 1
Set rngNew = shtNew.Range(rngOld.Address).Resize(, lcolNew)
rngNew.Columns(lcolNew).Formula = "=IfError(Match(" & rngNew.Cells(1, 1).Address(0, 1) & "," & rngLookup.Address(external:=True) & ", 0), 999999)"
rngNew.Columns(lcolNew).Value = rngNew.Columns(lcolNew).Value
Set rngNew = rngNew.Offset(-1).Resize(rngNew.Rows.Count + 1)
shtNew.Sort.SortFields.Clear
shtNew.Sort.SortFields.Add2 Key:=rngNew.Columns(lcolNew) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange rngNew
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' XXX Additional code to colour code the old and new clients
Dim rowFirstNew As Long
Dim lrowNew As Long
lrowNew = shtNew.Cells(Rows.Count, lcolNew).End(xlUp).Row
With Application
rowFirstNew = .IfError(.Match(999999, rngNew.Columns(lcolNew), 0), 0)
If rowFirstNew <> 0 Then
With rngNew
.Range(.Cells(1, 1), .Cells(rowFirstNew - 1, lcolNew)).Interior.Color = RGB(239, 255, 254)
.Range(.Cells(rowFirstNew, 1), .Cells(lrowNew, lcolNew)).Interior.Color = RGB(250, 255, 203)
End With
End If
End With
' XXX End of additional code
rngNew.Columns(lcolNew).EntireColumn.Delete
shtNew.Activate
shtNew.Range("A1").Select
Application.CutCopyMode = False
Application.ScreenUpdating = False
End Sub