I'm trying to write a macro for excel that will find the data in a selected cell (H5 in my project). It needs to replace any special character such as "/" with a "-", then truncate the data down to 31 characters so that the data can be used to rename the appropriate worksheet. If there are multiple worksheets in the workbook, it needs to continue to the next worksheet and perform the same functions as before...until its done.
Here is the code I'm trying to use. It runs until it reaches the first instance of a special character or more than 31 characters and fails to apply the changes requested in the macro. Any suggestions?
Sub Rename()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Document map" Then
Range("H5").Replace What:="/", Replacement:="-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("H5") = Left(Range("H5"), 31)
ws.Select
ws.Name = Range("H5").Value
End If
Next ws
End Sub
Here is the code I'm trying to use. It runs until it reaches the first instance of a special character or more than 31 characters and fails to apply the changes requested in the macro. Any suggestions?
Sub Rename()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Document map" Then
Range("H5").Replace What:="/", Replacement:="-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Range("H5") = Left(Range("H5"), 31)
ws.Select
ws.Name = Range("H5").Value
End If
Next ws
End Sub