VBA Script to extract two sets of numbers between text in cell

johnbrownbaby

New Member
Joined
Dec 9, 2015
Messages
38
Hello,

Thanks to the elegant solution to the first question posted here, I was able to get a formula based solution. I am finding that if the data is given like this:

1637176548920.png


I am having a problem keeping up with the formula based approach. How can I do this from VB script viewpoint to collect the data from column A where all the "eFine" and "eFood" numbers are placed in separate columns found in K and L respectively?

I posted a preliminary question on OzGrid found here but I do not know how to delete that question to make this updated question priority.

test1.xlsx
ABCDEFGHIJKLMNO
1Test 1Amp
2240485212827
3240489212855
4240486012914
5240458913015
6
7Pack: AMPLITUDE_ACCESS
8eFine: 2404852 eFood: 12827
9
10
11
12
13
14Pack: AMPLITUDE_ACCESS
15eFine: 2404892 eFood: 12855
16
17
18
19
20
21Pack: AMPLITUDE_ACCESS
22eFine: 2404860 eFood: 12914
23
24
25
26
27
28Pack: AMPLITUDE_ACCESS
29eFine: 2404589 eFood: 12015
30
31***
32*** DISCONNECT
33*** time 14:38:02
34***
35
36
37[INFO] INFO: DISCONNECTED
38
39
40Pack: AMPLITUDE_ACCESS
41eFine: 2404892 eFood: 12855
42
43
44
45
46
47Pack: AMPLITUDE_ACCESS
48eFine: 2404860 eFood: 12914
49
50
51
52
53
54Pack: AMPLITUDE_ACCESS
55eFine: 2404589 eFood: 12015
56
57***
58*** DISCONNECT
59*** time 14:38:02
60***
61
62
63[INFO] INFO: DISCONNECTED
Sheet1


Thanks for your help and time!
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:

VBA Code:
Sub extract_two_sets_of_numbers()
  Dim r As Range, f As Range
  Dim cell As String
  Dim arr As Variant
  Dim dic As Object
  
  Set dic = CreateObject("Scripting.Dictionary")
  Set r = Range("A1", Range("A" & Rows.Count).End(3))
  Set f = r.Find("eFine", , xlValues, xlPart, , , False)
  If Not f Is Nothing Then
    cell = f.Address
    Do
      arr = Split(f.Value, " ")
      If Not dic.exists(arr(1)) Then
        dic(arr(1)) = Empty
        Range("K" & Rows.Count).End(3)(2).Resize(, 2).Value = Array(arr(1), arr(3))
      End If
      Set f = r.FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
  End If
End Sub
 
Upvote 0
Hello,

Thanks for your code. I tried it but it works partially. It stops filling column K, L when it sees the "***". For the example spreadsheet I posted, I only get 4 entries, I should get 7. How to get all the entries with the "eFine" and "eFood"?

Thanks!
 
Upvote 0
Give this macro a try...
VBA Code:
Sub ExtractNumbers()
  Dim X As Long, Arr As Variant, Parts As Variant, Result As Variant
  Arr = Filter(Application.Transpose(Range("A1", Cells(Rows.Count, "A").End(xlUp))), "eFine:", True, vbTextCompare)
  ReDim Result(LBound(Arr) To UBound(Arr), 1 To 2)
  For X = LBound(Arr) To UBound(Arr)
    Parts = Split(Arr(X))
    Result(X, 1) = Parts(1)
    Result(X, 2) = Parts(3)
  Next
  Range("K2").Resize(UBound(Result), 2) = Result
End Sub
 
Upvote 0
Do you mean all the sheets in the workbook or only some of them? If only some of them, what sheets are they (the code needs someway to identify them)?
 
Upvote 0
For the example spreadsheet I posted, I only get 4 entries, I should get 7.
In your example you only have 4 results, I assumed that you only wanted the unique values. But if you want all, on all sheets, try the following. The results will be on the first sheet.

VBA Code:
Sub extract_two_sets_of_numbers2()
  Dim r As Range, f As Range
  Dim cell As String
  Dim arr As Variant
  Dim sh As Worksheet
  
  For Each sh In Sheets
    Set r = sh.Range("A1", sh.Range("A" & Rows.Count).End(3))
    Set f = r.Find("eFine", , xlValues, xlPart, , , False)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        arr = Split(f.Value, " ")
        Sheets(1).Range("K" & Rows.Count).End(3)(2).Resize(, 2).Value = Array(arr(1), arr(3))
        Set f = r.FindNext(f)
      Loop While Not f Is Nothing And f.Address <> cell
    End If
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

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