Would like Macro Adapted for Selected Cells and Ask for Character to be Used

Alex20850

Board Regular
Joined
Mar 9, 2010
Messages
146
Office Version
  1. 365
Platform
  1. Windows
This macro works, but I would like adapted in two ways:
1) Instead of hard coded d1:d1000, I would like to use selected cells
2) I would like to be asked for the character to use instead of the hard coded "m".

Sub RemoveAfter()


For Each c In Range("d1:d1000")
If InStr(c.Value, "m") > 0 Then
c.Value = Left(c.Value, InStr(c.Value, "m") - 1)
End If
If InStr(c.Value, "m") > 0 Then
c.Value = Left(c.Value, InStr(c.Value, "m") - 1)
End If
Next c


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
Code:
Sub RemoveAfter()
   Dim Cl As Range
   Dim Srch As String
   
   Srch = InputBox("Please enter the value to be searched for")
   If Srch = "" Then Exit Sub
   For Each Cl In Selection
      If InStr(Cl.Value, Srch) > 0 Then
         Cl.Value = Left(Cl.Value, InStr(Cl.Value, Srch) - 1)
      End If
   Next Cl
End Sub
 
Upvote 0
How about
Code:
Sub RemoveAfter()
   Dim Cl As Range
   Dim Srch As String
   
   Srch = InputBox("Please enter the value to be searched for")
   If Srch = "" Then Exit Sub
   For Each Cl In Selection
      If InStr(Cl.Value, Srch) > 0 Then
         Cl.Value = Left(Cl.Value, InStr(Cl.Value, Srch) - 1)
      End If
   Next Cl
End Sub

Works great. thanks!
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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