I am new to VBA in Excel. I have a spreadsheet that always has 172 columns. A:FP. It may have 2 to many rows. For every cell in the spreadsheet, I want to remove all leading and trailing spaces along with any leading or trailing "/" or "\". A string can contain a back or forward slash, just not at the beginning or end of the string. My code removes leading and trailing blank spaces. It removes trailing back or forward slashes. However, it does not remove leading slashes. I cannot understand why. Is there a better way of accomplishing my goal? Thank you for your help.
For Each Rng In ActiveSheet.UsedRange
CellVal = Trim(Rng.Value)
LengT = Trim(Len(CellVal))
If CellVal <> "" Then
If Not Rng.HasFormula Then
While Trim(Left(CellVal, 1) = Chr(47)) Or Trim(Left(CellVal, 1) = Chr(92)) Or Trim(Right(CellVal, 1) = Chr(47)) Or Trim(Right(CellVal, 1) = Chr(92))
If Trim(Left(CellVal, 1) = Chr(47)) Or Trim(Left(CellVal, 1) = Chr(92)) Then
CellVal = Trim(Mid(CellVal, 2, LengT))
ElseIf Trim(Right(CellVal, 1) = Chr(47)) Or Trim(Right(CellVal, 1) = Chr(92)) Then
LengT = Len(CellVal)
CellVal = Trim(Left(CellVal, LengT - 1))
Rng.Value = CellVal
LengT = LengT - 1
End If
Wend
End If
End If
Next Rng