If have the below code in the attached file. The file is updated weekly, so CW (current week) will has the data of W-1 plus CW (current week) to be updated, and the code needs to cross check if the data already exists or not. The code is trigger by the existing butonn in tab "Week Update". There are aproximately 4 checks, but only one is not working, which is:
Whats already working?
Click here to access the file
- If the line already exist in tab "Data" (using the collunm AV as key to check), but do not exist in the data of CW (current week) in tab "Week Update"(using the collunm AK as key to check), then the correspondent line in collunm AT in tab "Data", will switch to "Historical";
Whats already working?
- New lines are added on tab "data" from "Week Update", if do not exists yet, and the collunm AT mus be [blank]; OK
- If an existing line exists on "Data" and collunm AT is "Historical", and the it also exists on CW (current week) in tab "Week Update", then switch from "Historical" to ""; OK
Click here to access the file
VBA Code:
Sub DataUpdate()
Dim Dary As Variant, Hary As Variant, Uary As Variant, Nary As Variant, Nhary As Variant
Dim i As Long, c As Long, UsdRws As Long, nr As Long
Dim Dic As Object
Application.ScreenUpdating = False
Worksheets("Data").Unprotect Password:="Henkel2020"
Set Dic = CreateObject("scripting.dictionary")
With Sheets("Week Update")
UsdRws = .Range("C" & Rows.Count).End(xlUp).Row
Uary = .Range("A3:AK" & UsdRws)
End With
With Sheets("Data")
UsdRws = .Range("C" & Rows.Count).End(xlUp).Row
Dary = .Range("AV3:AV" & UsdRws).Value2
Hary = .Range("AT3:AT" & UsdRws).Value2
End With
For i = 1 To UBound(Dary)
Dic(Dary(i, 1)) = i
Next i
With Sheets("Data")
Dary = .Range("A3:AJ" & UsdRws).Value2
End With
ReDim Nary(1 To UBound(Uary), 1 To 36)
For i = 1 To UBound(Uary)
If Dic.Exists(Uary(i, 37)) Then
For c = 1 To 36
Dary(Dic(Uary(i, 37)), c) = Uary(i, c)
Next c
If Hary(Dic(Uary(i, 37)), 1) = "Historical" Then Hary(Dic(Uary(i, 37)), 1) = ""
Else
nr = nr + 1
For c = 1 To 36
Nary(nr, c) = Uary(i, c)
Next c
Hary(i, 1) = "Historical"
End If
Next i
With Sheets("Data")
.Range("A3:AJ" & UsdRws).Value = Dary
.Range("AT3:AT" & UsdRws).Value = Hary
If nr > 0 Then
.Range("A" & UsdRws + 1).Resize(nr, 36).Value = Nary
.Range("AT" & UsdRws + 1).Resize(nr, 1).Value = Nhary
End If
End With
Sheets("Week Update").ListObjects(1).DataBodyRange.EntireRow.Delete
Sheets("Week Update").Range("Update[Document NumberDocument Line Number]") = "=[@[Document Number]]&[@[Document Line Number]]"
Worksheets("Data").Protect Password:="Henkel2020", DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowFiltering:=True, UserInterfaceOnly:=True
Worksheets("Data").EnableOutlining = True
Application.ScreenUpdating = True
End Sub