Question

I have to replace one character with another in Excel file.

I have used following Replace function, but due to exceeds of 1024 character limit in some cells, it stops there.

Sub Replace()

    With Sheets("Sheet1").Range("A1:A629")   

      .Cells.Replace ",", ";", xlPart, xlByRows, False

    End With

End Sub 

I got to know Substitute function would do

Cells(1, 2) = "=SUBSTITUTE(A1,"","","";"")"

But how do I use that for cell range?

Was it helpful?

Solution

Try this

Sub Replace()

    Dim rng As Range, cell As Range
    Set rng = Sheets("Sheet1").Range("A1:A629")

    For Each cell In rng
        cell = WorksheetFunction.Substitute(cell, ",", ";")
    Next
End Sub

enter image description here

OTHER TIPS

Try this. Note that it uses the VBA Replace function, so you need to rename your 'Replace` subroutine.

Sub ReplaceText()

    For Each c In Sheets("Sheet1").Range("A1:A629").Cells

      c = Replace(c.Value, ",", ";")

    Next c

End Sub

Note: This will only work if you have values in the cells, no Formulas. Because Excel has a formula length limit of 1024 characters. But given you have this specific error, your cells must not be formulas.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top