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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,223,741
Messages
6,174,230
Members
452,553
Latest member
red83

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