Delete text after delimiter using text to columns

mrmcoupe

New Member
Joined
Jan 22, 2017
Messages
17
Hi,

quite often I get spreadsheets containing product numbers - however, they often have a "#" as delimiter followed by a three letter option code. The data can come in a any column and vary from 5 to 1000 rows. So, I wan to make a permanent tool where I can select the column with the specific data - push the macro button (in my own tool view in the ribbon) and remove (delete) everything after the delimiter.

I have recorded a macro. However, the selection is static. I have also tried using various selection methods, but it always strands at the beginning of the texttocolumns code.

The error message I get is: Compile Error: Expected Function or Variable (highlighting "Selection" in the code). As if it doesn't have a selection to work with(?).
Also: not sure if the data in "Field Info" is correct. I guess this also should be dynamic.

I appreciate all the help I can get.



Code:
Sub remove_option_code()'


Dim rng As Range
Set rng = ActiveSheet.Range.CurrentRegion




'
'
    rng.Select
    Selection.TextToColumns _
    Destination:=rng, _
    DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, _
    ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, _
    Comma:=False, _
    Space:=False, _
    Other:=True, _
    OtherChar:="#", _
    FieldInfo:=Array(Array(1, 1), Array(2, 9)), _
    TrailingMinusNumbers:=True
    
    
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
try posting a sample of your data, and what you would like to do with it

I think you maybe are making this more complicated than it needs to be
 
Last edited:
Upvote 0
See if this works:

Code:
Dim rng As Range, x As Variant, lr as Long

Set rng = ActiveCell.Cells(1)
lr = Cells(Rows.Count, rng.Column).End(xlUp).Row
 
Set x = Range(Cells(1, rng.Column), Cells(lr, rng.Column))
x = Evaluate("=IF(ROW(1:" & lr & "),TRIM(LEFT(SUBSTITUTE(" & x.Address & ",""#"",REPT("" "",99)),99)))")
Range(Cells(1, rng.Column), Cells(lr, rng.Column)) = x
 
Upvote 0
try posting a sample of your data, and what you would like to do with it

I think you maybe are making this more complicated than it needs to be


Sample data:

I would like to delete everything after #. Keeping the data in the same cells.

[TABLE="width: 64"]
<tbody>[TR]
[TD]Y8Q66EA#ABN[/TD]
[/TR]
[TR]
[TD]1EN16EA#ABN[/TD]
[/TR]
[TR]
[TD]Y6J44EA#ABN[/TD]
[/TR]
[TR]
[TD]1EN21EA#ABN[/TD]
[/TR]
[TR]
[TD]X2F25EA#ABN[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
Hi, here is another option you could try:

Code:
Sub RemoveHashandAfter()
Selection.Replace What:="#*", Replacement:="", LookAt:=xlPart
End Sub
 
Upvote 0
Hi! thanks for your solution.

I still get the same error message - Compile error - Expected funtion or variable.

brg
H
 
Upvote 0
See if this works:

Code:
Dim rng As Range, x As Variant, lr as Long

Set rng = ActiveCell.Cells(1)
lr = Cells(Rows.Count, rng.Column).End(xlUp).Row
 
Set x = Range(Cells(1, rng.Column), Cells(lr, rng.Column))
x = Evaluate("=IF(ROW(1:" & lr & "),TRIM(LEFT(SUBSTITUTE(" & x.Address & ",""#"",REPT("" "",99)),99)))")
Range(Cells(1, rng.Column), Cells(lr, rng.Column)) = x


POW! And there it was! This one worked as expected! Thanks for you answer!

H
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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