Perhaps, with the missing With.
Sub Year_Fix()
Dim Rng1 As Range, Rng2 As Range, Rng3, Rng4 As Range
Dim LastRow As Long
Dim ws1 As Worksheet
For Each ws1 In ActiveWorkbook.Worksheets
With ws1
With .Columns("K:K")
.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End With
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set Rng1 = .Range("N2", .Cells(LastRow, 14)) 'Original
Set Rng2 = .Range("M2", .Cells(LastRow, 13)) 'Year
Set Rng3 = .Range("L2", .Cells(LastRow, 12)) 'Century
Set Rng4 = .Range("K2", .Cells(LastRow, 11)) 'Date
Rng4.Formula = "=IF(N2>0,TEXT(N2,""dd/mm/""),"""")"
Rng3.Formula = "=IF(N2>0,LEFT(TEXT(N2,""yyyy""),2),"""")"
Rng2.Formula = "=IF(N2>0,RIGHT(TEXT(N2,""yyyy""),2),"""")"
Rng4.Select
Rng4.Copy
Rng4.PasteSpecial Paste:=xlPasteValues
Rng3.Copy
Rng3.PasteSpecial Paste:=xlPasteValues
Rng3.Replace What:="19", Replacement:="20", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Rng2.Copy
Rng2.PasteSpecial Paste:=xlPasteValues
' Rng1 = Rng3 & Rng2 & Rng4
Rng1.Copy
Rng1.PasteSpecial Paste:=xlPasteValues
'Remove the helper columns
.Columns("K:M").Delete Shift:=xlToLeft
End With
Next ws1
End Sub
By the way, I've commented out this line because it will error
Rng1 = Rng3 & Rng2 & Rng4
if you are trying to do some sort of concatenation involving 3 ranges have a look at using a formula.
Bookmarks