Macro to help match and sort data

sn0365

New Member
Joined
May 10, 2017
Messages
13

<tbody>
[TD="colspan: 4"]
ncDDuF

Hi, I am looking for a formulae or a script which would help me execute this problem.

Column B contains the name of the sample and column A contains the following breakdown of the sample but this data contains other sample informations.
Every sample is Column B starts with a Character ID (A to Z) followed by a space and then "-". Example (B - or A - )
The data which lists the breakdown of the sample in Column A will also start with the same character ID but can have a number attached to it (Number ID range A1-A99).

In Column Output 1 - I want to extract the relevant sample breakdown data from Column A for the sample listed in column B.
Example - If Sample started with the ID "B -", I want the ouput1 cell to contains all the sample data starting with "B and a number"

In Column Output 2 - I want to copy the data from Output 1 but without the starting ID (example B1 or A1 or A99)[/TD]

</tbody>
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I have been given this script to help sort my data, but I have tried to use it but I always get errors.

Can someone please apply the script to my excel file and solve the issue?

Link for the file - https://wetransfer.com/<wbr>downloads/<wbr>127ca2b06c123afe7ad4a9b554929b<wbr>0420170914135128/<wbr>55446f732785bfcfa39adba455b810<wbr>a220170914135128/10ecb5

[h=2]Script -[/h]
Module Code:

Option Explicit 'Handles the extraction of data
Public Sub ExtractSampleList()
Dim rngInput As Range
Dim rngSmpls As Range
Dim strCharCode As String
Dim varWrkCell As Variant
Dim colSmpls As Collection
Dim clsOutputs As OutputClass
'Get the input and set the search range
With wsInputSheet
Set rngInput = .Range("SelectionInput")
Set rngSmpls = .Range("SampleList")
End With
'Get the Single Letter Character Code
strCharCode = Left(rngInput.Value2, 1)

'Search the range where the samples are located
Set colSmpls = New Collection
For Each varWrkCell In rngSmpls
'If the first character of the sample equals the single character code then extract the data
If Left(varWrkCell.Value2, 1) = strCharCode Then
'Set up new instance of class and construct it
Set clsOutputs = New OutputClass
Call clsOutputs.Construct(varWrkCell)

'Add class instance to collection
colSmpls.Add clsOutputs
End If
Next varWrkCell
'Call the output routine (passing the collection of objects extracted
Call OutputData(colSmpls)
'Clear up objects
Set rngInput = Nothing
Set rngSmpls = Nothing
Set varWrkCell = Nothing
Set colSmpls = Nothing
Set clsOutputs = Nothing
End Sub
Private Sub OutputData(ByRef colSmpls As Collection)
Dim lngOutputRow As Long: lngOutputRow = 1 'Assuming Header in row 1 so this will move to row 2 on entering the loop
Dim lngChrCdCol As Long: lngChrCdCol = 7
Dim lngSmplCol As Long: lngSmplCol = 8
Dim clsOutputs As OutputClass
'Loop round each object in the collection and output
With wsOutputSheet
For Each clsOutputs In colSmpls
lngOutputRow = lngOutputRow + 1
.Cells(lngOutputRow, lngChrCdCol) = clsOutputs.CharacterCode
.Cells(lngOutputRow, lngSmplCol) = clsOutputs.CharacterCode & " " & clsOutputs.SampleData
Next clsOutputs
End With
End Sub


Class Module Code:


Option Explicit
Private pStr_SampleData As String
Private pStr_CharCode As String
Public Sub Construct(ByRef varInput As Variant)
Dim varSplitString As Variant
varSplitString = Split(CStr(varInput.Value2), " ", 2)
pStr_CharCode = varSplitString(0)
pStr_SampleData = varSplitString(1)
End Sub
Public Property Get CharacterCode() As String
CharacterCode = pStr_CharCode
End Property
Public Property Get SampleData() As String
SampleData = pStr_SampleData
End Property Appreciate all your help.
</pre>
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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