Just change one line
            Range("X5").Offset(0, c - 1).Resize(.Count).Value = Application.Transpose(.Keys)
to
            If .Count Then Range("X5").Offset(0, c - 1).Resize(.Count).Value = Application.Transpose(.Keys)