New to Excel so excuse me if I explain something wrong, happy to clear up any Q's in the comments, my VBA is most likely awful but I was only asked to do this yesterday for the first time.
I'm trying to clear carriage returns across all worksheets using a single button. I've found a way to clear them in the current worksheet and this runs fine:
Sub RemoveCarriageReturns()
Dim MyRange As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each MyRange In ActiveSheet.UsedRange
If 0 < InStr(MyRange, Chr(10)) Then
MyRange = Replace(MyRange, Chr(10), "")
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I've searched for several ways to get this to run across all Worksheets, however none seem to work, my current code is:
Sub RemoveCarriageReturns()
Dim MyRange As Range
Dim ws As Worksheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
For Each ws In Worksheets
For Each MyRange In ActiveSheet.UsedRange
Select Case UCase(ws.Name)
Case "OTCUEXTR", "OTFBCUDS", "OTFBCUEL"
With ws
If 0 < InStr(MyRange, Chr(10)) Then
MyRange = Replace(MyRange, Chr(10), "")
End If
End With
End Select
Next
Next ws
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
I can see that its running through all sheets, but nothing changes beyond the first sheet (which works fine and removes the carriage returns.) Any advice on what I'm doing wrong here?
Apologies if this has been answered before, I've been unable to find a solution so far online.
Thanks for your help!
ActiveSheetwithwsshould work.