Forum

Can we write a macr...
 
Notifications
Clear all

Can we write a macro to read the last sentence of 2 alphabets and filter?

7 Posts
3 Users
0 Reactions
57 Views
(@Anonymous)
Posts: 0
New Member Guest
 

Hi,

Can anyone help me to understand are we able to write a macro to read the last sentence of 2 alphabets and filter? I just want the macro help to pick the data end with ES to put in column D as wk1 and end with Prod in column D as wk2.

Currently, I have to use formula to split the last 2 and 4 letters in another column, then filter one by one. Is there any faster way to do that?

File attached Example:

column B,

consolidate_ES

consolidate_Prod

consolidate_xx

consolidate_Prod

 

Thank you & Regards

CY

 
Posted : 14/01/2020 4:27 am
(@sunnykow)
Posts: 1417
Noble Member
 

Why don't you just create a table and then do a VLOOKUP?

Consolidate_ES wk1
Consolidate_Prod wk2
Consolidate_Pre TBD
Consolidate_End TBD

If you need the word after the underscore you can try SUBSTITUTE(B2,"Consolidate_","")

Hope this helps.

Sunny

 
Posted : 14/01/2020 7:16 pm
(@Anonymous)
Posts: 0
New Member Guest
 

Hi Sunny,

Thank you for your suggestion. The reason being i need to crate macro because, this is only part of the process. there are thousand of rows & 25 columns data which need to filter, get the correct item and create a pivot table. Thus, I have to think a way to simply it and get macro to run for the whole report. 

Let me try to use the formula that you suggested. After I apply the formula to pick up the ending letters, I will need to filter those ending with ES, create a pivot table and ending with Prod, create another pivot table in order to work on the data analysis.

Any idea, can write a code to pick those end with ES and Prod to create the pivot table separately?

Thank you.

CY

 
Posted : 14/01/2020 7:52 pm
(@sunnykow)
Posts: 1417
Noble Member
 

Hi CY

Then just create multiple Pivot tables straight from your data and filter each one for the required type.

No need to do anything extra at all.

Sunny

 
Posted : 14/01/2020 10:22 pm
(@purfleet)
Posts: 412
Reputable Member
 

I agree with sunny for small, adhoc data sets filter is the easiest method but if it is repetitive and repeatable then a Macro can help.

Attached is a quick macro that should put the detail you want in column G (this can be changed in the code by changing the offset(0,5) to whatever column you want relative to column B

Option Explicit

Sub SplitAndPop()

Dim lr As Long 'last row

Dim tr As Range 'type range (where the data is)
Dim tc As Range 'type cell (cell loop)

Dim us As Integer 'underscore letter count in case different words are used

lr = Cells(Rows.Count, 1).End(xlUp).Row 'find last row

Set tr = Range("b2:b" & lr) 'set type range

For Each tc In tr 'loop through all cells in the type range
us = InStr(tc.Value, "_") 'finds the letter number of the underscore
If Right(tc, Len(tc) - us) = "ES" Then 'checks if the end bit is ES, PROD, Pre or End then assigns a new tag
tc.Offset(0, 5) = "wk1"
End If
If Right(tc, Len(tc) - us) = "Prod" Then
tc.Offset(0, 5) = "wk2"
End If
If Right(tc, Len(tc) - us) = "Pre" Then
tc.Offset(0, 5) = "TBD"
End If
If Right(tc, Len(tc) - us) = "End" Then
tc.Offset(0, 5) = "TBD"
End If
Next tc

End Sub

Purfleet

 
Posted : 15/01/2020 3:18 am
(@Anonymous)
Posts: 0
New Member Guest
 

Hi Sunny,

Alright. Thank you.

The SUBSTITUTE(B2,"Consolidate_","") is very useful. 🙂

Thank you.

 
Posted : 15/01/2020 3:29 am
(@Anonymous)
Posts: 0
New Member Guest
 

Thank you Purfleet. Is really help. Appreciated.

 
Posted : 16/01/2020 9:52 am
Share: