cell is empty while using For Each cell In Range
I've got a function which check for a particular date and then format columns around it. It was previously working when I use it within a Macro and open the file I want to work on manually and manually execute the Macro. However, when I move the code to a button and try to have VBA to automatically open the files with a folder and execute the existing formating code, it's not working anymore. The problem exist within the function below. The variable "cell" appears to be empty for some reason? Can any offer any help?
Function FormatSplit(sDate As String, wb As Workbook, Optional splitLeft As Boolean)
For Each cell In Range(Range("a2"), Range("IV2").End(xlToLeft))
If Format(cell.Value, "dd/mm/yyyy") = sDate Then
If splitLeft = True Then
Range(cell, Cells(1, Columns.Count)).EntireColumn.Delete
Else
iCount = Range("b2", cell).Count
Range("b2", Cells(1, iCount)).EntireColumn.Delete
End If
Exit For
End If
Next cell
End Function
|