Fixing my .selection code

Laurence D

New Member
Joined
Sep 14, 2016
Messages
31
Hey all,

Can somebody spend a minute or so to fix my code that I cannot get working? I am trying to increase the flexibility of this code rather than just setting the range it will be based on my selection in the active sheet. Do I have to loop this each cell in the selection to get this working?

Code:
Sub Remove_slashes()
Dim the_string As String


the_string = ActiveSheet.Selection
   
   the_string = Replace(the_string, "/", "")
    
    ActiveSheet.Selection = the_string


End Sub

Cheers,
Laurence
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Try this:
Code:
Sub Remove_slashes()


    On Error Resume Next
    
    Selection.Replace What:="/", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        
    On Error GoTo 0
    
End Sub
 
Upvote 0
Try this:
Code:
[LEFT][COLOR=#222222][FONT=Tahoma]Sub Remove_slashes()[/FONT][/COLOR]
[COLOR=#222222][FONT=Tahoma]'Modified  12/5/2018  4:05:12 PM  EST[/FONT][/COLOR]
[COLOR=#222222][FONT=Tahoma]Dim the_string As String[/FONT][/COLOR]
[COLOR=#222222][FONT=Tahoma]the_string = Selection.Value[/FONT][/COLOR]
[COLOR=#222222][FONT=Tahoma]the_string = Replace(the_string, "/", "")[/FONT][/COLOR]
[COLOR=#222222][FONT=Tahoma]Selection.Value = the_string[/FONT][/COLOR]
[COLOR=#222222][FONT=Tahoma]End Sub[/FONT][/COLOR][/LEFT]
 
Last edited:
Upvote 0
Try this:

Sub Remove_slashes()
'Modified 12/5/2018 4:05:12 PM EST
Dim the_string As String
the_string = Selection.Value
the_string = Replace(the_string, "/", "")
Selection.Value = the_string
End Sub
That only works if the selection is exactly one cell. If it is a multi-cell selection, the code throws errors.

Based on this line here:
Do I have to loop this each cell in the selection to get this working?
I am pretty sure that they want it to work on a multi-cell selected range (which my code will do).
 
Last edited:
Upvote 0
If your selection is for more then one cell then use this:

Code:
Sub Remove_slashes()
'Modified  12/5/2018  4:22:12 PM  EST
Dim r As Range
    For Each r In Selection
        r = Replace(r.Value, "/", "")
    Next
End Sub
 
Upvote 0
You are welcome.

There is no reason to use a loop here, as it can be done without loops.
Generally, you want to avoid using loops whenever possible, as they use more resources, and thus are slower and less efficient (there are times when you can't avoid using them, but many times you can!).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,314
Members
452,634
Latest member
cpostell

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