Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2011, 11:38 AM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default ComboBox1

Hi,

I have created a ComboBox in a Userform.

I have added the following items using code below:

Code:
Private Sub UserForm_Initialize()
    With ComboBox1
        .AddItem "France"
        .AddItem "Italy"
        .AddItem "Spain"
        .AddItem "Guatemala"
    End With
End Sub
Instead of adding each item (ie. country names) can I put in code the reference to a country list in Excel? This way I would not have to add each single item but simply reference cells.

So if my country names were found in Sheet1 from A1 to A4 how would my code look?

Thanks!
 
Old September 6th, 2011, 07:07 AM
Authorized User
 
Join Date: May 2010
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default Combo Box LOading

The VB end of tis is quite simple...

Code:
 

Public Class Form1
 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        FillCBO()
    End Sub


    Public Sub FillCBO()
       
        ' Put your code here to read from xl... I'm not sure how but if I were doiing it I would   export the XLS information From the Spreadsheet
        ' into a Text file named "c:\TextFiles\Countries.
           ' Then Read the text file with a stream reader ie.
        Dim fileReader As System.IO.StreamReader
        fileReader = _
        My.Computer.FileSystem.OpenTextFileReader("C:\TextFiles\Countries.txt")


        For j = 1 To 5
              ' While reading the file ...

            ComboBox1.Items.Add(fileReader.ReadLine())

        Next


        ' search "Help" For "How to read Text From Files With A Stream Reader (Visual Basic)"


    End Sub

End Class
Hopefully someone; who is better with VBA than I am; will be able to fill in the gaps for you.... you might search "Help" for How to read from XML files in Visual Basic ... I Think the newer Spreadsheets will allow exporting into a Spread Sheed.... Good Luck!
 
Old September 7th, 2011, 07:52 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You can link the Listbox to an Excel table using VBA (http://vbadud.blogspot.com/2011/09/h...o-listbox.html)

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.