Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

String variable Incrimentation and ListBoxes in VBA


 I am having a hard time with VB for applications trying to copy all the text in a ListBox on a UserDefined form, and writting it to a file on a specified path ...

 The problem is when I write to the file, through a For To loop, that loops through the items in my ListBox, stores them in a global String, and writes that string to a file ... the out come of this is always one item being writen to the file, because the ListBox items inseted over write each others ...

 Please advice me on a way to increment string values in VBA, like in Java for example using ' String +=' so that I can have all the items in the ListBaox, assigned to my global String variable, using the For loop.

Or maybe you can help me get all of the text raws displayed in a ListBox, and assign them all at once to a String variable ...

Thank you  
1 Solution
You can use array instead:

dim strAry() as string

Then in your loop:
for i = 0 to listbox.itemcount - 1
   strary(i) = ...

~ fantasy ~
dim myString as String

myString = "blah"
myString = myString & " more text"
myString = myString & " foo"

debug.print myString

this will result in:   blah more text foo
Dim i
Dim str

For i = 0 To List1.ListCount - 1
str = str & List1.List(i) & vbCrLf
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Why not just print each item directly to a line in the file.

Dim ff As Integer, i As Integer
ff = FreeFile
Open "C:\Somefile.txt" For Output As #ff
For i = 0 To List1.ListCount - 1
Print #ff, List1.List(i)
Next i
Close #ff

In case it wasn't clear, the VBA equivalent of the Javascript

    String1 += String2;


    String1 = String1 & String2



Hello RaedKhlaifat !

It's not clear what you mean:

You can concatenate strings by this way for example:

Dim strTemp As String
strTemp = strTemp & chr(int(rnd*255))

Incrementing a String is an other topic:

"ABC" incremented by one is then "ABD"

This is more complicated:

Public Function IncrementString(Text As String, Min As Byte, Max As Byte)
    Dim strTemp As String
    Dim Count As Byte
    Dim i As Long
    Dim n As Long
    Dim x As Boolean

    Select Case True
        Case Len(Text) = 0
        Case Min > Max
        Case Else
            Count = Max - Min + 1
            n = Asc(Right(Text, 1)) - Min
            x = (n + 1 = Count)
            strTemp = Chr(Min + ((n + 1) Mod Count))
            For i = Len(Text) - 1 To 1 Step -1
                n = (Asc(Mid(Text, i, 1)) - Min + IIf(x, 1, 0)) Mod Count
                x = (n = 0)
                strTemp = Chr(Min + n) & strTemp
            If x Then
                strTemp = Chr(Min) & strTemp
            End If
    End Select
    IncrementString = strTemp
End Function


? IncrementString("ABC",65,90)
? IncrementString("AZZ",65,90)
? IncrementString("ZZZ",65,90)

Because it seems that you have only one item in your file:

1. You might have overwritten the content of your global string with the text of the next listitem every time in the loop.
    Instead append your global string by the text . See how to concatenate strings in vb above.
2. You might have forgotten to add (chr(13) & chr(10))  = CR+LF = vbNewline to the end of the text of each listitem before appending to the file. In this case there should be only a big row containing all the text.

RaedKhlaifatAuthor Commented:
Thank you all for your comments,

 I ahve just used an array of strings as a buffer, which clears itself after each usage, and that solved the problem, even though I was hoping for a more Robust solution, such as String A += String B , and whatever strings that are paased to String A will be added to its value, but it seems like it doesn't work that way in VBA, like in Java.

 Anyway, thank you all for your effort, it has all been very useful.

Raed Khlaifat
It DOES work that way in VBA, but the syntax is different.

As posted earlier, the VBA equivalent of "A += B" is "A = A & B"

Never mind, good luck in the future.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now