Conditional extraction of number references from text contained in single cell

Noodles90

New Member
Joined
Sep 14, 2021
Messages
9
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello!

I have following text contained in a single cell at the top of some reports:

Main information
--Menu--
Dimentions and specifications - Dacia Duster
Intref: 05897423654422; v 541 n

C number.: 200000054878 Car ref nr.: 900000058746
Report status: Active Spec report version nr.:3.4
Opt2 nr.: 3.3 Spec report: Active
Last changed: 29-SEP-2021 Modified by: Employee X

And, I needed to extract specific references based on their designations.
There are two scenarios.

Scenario 1:
1632485751193.png

-The macro searches for the ref "Secref:", in the cell A1 and pastes the value, in this case 05897423654422, in an outside cell in text format (the zeros should remain);
-If there is no "Secref:" reference in the cell, it should search for the "C number" instead and paste the reference in the same outside cell;
-If there is no "C number" either, the macro should write "Not found" in this outside cell.

Scenario 2:
1632485732099.png

-For a similar kind of report, the macro should use the reference from an outside cell as input, and search for the reference to it associated;
-If found, the value, should be pasted in an outside cell in text format (the zeros should remain);
-If the reference input is not found, then "Not found" should be written in this cell.

Can someone help?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Writting the solutions I've managed to write, based on @JEC 's answer to my today's post:

Scenario 1:

VBA Code:
Sub Scenario_1()
 With ActiveCell
        If InStr(1, (Range("A1").Value), "Intref:") > 0 Then
            .NumberFormat = "@"
        .Value = Left(Split(Cells(1, 1), "Intref:")(1), 15)
        Else
                    If InStr(1, (Range("A1").Value), "Secref:") > 0 Then
                    .NumberFormat = "@"
                    .Value = Left(Split(Cells(1, 1), "Secref:")(1), 15)
        Else
                        If InStr(1, (Range("A1").Value), "C number.:") > 0 Then
                        .NumberFormat = "@"
                        .Value = Left(Split(Cells(1, 1), "C number.:")(1), 13)
        Else
                            ActiveCell.FormulaR1C1 = "Keyword not found"
        End If
        End If
        End If
        End With
End Sub

Scenario 2:
VBA Code:
Sub Scenario_2()

Dim RefWord As Variant
RefWord = Cells(4, 3).Value

 With ActiveCell
        If InStr(1, (Range("A1").Value), RefWord) > 0 Then
            .NumberFormat = "@"
        .Value = Left(Split(Cells(1, 1), RefWord)(1), 15)
        Else
        ActiveCell.FormulaR1C1 = "Keyword not found"
        End If
   
        End With
End Sub

Once again, many thanks for all the help here, I am learning a lot! :D
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

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