userxyz777
New Member
- Joined
- Apr 12, 2018
- Messages
- 15
I need to remove the hyphens and equal signs from cell values of a sheet (with lots of data) if they are the first character of the string
Cells look like
=Bob - Carol
-Ted - Alice
Then I need
Bob - Carol
Ted - Alice
I have tried many macros to do this and I can get non of them to work
Here is one of them. it gives an empty output
I tried to upload a Mimi-sheet but all I got was the chance to download a plugin
Thanks in advance for help with this
Cells look like
=Bob - Carol
-Ted - Alice
Then I need
Bob - Carol
Ted - Alice
I have tried many macros to do this and I can get non of them to work
Here is one of them. it gives an empty output
I tried to upload a Mimi-sheet but all I got was the chance to download a plugin
Thanks in advance for help with this
VBA Code:
Sub removeTest()
Dim ws As Worksheet
Dim arr As Variant, v
Dim rng As Range
Dim LR As Long, LC As Long, i As Long, j As Long
Set ws = ThisWorkbook.Sheets("Elements")
If WorksheetFunction.CountA(ws.UsedRange) <> 0 And ws.UsedRange.Address <> "$A$1" Then
With ws
LC = .Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
LR = .Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set rng = .Range("A" & startrow).Resize(LR, LC)
arr = rng.Value2
'Remove "=" if first char. Add more if needed
For i = LBound(arr) To UBound(arr)
For j = LBound(arr, 2) To UBound(arr, 2)
arr(i, j) = RemoveFirstChar(CStr(arr(i, j)), "=-", 2)
arr(i, j) = RemoveFirstChar(CStr(arr(i, j)), "=", 1)
arr(i, j) = RemoveFirstChar(CStr(arr(i, j)), "-", 1)
Next j
Next i
Debug.Print "Array of Keys:"
For Each v In arr
Debug.Print v
Next
ThisWorkbook.Sheets("A").Cells.Clear
ThisWorkbook.Sheets("A").Range("A1").Resize(UBound(arr), UBound(arr, 2)) = arr
End Sub
Public Function RemoveFirstChar(RemFstChar As String, what As String, L As Long) As String
Dim TempString As String
TempString = RemFstChar
If InStr(RemFstChar, what) = 1 Then
'MsgBox RemFstChar
TempString = Right(RemFstChar, Len(RemFstChar) - 1)
'MsgBox TempString
End If
End Function