Remove excess characters in url domain

Mike TheSpike

New Member
Joined
Jul 12, 2021
Messages
40
Office Version
  1. 2010
Platform
  1. Windows
Hi there, pls help. this code is not working when i select specific column.

VBA Code:
Sub removeExcess_domain()
Dim Cell As Range
Dim tmp As String
Dim Lr As Long

Lr = Cells(Rows.Count, "L").End(xlUp).Row

'loop through every cell in range and remove characters after ?

For Each Cell In Range("L2:L" & Lr)
    tmp = cell.Value
    '
    cell.Value = Left(tmp, InStr(tmp, "?") - 1)
Next
End Sub


Thanks for your help,
Mike
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Can you give us samples of what you are trying to achieve ?
 
Upvote 0
Hi @Michael M,

I want to remove all characters from ?

sample:

the output should be like this:


above code is not working that there's missing in the code. however if i use the selection it worked but i want both functionality to be used.

here's the code that works:

VBA Code:
Sub removeExcess_domain()
Dim cell As Range
Dim MyRange As Range
Dim tmp As String


Set MyRange = Selection  'this is your range of data

'loop through every cell in range and remove characters after ?
For Each cell In MyRange.Cells
    tmp = cell.Value
    '
    cell.Value = Left(tmp, InStr(tmp, "?") - 1)
Next
End Sub

Thanks for your reply,
Mike
 
Last edited:
Upvote 0
The code works fine for me but selecting an entire column would mean running the process through 65000 rows....Do you really want to do that ??
Maybe this way..select the entire column, but it finds the last row of that column first !!
VBA Code:
Sub MM1()
Dim cell As Range, MyRange As Range, tmp As String, lr As Long
lr = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
Set MyRange = Range(Cells(2, ActiveCell.Column), Cells(lr, ActiveCell.Column)) 'this is your range of data
'loop through every cell in range and remove characters after ?
On Error Resume Next
For Each cell In Range(Cells(2, ActiveCell.Column), Cells(lr, ActiveCell.Column))
    tmp = cell.Value
    cell.Value = Left(tmp, InStr(tmp, "?") - 1)
Next cell
End Sub
 
Upvote 0
Solution
@Michael M
By the way, is it okay to put specific column like column(L) rather on ActiveCell.Column?
In this case whenever my active cell it will always execute the column "L" .
i'm having difficulty in that column. Thanks again.
 
Upvote 0
@Michael M
Sorry for the follow up above. I figured it out on how to make it happen.
it is just a simple "On Error Resume Next" :)
Thanks to you! you're great.
 
Upvote 0
Assuming the question mark always starts your excess junk and that you have selected one or more cells in the column you want to process, only one line of code is needed to do what is wanted...
VBA Code:
Sub ShortenURL()
  ActiveCell.EntireColumn.Replace "~?*", "", xlPart, , , , False, False
End Sub
If you want to specify the column directly, then use this line of code instead of the one I posted above...
VBA Code:
  Columns("L").Replace "~?*", "", xlPart, , , , False, False
Note: You do not have to worry about the code processing the entire column as Replace seems to only look at the UsedRange.
 
Last edited:
Upvote 0
@Rick
OMG! Amazing. this is a short code and fast run of macro. both of your codes is amazing and i am using it now.
Thanks to both of you.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,047
Members
449,064
Latest member
scottdog129

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