check first 3 Letters of a string !!!

G21

New Member
Joined
Oct 17, 2010
Messages
26
HI ,
Need some help !!!
im having a list of data & it contains various patterns. i want to categorize these data depending on first 3 letters of the string.
eg -

ABC1
ABC2
XYZ1
XYZ2

so i want to copy data which contains "ABC" as first 3 letters to sheet1 & copy data contains "XYZ" to sheet2.

RGDS,
G
 

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.
You can use the LEFT function in Excel or the Left function in VBA to extract the 1st N characters of a string. Then, you can use that information as you see fit.
 
Upvote 0
Hi

Manually you could use Autofilter to do this - use a text filter of "Begins with" and use ABC or XYZ etc and copy from the original sheet to sheet1 and sheet2.

If you want to automate this then most of the code can be had from recording a macro whilst following the above steps.
 
Upvote 0
HI ,
Need some help !!!
im having a list of data & it contains various patterns. i want to categorize these data depending on first 3 letters of the string.
eg -

ABC1
ABC2
XYZ1
XYZ2

so i want to copy data which contains "ABC" as first 3 letters to sheet1 & copy data contains "XYZ" to sheet2.

RGDS,
G

Some options:

1] Running Advanced Filter with criteria.

2] Running a formula system...

Assumption: The data is in A2:A5 on the Data sheet.

Sheet1

A1: ABC

A2, just enter:
Code:
=COUNTIF(Data!A2:A5,A1&"*")
A3: List

A4, control+shift+enter, not just enter, and copy down:
Code:
=IF(ROWS($A$4:A4)<=$A$2,INDEX(Data!$A$2:$A$5,
    SMALL(IF(LEFT(Data!$A$2:$A5,3)=$A$1,
     ROW(Data$A$2:$A$5)-ROW(Data$A$2)+1),
      ROWS($A$4:A4))),"")
Repeat a similar process for Sheet2, etc.
 
Upvote 0
Try this code. Works for me.:)

You data is in Col A of your activesheet.

Code:
[FONT=Courier New]Option Explicit[/FONT]
[FONT=Courier New]Sub TRY()[/FONT]
[FONT=Courier New]Dim c As Range, lr As Long[/FONT]
[FONT=Courier New]lr = Range("A" & Rows.Count).End(xlUp).Row[/FONT]
[FONT=Courier New]For Each c In Range("A1:A" & lr)[/FONT]
[FONT=Courier New] If Left(c.Value, 3) = "ABC" Then[/FONT]
[FONT=Courier New]  c.Copy Sheet1.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)[/FONT]
[FONT=Courier New] ElseIf Left(c.Value, 3) = "XYZ" Then[/FONT]
[FONT=Courier New]  c.Copy Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)[/FONT]
[FONT=Courier New] Else[/FONT]
[FONT=Courier New] 'Nothing[/FONT]
[FONT=Courier New] End If[/FONT]
[FONT=Courier New]Next c[/FONT]
[FONT=Courier New]End Sub[/FONT]
 
Upvote 0
HI All,
My requirement is done....

Dim cat As String
Dim val As String
cat = ActiveCell.Offset(0, -1).Value
val = Left(cat, 3)

If val = "xxx" Then

etc...


Thanks for the support of everyone who replied. I got some points from all of your replies.

RGDS,
G
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,391
Members
452,909
Latest member
VickiS

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