So I am computing a date 18 months out from start date then deleting every column after 18 months.
E2 is where I currently store the calculated 18 months out value. So this week, 18 months out is 7/24/2023.. I need this to search for 7/24/2023 in row 1, find it, then delete every column after.
Range(Cells(1, Range("1:1").Find(What:=Range("E2").Value.Column 1),
_ Cells(1, Cells(1, Columns.Count).End(xlToLeft).Column)).EntireColumn.Delete
CodePudding user response:
Your 1 is in the wrong place
When using find() it's best to check you got a match:
Dim f as Range, ws as Worksheet
set ws = Activesheet
Set f = ws.Rows(1).Find(What:=ws.Range("E2").Value)
If Not f Is Nothing then
ws.Range(f.Offset(0, 1), ws.Cells(1, ws.Columns.count).End(xlToLeft)).EntireColumn.Delete
End if
