Excel vba: extract characters before delimiter in active cell

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Hi,

Not looking for a Function.
  1. Need VBA to extract left characters before delimiter "." in active cell and call it ExL
  2. Need VBA to extract right characters before delimiter " - " in active cell and call it ExR
Got code below, but those are functions, don't need that:

VBA Code:
If InStr(txt, char) > 0 Then Extract_LString = Left(txt, InStr(txt, char) - 2)

If InStr(txt, char) > 0 Then Extract_RString = Right(txt, InStr(txt, char) - 4)
 
What range are you trying to filter? H10:H20 for example
I think I follow you, I need something like below code snippet (completely unrelated) to tell XL what range to filter...
1. How do I edit above code to tell it to filter UsedRange, relative to the cell that code returns, H10 in above example? (If UsedRange is going to include the corresponding data table)
2. And, guessing field:=2 in below would refer to Column H in my example above? Is there easier way to refer to it by the Column letter, than by its corresponding number?

How would you edit the above code with these things in mind?

ActiveSheet.Range("B4:E15").AutoFilter field:=2
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You basically need 3 things for Filter:
1) The full range of data that you want to filter ... in your example from your last post, the full range would be B4:E15
2) The column you want to set the filter for ... 2 from your example means the second column in the full range ... Column B is the first column so '2' would mean Column C
3) What you want to filter for ... this is the Criteria ... ExR from your previous code

I can't see your data so I can't guess what you are trying to filter.
 
Upvote 0
You basically need 3 things for Filter:
1) The full range of data that you want to filter ... in your example from your last post, the full range would be B4:E15
2) The column you want to set the filter for ... 2 from your example means the second column in the full range ... Column B is the first column so '2' would mean Column C
3) What you want to filter for ... this is the Criteria ... ExR from your previous code

I can't see your data so I can't guess what you are trying to filter.
B4:E15 is unrelated, I was just using that code snippet to wrap my mind around the syntax. Sorry, confusion is all mine.
My data starts in A10, row 10 in where the table header is. Data goes down different number of rows - it's not consistent, and columns to the right may not be consistent in terms of how many.

What would be the code to be flexible enough to determine the range to filter automatically from, in a above example H10, something like UsedRange maybe?

re: "field:=" - Is there easier way to refer to it by the Column letter, than by its corresponding number?
"field:=" would need to be ExL, as it is variable

How would you edit the above code with these things in mind?
 
Upvote 0
So something like the following:
VBA Code:
    With ActiveSheet.UsedRange
        .AutoFilter Field:=Range(ExL & 1).Column, Criteria1:=ExR
    End With

This part is the part that converts the 'H' to the equivalent column number
VBA Code:
Range(ExL & 1).Column
 
Upvote 0
So something like the following:
VBA Code:
    With ActiveSheet.UsedRange
        .AutoFilter Field:=Range(ExL & 1).Column, Criteria1:=ExR
    End With

This part is the part that converts the 'H' to the equivalent column number
VBA Code:
Range(ExL & 1).Column
At the end it is so simple, so elegant.
 
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,431
Members
449,158
Latest member
burk0007

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