VB to Select & Print Data following Occurrences in a String

SStrange

Board Regular
Joined
Oct 21, 2013
Messages
52
So I have some VB to count the number of occurrences of groupings of characters. What I need to do now is select each of them and have them print the text. To be more specific, I had instances of carriage returns and hard returns in a data string. To be able to select within the string, I removed one character and replaced the other with a separate special character. That allowed me to find the beginning of the individual strings by using the special character (specifically CHR(8)). Basically now there is a string in my data that is (if you use this formula in excel) =CHAR(8)&"A1:". the thing is there could be 5-80 occurrences in the strings. I want it to pull the data for each one selecting as below....

A1: (1) 12345
A1: (2) 13524
A1: (3) 12453
A1: (4) 12534
A1: (5) 31425
etc...

Any help you can offer would be appreciated.

Thanks
 
See if this helps
Rich (BB code):
Sub Test1()
 
  ' Each block of elements is separated by vbCR
  ' Each element (line) in block is separated by vbLF
  ' Each data value in element is separated by Chr(42) (symbol "*")
  ' Structire of the element: "Id*Value1*Value2* ..."
 
  Const BlockSep = vbCr
  Const LineSep = vbLf
  Const ValueSep = "*"
 
  Dim Serial As String, TheDate  As String
  Dim TestData As String, RetData As String
  Dim i As Long
  Dim a, b
 
  Serial = "12345"
  TheDate = "1/1/2016"
 
  TestData = _
    "A1*1*100*12345*65482" & LineSep & _
    "A2*321654*44545*65421*12345" & LineSep & _
    "A3*1000*1001*1002*1003" & LineSep & _
    "A4*32151*65481*654214*54127" & LineSep & _
    "A5*64821*65412*68251*12426" & LineSep & _
     BlockSep & _
    "A1*2*100*12345*65482" & LineSep & _
    "A2*321654*44545*65421*12345" & LineSep & _
    "A3*1000*1001*1002*1003" & LineSep & _
    "A4*32151*65481*654214*54127" & LineSep & _
    "A5*64821*65412*68251*12426" & LineSep
 
    TestData = Replace(TestData, LineSep, " ")
    a = Split(TestData, BlockSep)
    ReDim b(0 To UBound(a))
    For i = 0 To UBound(a)
      b(i) = Serial & " " & TheDate & " " & Trim(a(i))
    Next
    RetData = Join(b, vbLf)
   
    Debug.Print RetData
   
End Sub
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,221,808
Messages
6,162,097
Members
451,742
Latest member
JuanMark10

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