Insert pipe symbol as first character in all cells of every column in sheet

RogerC

Well-known Member
Joined
Mar 25, 2002
Messages
537
Is there a method to insert a pipe symbol into every cell of my spreadsheet that contains data, to act as a delimiter character?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try:
VBA Code:
Sub InsertPipe()
    Application.ScreenUpdating = False
    Dim v As Variant, r As Long, c As Long, arr() As Variant
    v = ActiveSheet.UsedRange.Cells.Value
    ReDim arr(1 To UBound(v), 1 To UBound(v, 2))
    For r = LBound(v) To UBound(v)
        For c = LBound(v, 2) To UBound(v, 2)
            If v(r, c) <> "" Then
                arr(r, c) = "|" & v(r, c)
            Else
                arr(r, c) = ""
            End If
        Next c
    Next r
    Range("A1").Resize(UBound(v), UBound(v, 2)) = arr
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
or just this?
VBA Code:
Sub Insert_Pipe()
  With ActiveSheet.UsedRange
    .Value = Evaluate(Replace("if(#="""","""",""|""&#)", "#", .Address))
  End With
End Sub
 
Upvote 0
@mumps
To your knowledge, is there a disadvantage to go with a single array?
Code:
Sub InsertPipe_2()
    Application.ScreenUpdating = False
    Dim v As Variant, i As Long, ii As Long
    v = ActiveSheet.UsedRange.Value
    For i = LBound(v) To UBound(v)
        For ii = LBound(v, 2) To UBound(v, 2)
            If Len(v(i, ii)) <> 0 Then v(i, ii) = "|" & v(i, ii)
        Next ii
    Next i
    Range("A1").Resize(UBound(v), UBound(v, 2)) = v
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I found the difference.
With 1,300,208 Cells (100,016 Rows x 13 Columns) of which 952,784 cells have data and 347,424 cells are empty, your suggestion is 0.2 seconds faster.
Every little bit counts.
 
Upvote 0

Forum statistics

Threads
1,224,845
Messages
6,181,298
Members
453,030
Latest member
PG626

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