VBA: How to remove all extra spaces or a given character in the beginning and end of all cell data?

azov5

New Member
Joined
Dec 27, 2018
Messages
40
1. VBA: How to remove all extra spaces in the beginning and end of all cell data in a sheet ?
2. VBA: How to remove a given character in the beginning and end of all cell data ?
3. VBA: Remove cell data if no text charterer found.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
1. VBA: How to remove all extra spaces in the beginning and end of all cell data in a sheet ?
2. VBA: How to remove a given character in the beginning and end of all cell data ?
3. VBA: Remove cell data if no text charterer found.
1.
Code:
With Sheets(1)
    .Range("A1") = Trim(.Range("A1").Value
End With
2.
Code:
With Sheets(1)
    .Range("A1") = Mid(.Range("A1").Value, 2, Len(.Range("A1").Value) - 2)
End With
3.
Code:
With Sheets(1)
    For i = 1 To Len(.Range("A1").Value)
        If Not Mid(Range("A1").Value, i, 1) Like "[A-Z, a-z]" Then
            .Range("A1").ClearContents
            Exit For
        End If
    Next
End With
 
Upvote 0
Thanks JLGWhiz
2. VBA: How to remove a given character in the beginning and end of all cell data ?

I want to remove comma's "," if found in the beginning or end of all cell data.
 
Upvote 0
Thanks JLGWhiz
2. VBA: How to remove a given character in the beginning and end of all cell data ?

I want to remove comma's "," if found in the beginning or end of all cell data.
Maybe:
Code:
Sub RemoveLeadingOrTrailingComma()
'select cells to alter then run this macro
Dim c As Range
For Each c In Selection
    c.Value = Replace(c.Value, ",", "", 1, 1)
    If InStr(Len(c.Value), c.Value, ",") > 0 Then
        c.Value = Left(c.Value, Len(c.Value) - 1)
    End If
Next c
End Sub
 
Upvote 0
Rich (BB code):
    c.Value = Replace(c.Value, ",", "", 1, 1)
Joe, that will remove the first comma in the cell, even if that comma is not "in the beginning of the data" in the cell.


Two suggestions for problem 2. I'd probably go with the second one in this case as I think it is a bit more robust.

Rich (BB code):
Sub RemoveLeadingOrTrailingCharacter_v1()
  Dim RX As Object
  Dim a As Variant
  Dim i As Long, j As Long, uba2 As Long
  
  Const myCharacter As String = ","
  Set RX = CreateObject("VBSCript.RegExp")
  RX.Global = True
  RX.Pattern = Replace("(^\#)|(\#$)", "#", myCharacter)
  a = ActiveSheet.UsedRange.Value
  uba2 = UBound(a, 2)
  For i = 1 To UBound(a)
    For j = 1 To uba2
      a(i, j) = RX.Replace(a(i, j), "")
    Next j
  Next i
  ActiveSheet.UsedRange.Value = a
End Sub


Sub RemoveLeadingOrTrailingCharacter_v2()
  Dim a As Variant
  Dim i As Long, j As Long, uba2 As Long
  
  Const myCharacter As String = ","
  a = ActiveSheet.UsedRange.Value
  uba2 = UBound(a, 2)
  For i = 1 To UBound(a)
    For j = 1 To uba2
      If a(i, j) Like myCharacter & "*" Then a(i, j) = Mid(a(i, j), 2)
      If a(i, j) Like "*" & myCharacter Then a(i, j) = Left(a(i, j), Len(a(i, j)) - 1)
    Next j
  Next i
  ActiveSheet.UsedRange.Value = a
End Sub


3. VBA: Remove cell data if no text charterer found.
Can you define exactly what you mean by a "text character"? To help clarify could you give 2 or 3 examples of cell contents that should be cleared and the same for those that should be kept?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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