Results 1 to 24 of 24

Thread: [RESOLVED] VB 6.0 Mid$ function split.

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    [RESOLVED] VB 6.0 Mid$ function split.

    Hello, VB 6.0 Mid$ function split.

    I Need someone's assistance in this seemingly straightforward code I am usingand having some difficulties with.

    [CODE]Lines = Split(Text1, vbNewLine)
    b = 2
    For a = 0 To UBound(Lines)
    inp = inp & Mid$(Text1, b, 3) & vbNewLine
    b = b + 2
    Text2 = inp & vbNewLine
    Next

    What I am trying to achieve here is: from the Text1.text in this format to another Text2.text1

    1 2 3
    2 3 4
    5 6 7
    8 9 9
    9 8 7..... and so..and so...

    to get
    1 2
    2 3
    5 6
    8 9 and later the second column

    2 3
    3 4
    6 7
    9 9 etc...

    Any suggestion in the matter would be greatly appreciated.Thank you.
    Last edited by rpmax47; Apr 10th, 2023 at 03:28 PM.

  2. #2
    Addicted Member ISAWHIM's Avatar
    Join Date
    Jan 2023
    Posts
    181

    Re: VB 6.0 Mid$ function split.

    Looks like you need to just split the lines into an array and then build new lines with the array values.

    I would use Split() for ease of coding. Splits the whole string into an array in one shot.
    https://learn.microsoft.com/en-us/of...split-function

    Code:
    Dim tempArray() As String
    Dim tempText as String
    Text2.text = ""
    ' Your line-loop here, to get MyLine
    tempArray = Split(Myline, " ")
    ' Direct output of the first format (0, 1)
    Text2.text = Text2.text & tempArray(0) & " " & tempArray(1) & vbCrLf
    ' Temp storage of the second format, so you don't have to loop through the code again
    tempText = tempText & tempArray(1) & " " & tempArray(2) & vbCrLf
    ' End of your line loop
    
    ' In some button_click()...
    ' Your other built string-list with the other format (1, 2)
    Text2.text = tempText
    You can use Ubound(tempArray) to determine the total number of values, if they are more than the 3... (0, 1, 2) That is more relevant if you use Split to also split lines from a whole string of the textbox, instead of reading "NewLine".

    You could read all lines into a string, then do a double-split. One for the "lines" Split(MyText, vbCrLf) then again for each line, in that array... MyVals = Split(MyTextArr(x), " "). Though I am not sure what gains that would offer, other than using both text boxes at the same time, for the processed output.

    I am not 100% sure if your sample desires are a literal or suggestive format.
    Last edited by ISAWHIM; Apr 5th, 2023 at 02:28 PM.
    Please, chime-in on my latest WIP.
    I can use all the help I can get.
    [VB6 Game], (In Development), "Galactic-Bondsman"

  3. #3
    Addicted Member ISAWHIM's Avatar
    Join Date
    Jan 2023
    Posts
    181

    Re: VB 6.0 Mid$ function split.

    Mid would only be used if you "explicitly know" the positions to cut the values out of the text. If your format is literally "1 2 3", then Split() would still be the better choice.

    Especially if your format may include "2 43 17", where a Mid() would fail, unless you use "InStr(1, MyText, " ")" to find the "spaces", to determine your Mid(start, length) values.
    Please, chime-in on my latest WIP.
    I can use all the help I can get.
    [VB6 Game], (In Development), "Galactic-Bondsman"

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: VB 6.0 Mid$ function split.

    Hello, Thanks for the reply.
    I would like to keep all this, in this showing format. " Number space number " , and " number space "
    ( in the 2nd textbox). Further ahead I have lined up some searches and sorts.
    If you have any other ideas/suggestions of how to conduct this splitting operation before I go further
    please let me know. Much appreciated your assistance.
    Last edited by rpmax47; Apr 5th, 2023 at 03:01 PM.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: VB 6.0 Mid$ function split.

    I also forgot to mention that in this exercise the numbers are 0 to 9. There is no higher number then 9.
    I would like to keep this format " Number space number space number "and Number space Number, with keeping track on all vbnewlines. All this is needed for further calculations, searches and sorts.
    Last edited by rpmax47; Apr 5th, 2023 at 02:59 PM. Reason: delete

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: VB 6.0 Mid$ function split.

    Code:
    
    Option Explicit
    
    Private Sub Command1_Click()
        Dim Lines() As String
        Lines = Split(Text1.Text, vbNewLine)
        Dim a As Long
        For a = LBound(Lines) To UBound(Lines)
            Lines(a) = Left$(Lines(a), 3&)
        Next
        Text2.Text = Join(Lines, vbNewLine)
    End Sub
    
    
    ADDED: And maybe Mid$(YourLine, 3&) to grab the second two columns.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: VB 6.0 Mid$ function split.

    Works beautifully. It looks very simple, when you understand the procedure.
    I was programming back in the late 80s, I am retired now.
    I Can not remember using the " join " function ever then.
    Thank you both for helping in the matter, Best Regards,

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: VB 6.0 Mid$ function split.

    Comes with a small head scratcher here in the last part of my project.

    Texbox has 1000 lines, I need to deal with ONLY the last 200 lines for the time being.
    All this is ( must ) to go backwards.
    At first - I Must get the very last line " at the bottom "and go up towards the very first line,
    but get only the last 200 lines in this particular order for time being.
    I came up with something like this code, it does work, but not the way I was expected. Well.. ??
    Suggestion, correction, anything in this matter will be appreciated. Thanks a lot.

    Lines = Split(Text1.Text, vbNewLine)

    For a = UBound(Lines) - 201 To UBound(Lines) - 1
    Line = Split(Lines(a), vbNewLine)
    sOut = sOut & Line(0) & vbCrLf
    Next

    Text2.Text = sOut

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: VB 6.0 Mid$ function split.

    Quote Originally Posted by rpmax47 View Post
    Comes with a small head scratcher here in the last part of my project.

    Texbox has 1000 lines, I need to deal with ONLY the last 200 lines for the time being.
    All this is ( must ) to go backwards.
    At first - I Must get the very last line " at the bottom "and go up towards the very first line,
    but get only the last 200 lines in this particular order for time being.
    I came up with something like this code, it does work, but not the way I was expected. Well.. ??
    Suggestion, correction, anything in this matter will be appreciated. Thanks a lot.

    Code:
    Lines = Split(Text1.Text, vbNewLine)
              
              For a = UBound(Lines) - 201 To UBound(Lines) - 1
              Line = Split(Lines(a), vbNewLine) '???
              sOut = sOut & Line(0) & vbCrLf
              Next
        
        Text2.Text = sOut
    You've already filtered out all of the vbNewLine characters with your initial Split statement. So the Split statement inside of the For Loop is useless since there will never be a vbNewLine character in Lines(a). Inside the For loop, you should be able to just have:

    Code:
    sOut = sOut & Lines(a)

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Changing what you have suggest. :

    Lines = Split(Text1.Text, vbNewLine)

    For a = UBound(Lines) - 201 To UBound(Lines) - 1
    ' Line = Split(Lines(a), vbNewLine)
    sOut = sOut & Lines(a) ' & vbCrLf

    Next

    Text2.Text = sOut

    That does not sems to do the trick.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Sorry, you still want the & vbCrLf at the end of the sOut = ... line. I omitted it by accident.

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    O yes I new that and I add it, but the lines are still messed up going upwards.

  13. #13
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Something like this?

    Code:
    For a = UBound(Lines) To UBound(Lines) - 199 Step -1

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Let's try this... Thanks for replay...
    Bugs me a lot in such a straightforward simple way of programming.
    This going backwards up in lines order ... screwed me a bit.

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Works in a opposite order. Puts the last ( bottom ) number on the top ( revers situation)
    where it should be on the bottom going up, but all rest is OK. Getting closer....

  16. #16
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    No idea what you are looking for then, sorry.

  17. #17

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Simple.. Textbox has 1000 lines. I need to deal with last 200 lines. ( not from the first down - but from the last up )
    in this particular order that every last line going up remains as is.
    This is a special format that I must have and keep it after the cut is done.
    That is way I called it a " head scratcher " not getting proper results.

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Talking Re: [RESOLVED] VB 6.0 Mid$ function split.

    1 2 3
    2 3 4
    5 6 7
    8 9 9
    3 4 6
    4 6 7
    3 3 1
    9 8 7..... and so..and so...

    In the final I need to see them let's cut them from the bunch by 5... they should look like:
    for me: to - deal with them further ahead in this format.

    8 9 9
    3 4 6
    4 6 7
    3 3 1
    9 8 7
    Last edited by rpmax47; Apr 7th, 2023 at 09:47 PM.

  19. #19
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Quote Originally Posted by rpmax47 View Post
    1 2 3
    2 3 4
    5 6 7
    8 9 9
    3 4 6
    4 6 7
    3 3 1
    9 8 7..... and so..and so...

    In the final I need to see them let's cut them from the bunch by 5... they should look like:


    8 9 9
    3 4 6
    4 6 7
    3 3 1
    9 8 7
    That example doesn't seem to agree with what you've said in a couple of the earlier posts.
    In those posts you say you want the values to be in reverse row order, but in your example you're showing them in the same order.

    I started to say you need to loop in reverse order, i.e. Step -1 which was already mentioned (OptionBase1 post#13), but then your example belies that requirement.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  20. #20

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Post #13 gives me exactly what I am looking for in a way of cuts, but the display result is in opposite order.
    The last line is being displayed on the top and all other lines are going down, reversing exactly what I was looking for.
    From the beginning I have try to describe this as much detailed as possible.
    Thanks for assistance, Much appreciated.
    I will work on your suggestion have the textbox display revers vertically " in a way of speaking. "
    Last edited by rpmax47; Apr 7th, 2023 at 08:16 PM.

  21. #21

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Attachment 187364


    This is exactly in vertical reverse as I indicated early in this post.

    7 7 7 The very " bottom " line must be at the bottom of Text2 and all others lines up above.
    Picture shows of Vetrival revers lines of what I was after.
    The code must be changed. I have tried in so many ways..., but failed.
    Last edited by rpmax47; Apr 8th, 2023 at 01:03 AM.

  22. #22
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Code:
    Lines = Split(Text1.Text, vbNewLine)
    
    For a = UBound(Lines) - 199 To UBound(Lines)
        sOut = sOut & Lines(a) & vbCrLf
    Next
    
    Text2.Text = sOut
    If that doesn't work, then please post the exact data you are working with, along with the exact results of the code. This is essentially the code you already posted earlier except the bounds are corrected (I believe) to get the final 200 lines, rather than the penultimate 201 lines.

  23. #23

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Thank you for the reply.
    I will get back to you in a couple of days (out of town) with all you have requested.
    Meanwhile Happy Easter, All the Best. rpmax47

  24. #24

    Thread Starter
    Member
    Join Date
    Mar 2023
    Location
    Canada
    Posts
    36

    Re: [RESOLVED] VB 6.0 Mid$ function split.

    Finally I got the issue resolved.
    Your suggestion was the key and for the rest I just follow up.
    The starting point was the first cut, but the display wasn't showing the proper order I need, (see attached first picture.)
    For simplicity I used the same data format of 29 lines instead of over 4000 lines:
    Number space Number space Number vbnewline, and started cutting by 5 lines only (for this test purposes.)
    Lines = Split(Text1.Text, vbNewLine)

    For a = UBound(Lines) - 5 To UBound(Lines) - 1
    ' Line = Split(Lines(a), vbNewLine)...... not needed
    sOut = sOut & Line(0) & vbCrLf
    Next

    Text2.Text = sOut
    This produced this outcome:

    Attachment 187382

    Now this is my follow up:

    Dim Lines() As String
    Dim a, b, c, k, n As Long
    Dim sOut As String
    Dim Lns() As String

    Lns() = Split(Text1.Text, vbNewLine)
    Lines = Split(Text1.Text, vbNewLine)

    c = 5

    For a = UBound(Lns()) - c + 1 To UBound(Lines) ' From the bottom up
    sOut = sOut & Lines(a) & vbCrLf
    Next
    Text3.Text = sOut
    sOut = ""

    For a = LBound(Lines) To LBound(Lines) + c - 1 Step 1 ' From the top down OK
    sOut = sOut & Lines(a) & vbCrLf
    Next
    Text2.Text = sOut

    End Sub

    This let me choose number of lines going from the first to the chosen last ( 5 lines as an example here )
    and from the last to the first (both ways). To choose number of lines is up to me - how many I can take for further procedures.

    Dim Lines() As String
    Dim Lns() As String
    Dim a, b, c, k, n As Long


    k = 11
    n = 15 ' K < N always


    Lines = Split(Text1.Text, vbNewLine)
    Lns() = Split(Text1.Text, vbNewLine)

    For a = LBound(Lines) + k - 1 To LBound(Lines) + n - 1 ' From any line going DOWN
    sOut = sOut & Lines(a) & vbCrLf
    Next
    Text4.Text = sOut

    sOut = ""

    For a = UBound(Lns) - n + 1 To UBound(Lines) - k + 1 'N > K always From any line going UP
    sOut = sOut & Lines(a) & vbCrLf
    Next
    Text5.Text = sOut

    I also added splitting number of lines start counting from the first line ( 11 lines down as the start of splitting)
    and going down 15 lines down from the first line to the END of the split.
    The same splitting " chosen number of lines " goes from the last line upward.
    This way I cover all mine needs in regard to the splitting.
    Here is the picture of this outcome. Splitting from any line to any line.

    Attachment 187383
    Last edited by rpmax47; Apr 9th, 2023 at 06:18 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width