FIND and SEARCH numbers from String

unknownymous

Board Regular
Joined
Sep 19, 2017
Messages
249
Office Version
  1. 2016
Platform
  1. Windows
Hello Guys,

I was looking for the codes where I can search for the numbers on the data below so basically it's the [**]

IDSubjectStatusNote
1Science [1]YES
2Math [49]NO
3Health [9]YES
4SportsYES
5History [29]YES

<tbody>
</tbody>

Once I run the codes, I can have below data (number sorted ascending) on another sheet called "Check"

After:

SubjectNumber
Science1
Health9
History29
Math49

<tbody>
</tbody>


Any help will be much appreciated. :)
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
If you are looking for a formula you can use Find to locate the left [ and the right ] and MID out your numbers like this

=IFERROR(MID(B2,(FIND("[",B2)+1),(FIND("]",B2))-(FIND("[",B2)+1)),"")

I used your table and put the formula in E2 where your data started in A2

HTH ~DR


You can also take a look here and modify this to suit your needs if you like this better.
https://www.ablebits.com/office-addins-blog/2017/11/22/excel-extract-number-from-string/
 
Last edited:
Upvote 0
Thank you! Can I use this code in case I want to create a macro?
Here is a macro solution for you to try...
Code:
[table="width: 500"]
[tr]
	[td]Sub SubjectsAndNumbers()
  Dim R As Long, SubjNum As Variant, Result As Variant
  SubjNum = Range("B2", Cells(Rows.Count, "B").End(xlUp))
  ReDim Result(1 To UBound(SubjNum), 1 To 2)
  For R = 1 To UBound(SubjNum)
    If SubjNum(R, 1) Like "*[[]*" Then
      Result(R, 1) = Left(SubjNum(R, 1), InStr(SubjNum(R, 1) & "[", "[") - 1)
      Result(R, 2) = Val(Mid(SubjNum(R, 1), InStr(SubjNum(R, 1) & "[", "[") + 1))
    End If
  Next
  With Sheets("Check")
    .Range("A1:B1") = Array("Subject", "Number")
    .Range("A2").Resize(UBound(Result), 2) = Result
    .Range("A2:B" & UBound(Result) + 1).Sort Key1:=.Range("B2")
  End With
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Assuming that ..
- the original data is in the active sheet when the code is run and that the data is in columns A:D, and
- sheet Check exists with no current data in columns A:B
.. you could try this non-looping method (in a copy of your workbook)

Code:
Sub SubjectNum()
  With Sheets("Check").Range("A1").Resize(Range("B" & Rows.Count).End(xlUp).Row)
    .Value = Range("B1", Range("B" & Rows.Count).End(xlUp)).Value
    .Replace What:="]", Replacement:="", LookAt:=xlPart
    .Replace What:=" [", Replacement:="[", LookAt:=xlPart
    .TextToColumns DataType:=xlDelimited, Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar:="["
    .Resize(, 2).Sort Key1:=.Columns(2), Order1:=xlAscending, Header:=xlYes
    .Offset(1, 1).SpecialCells(xlBlanks).EntireRow.Delete
    .Cells(1, 2).Value = "Number"
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,217,440
Messages
6,136,627
Members
450,022
Latest member
Joel1122331

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