EXCEL VBA CODE for replace duplicate semi colon in single cell

Jackyk1994

New Member
Joined
Jul 16, 2024
Messages
5
Office Version
  1. 365
Platform
  1. Windows
I have many data in Cell A3 to A lastrow

For example like this:
abc;;;ab;;ccc;;;;;;;wwwwww

I want the result to replace at the same cell like this:
abc;ab;ccc;wwwwww

The quantity of semicolons are unknown

Tried a lot of AI generated results but failed, i am a beginner in VBA, thank you for your help
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
How about

VBA Code:
Sub jec()
 Dim ar, j As Long
 ar = Range("A3", Range("A" & Rows.Count).End(xlUp))
 With CreateObject("vbscript.regexp")
  For j = 1 To UBound(ar)
    .Global = True
    .Pattern = ";(?=;)"
    ar(j, 1) = .Replace(ar(j, 1), "")
  Next
  Range("A3", Range("A" & Rows.Count).End(xlUp)) = ar
 End With
End Sub
 
Upvote 0
try
Code:
Sub test()
    Dim a, x, i&, ii&, n&
    With Range("c3", Range("c" & Rows.Count).End(xlUp))
        a = .Resize(, 2).Value
        For i = 1 To UBound(a, 1)
            x = Split(a(i, 1), ";"): n = 0
            For ii = 0 To UBound(x)
                If x(ii) <> "" Then x(n) = x(ii): n = n + 1
            Next
            If n Then
                ReDim Preserve x(n - 1)
                a(i, 1) = Join(x, ";")
            End If
        Next
        .Value = a
    End With
End Sub

abc;;;ab;;ccc;;;;;;;wwwwww;;;;
abc;;;ab;;ccc;;;;;;;wwwwww
abc;;;ab;;ccc;;;;;;;wwwwww
abc;;;ab;;ccc;;;;;;;wwwwww
abc;;;ab;;ccc;;;;;;;wwwwww
abc;;;ab;;ccc;;;;;;;wwwwww

To
abc;ab;ccc;wwwwww
abc;ab;ccc;wwwwww
abc;ab;ccc;wwwwww
abc;ab;ccc;wwwwww
abc;ab;ccc;wwwwww
abc;ab;ccc;wwwwww
 
Upvote 0
or even

VBA Code:
Sub jecc()
 Dim j As Long
 For j = 1 To 10
  Range("A3", Range("A" & Rows.Count).End(xlUp)).Replace ";;", ";"
 Next
End Sub
 
Upvote 0
Another VBA option:
VBA Code:
Sub test()
  Dim ws As Worksheet, rRng As Range
  
  Set ws = Sheets("Sheet1")
  Set rRng = ws.Range("A3:A" & ws.Range("A" & Rows.Count).End(xlUp).Row)
  rRng = Evaluate("SUBSTITUTE(TRIM(SUBSTITUTE(" & rRng.Address(, , , 1) & ","";"","" "")),"" "","";"")")
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,557
Messages
6,160,478
Members
451,650
Latest member
kibria

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top