Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Load a column into an array/listbox, skipping a certain string 3

Status
Not open for further replies.

davedave24

Programmer
Aug 26, 2010
113
GB
Hi all. I have a column with many different entries. It's set out weekly, so for each week there will be column headings. How would I load the contents of the column into a listbox, while skipping the empty cells and the column heads?

Example: the string to skip (the heading) is "Consignment Note"

C1 = "Consignment Note"
C2:C10 = stuff we want
*blank cells*
C14 = "Consignment Note"
C15:C26 = stuff we want
*blank cells*
C30 = "Cosignment Note"
C31:C35 = stuff we want
*blank cells
C39 = "Consignment Note"
etc.
etc.

The 'consignment note' string will not appear in any of the data we want by chance; it's all random numbers/letters.

Thanks in advance guys.
 


hi,

loop thru the list excluding blanks & "Consignment Note"

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I solved it thanks

Code:
Private Sub comboCustomers_Change()
        On Error Resume Next
        Dim NoteArray As Variant
        Workbooks("All Deliveries " & Year(Date) & ".xlsx").Activate
        NoteArray = Workbooks("All Deliveries 2011.xlsx").Sheets(comboCustomers.Text).Range("C:C").Value
        Dim i As Long
        ListBox1.Clear
        
        For i = LBound(NoteArray) To UBound(NoteArray)
            If NoteArray(i, 1) <> vbNullString And NoteArray(i, 1) <> "Consignment Note" Then
                ListBox1.AddItem (NoteArray(i, 1))
            End If
        Next i
End Sub
 
Have you had a look at UBound(NoteArray) when your code is running? Just curious as to how mnay times through that loop you are interating.

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
I'd imagine it's extremely innefficient Glenn; I did a msgbox of it and it said something like 1053373
 
davedave, something that may save you a little bit of grief, and all around a little better coding practice.

You can find the last used cell in a column by using the Range().Find method. You can specify that it search backwards, and it returns a cell object. I don't have exact syntax at the moment, but it's in the VBA help file.
 
This will loop a shorter range:
Code:
Private Sub comboCustomers_Change()
        On Error Resume Next
        Dim NoteArray As Variant
        Workbooks("All Deliveries " & Year(Date) & ".xlsx").Activate
        ListBox1.Clear

        For Each c in InterSect(Sheets(comboCustomers.Text).Range("C:C").Value, Sheets(comboCustomers.Text).UsedRange)
               
            If c.Value <> vbNullString And c.Value <> "Consignment Note" Then
                ListBox1.AddItem c.Value
            End If
        Next i
End Sub
... not tested.

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
The "Next i" should just be "Next".

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
Thanks Glenn - what is the variable 'c' in this code?
 
what is the variable 'c' in this code?
A Range object.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sorry it's not working, have I copied it over wrong?
 
Intersect has ranges as arguments:
Code:
For Each c in InterSect(Sheets(comboCustomers.Text).Range("C:C")[s].Value[/s], Sheets(comboCustomers.Text).UsedRange)


combo
 
Ah thanks combo, works great now.

Thanks for your help everyone. Stars all round.
 
Thanks combo ... I obviously didn't proof-read the code enough. :-D

Cheers, Glenn.

Beauty is in the eye of the beerholder.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top