Results 1 to 3 of 3

Thread: Excel VBA, trim

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    12

    Excel VBA, trim

    Hi guys,

    basically what i have is a cell with a couple of price codes all consist of 4 characters. I am able to pull them from the cell and populate a listbox with one item, however the cell has a number of items in it which i would like to be listed seperatly (as their own item in the list)

    example codes
    PS7D, PSHD, NW7D

    I would like them to list as
    PS7D
    PDHD
    NW7D
    (multiple items)

    Not
    PS7D, PSHD, NW7D
    (one item)

    How can i achieve this as at the moments they are all listing as one item in the listbox

    here is the code i am using to pull the information from the cell, if it helps
    Code:
    'This clears the listbox on the frmReturns form so that new values can be added
    frmReturns.lstRentals.Clear
    
    Dim ws As Worksheet
    Dim ilastrow As Long
    Dim irow As Long
     
    Set ws = Worksheets("CustomerDetails")
    ilastrow = lstCustomers.ListIndex + 5
     
    For irow = lstCustomers.ListIndex + 5 To ilastrow 'u cannot start from 0 bcoz there is no such cell-A0
        If Trim(ws.Range("H" & irow).Value) <> "" Then
            With frmReturns.lstRentals
                .AddItem Trim(ws.Range("H" & irow).Value)
            End With
        End If
    Next irow
    Thanks,

    Marc

  2. #2
    Member
    Join Date
    Nov 2009
    Posts
    35

    Re: Excel VBA, trim

    Have a look at the Split method. It will split the string into an array, and then you can loop through that array to add single items.

    -Terry

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA, trim

    Something like
    Code:
    Private Sub CommandButton1_Click()
    Dim sTest As String
    Dim arrTest() As String
    Dim i As Long
    sTest = "PS7D, PSHD, NW7D"
    arrTest = Split(sTest, ",")
    For i = 0 To UBound(arrTest)
        ListBox1.AddItem Trim(arrTest(i))
    Next
    End Sub

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