+ Reply to Thread
Results 1 to 3 of 3

List Box Help

Hybrid View

  1. #1
    Registered User
    Join Date
    09-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    2

    List Box Help

    Hi all, I am new to VB but have some programming experience.

    The Task:

    I have two data sets I need to compare and cross reference. I have them on two sheets in the workbook. I am linking each of the sheets to a listbox...fairly straight forward and all working so far.

    New I want to reference one sheet on the other the create a third listbox that contains a combination of both, in a certain format to be used in another process. The formatting is quite important for the next task.

    Here is my code:

    Dim x As Integer 'LOOPER SHEET ONE
    Dim y As Integer 'LOOPER SHEET TWO
    Dim i As Integer 'COLUMN LOCATION VARIABLE
    Dim j As Integer 'LOOKUP LOCATOR VARIABLE
    
    Dim dT As Date
    
    dT = Now()
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "TEXT"
    .ColumnCount = 6
    .AddItem
    .Column(0, i) = "TEXT"
    .Column(1, i) = ""
    .Column(2, i) = ""
    .Column(3, i) = ""
    .Column(4, i) = ""
    .Column(5, i) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "SPACER ROW"
    .AddItem
    .Column(0, i + 1) = dT
    .Column(1, i + 1) = ""
    .Column(2, i + 1) = ""
    .Column(3, i + 1) = ""
    .Column(4, i + 1) = ""
    .Column(5, i + 1) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "SPACER ROW"
    .AddItem
    .Column(0, i + 2) = ""
    .Column(1, i + 2) = ""
    .Column(2, i + 2) = ""
    .Column(3, i + 2) = ""
    .Column(4, i + 2) = ""
    .Column(5, i + 2) = ""
    End With
    
    For x = 0 To ListBox1.ListCount - 1 'LOOP THROUGH CNS DATA SHEET
    
    For y = 0 To ListBox2.ListCount - 1 'LOOP THROUGH DATABASE SHEET
     
     If ListBox2.List(y, 0) = ListBox1.List(x, 5) Then 'LOOKUP CNS NAMES ON DATABASE
     j = y 'SAVE LOCATION ADDRESS IN VARIABLE J
     End If
     
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "TEXT"
    .AddItem
    .Column(0, i + 3) = "TEXT"
    .Column(1, i + 3) = ""
    .Column(2, i + 3) = ""
    .Column(3, i + 3) = ""
    .Column(4, i + 3) = ""
    .Column(5, i + 3) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "PLANT NAME"
    .AddItem
    .Column(0, i + 4) = ListBox1.List(x, 5)
    .Column(1, i + 4) = ""
    .Column(2, i + 4) = ""
    .Column(3, i + 4) = ""
    .Column(4, i + 4) = ""
    .Column(5, i + 4) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "LQA / HNW"
    .AddItem
    .Column(0, i + 5) = "LQA=" & Format(ListBox2.List(j, 19), "00000.00")
    .Column(1, i + 5) = "HNW=" & Format(ListBox2.List(j, 21), "00000.00")
    .Column(2, i + 5) = ""
    .Column(3, i + 5) = ""
    .Column(4, i + 5) = ""
    .Column(5, i + 5) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "OCTAVE BANDS 1 - 6"
    .AddItem
    .Column(0, i + 6) = "LQ1=" & Format(ListBox2.List(j, 10), "00000.00")
    .Column(1, i + 6) = "LQ2=" & Format(ListBox2.List(j, 11), "00000.00")
    .Column(2, i + 6) = "LQ3=" & Format(ListBox2.List(j, 12), "00000.00")
    .Column(3, i + 6) = "LQ4=" & Format(ListBox2.List(j, 13), "00000.00")
    .Column(4, i + 6) = "LQ5=" & Format(ListBox2.List(j, 14), "00000.00")
    .Column(5, i + 6) = "LQ6=" & Format(ListBox2.List(j, 15), "00000.00")
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "OCTAVE BANDS 7 - 9"
    .AddItem
    .Column(0, i + 7) = "LQ7=" & Format(ListBox2.List(j, 16), "00000.00")
    .Column(1, i + 7) = "LQ8=" & Format(ListBox2.List(j, 17), "00000.00")
    .Column(2, i + 7) = "LQ9=" & Format(ListBox2.List(j, 18), "00000.00")
    .Column(3, i + 7) = ""
    .Column(4, i + 7) = ""
    .Column(5, i + 7) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "PNO"
    .AddItem
    .Column(0, i + 8) = "PNO=" & Format(ListBox2.List(j, 1), "00000.00")
    .Column(1, i + 8) = ""
    .Column(2, i + 8) = ""
    .Column(3, i + 8) = ""
    .Column(4, i + 8) = ""
    .Column(5, i + 8) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "SPACER ROW"
    .AddItem
    .Column(0, i + 9) = ""
    .Column(1, i + 9) = ""
    .Column(2, i + 9) = ""
    .Column(3, i + 9) = ""
    .Column(4, i + 9) = ""
    .Column(5, i + 9) = ""
    End With
    
    Next y
    
    i = i + 7 'INCREMENT VAR i FOR NEXT LIST ITEM
    
    Next x
    
    End Sub
    For some reason when it created the new listbox contents (and it does do that bit correctly) it also adds thousands of rows to the bottom of the new listbox. This is now causing some issues to the next stage of my work.

    Any ideas?

    Apologies if I am missing something really easy....Thanks
    Last edited by zbor; 09-04-2013 at 08:00 AM. Reason: It's enough to edit. No need for new post

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,038

    Re: List Box Help

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    09-04-2013
    Location
    London, England
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: List Box Help

    Hi all, I am new to VB but have some programming experience.

    The Task:

    I have two data sets I need to compare and cross reference. I have them on two sheets in the workbook. I am linking each of the sheets to a listbox...fairly straight forward and all working so far.

    New I want to reference one sheet on the other the create a third listbox that contains a combination of both, in a certain format to be used in another process. The formatting is quite important for the next task.

    Here is my code:

    
    Dim x As Integer 'LOOPER SHEET ONE
    Dim y As Integer 'LOOPER SHEET TWO
    Dim i As Integer 'COLUMN LOCATION VARIABLE
    Dim j As Integer 'LOOKUP LOCATOR VARIABLE
    
    Dim dT As Date
    
    dT = Now()
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "TEXT"
    .ColumnCount = 6
    .AddItem
    .Column(0, i) = "TEXT"
    .Column(1, i) = ""
    .Column(2, i) = ""
    .Column(3, i) = ""
    .Column(4, i) = ""
    .Column(5, i) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "SPACER ROW"
    .AddItem
    .Column(0, i + 1) = dT
    .Column(1, i + 1) = ""
    .Column(2, i + 1) = ""
    .Column(3, i + 1) = ""
    .Column(4, i + 1) = ""
    .Column(5, i + 1) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "SPACER ROW"
    .AddItem
    .Column(0, i + 2) = ""
    .Column(1, i + 2) = ""
    .Column(2, i + 2) = ""
    .Column(3, i + 2) = ""
    .Column(4, i + 2) = ""
    .Column(5, i + 2) = ""
    End With
    
    For x = 0 To ListBox1.ListCount - 1 'LOOP THROUGH CNS DATA SHEET
    
    For y = 0 To ListBox2.ListCount - 1 'LOOP THROUGH DATABASE SHEET
    
    If ListBox2.List(y, 0) = ListBox1.List(x, 5) Then 'LOOKUP CNS NAMES ON DATABASE
    j = y 'SAVE LOCATION ADDRESS IN VARIABLE J
    End If
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "TEXT"
    .AddItem
    .Column(0, i + 3) = "TEXT"
    .Column(1, i + 3) = ""
    .Column(2, i + 3) = ""
    .Column(3, i + 3) = ""
    .Column(4, i + 3) = ""
    .Column(5, i + 3) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "PLANT NAME"
    .AddItem
    .Column(0, i + 4) = ListBox1.List(x, 5)
    .Column(1, i + 4) = ""
    .Column(2, i + 4) = ""
    .Column(3, i + 4) = ""
    .Column(4, i + 4) = ""
    .Column(5, i + 4) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "LQA / HNW"
    .AddItem
    .Column(0, i + 5) = "LQA=" & Format(ListBox2.List(j, 19), "00000.00")
    .Column(1, i + 5) = "HNW=" & Format(ListBox2.List(j, 21), "00000.00")
    .Column(2, i + 5) = ""
    .Column(3, i + 5) = ""
    .Column(4, i + 5) = ""
    .Column(5, i + 5) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "OCTAVE BANDS 1 - 6"
    .AddItem
    .Column(0, i + 6) = "LQ1=" & Format(ListBox2.List(j, 10), "00000.00")
    .Column(1, i + 6) = "LQ2=" & Format(ListBox2.List(j, 11), "00000.00")
    .Column(2, i + 6) = "LQ3=" & Format(ListBox2.List(j, 12), "00000.00")
    .Column(3, i + 6) = "LQ4=" & Format(ListBox2.List(j, 13), "00000.00")
    .Column(4, i + 6) = "LQ5=" & Format(ListBox2.List(j, 14), "00000.00")
    .Column(5, i + 6) = "LQ6=" & Format(ListBox2.List(j, 15), "00000.00")
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "OCTAVE BANDS 7 - 9"
    .AddItem
    .Column(0, i + 7) = "LQ7=" & Format(ListBox2.List(j, 16), "00000.00")
    .Column(1, i + 7) = "LQ8=" & Format(ListBox2.List(j, 17), "00000.00")
    .Column(2, i + 7) = "LQ9=" & Format(ListBox2.List(j, 18), "00000.00")
    .Column(3, i + 7) = ""
    .Column(4, i + 7) = ""
    .Column(5, i + 7) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "PNO"
    .AddItem
    .Column(0, i + 8) = "PNO=" & Format(ListBox2.List(j, 1), "00000.00")
    .Column(1, i + 8) = ""
    .Column(2, i + 8) = ""
    .Column(3, i + 8) = ""
    .Column(4, i + 8) = ""
    .Column(5, i + 8) = ""
    End With
    
    With ListBox3 'PRINT FORMATTING REQUIREMENTS - "SPACER ROW"
    .AddItem
    .Column(0, i + 9) = ""
    .Column(1, i + 9) = ""
    .Column(2, i + 9) = ""
    .Column(3, i + 9) = ""
    .Column(4, i + 9) = ""
    .Column(5, i + 9) = ""
    End With
    
    Next y
    
    i = i + 7 'INCREMENT VAR i FOR NEXT LIST ITEM
    
    Next x
    
    End Sub
    For some reason when it created the new listbox contents (and it does do that bit correctly) it also adds thousands of rows to the bottom of the new listbox. This is now causing some issues to the next stage of my work.

    Any ideas?

    Apologies if I am missing something really easy....Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 02-22-2015, 11:40 AM
  2. Replies: 0
    Last Post: 01-09-2013, 11:44 AM
  3. [SOLVED] Taking a list of tasks and a list of subtasks and creating a new list with groupings
    By kknb0800 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-06-2012, 12:16 PM
  4. Replies: 3
    Last Post: 07-20-2012, 09:52 AM
  5. list 1 has 400 names List 2 has 4000. find manes from list 1 on 2
    By Max in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 09-06-2005, 09:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1