Results 1 to 2 of 2

Thread: Accessing Excel in Different Forms with ListBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400

    Accessing Excel in Different Forms with ListBox

    I need some advice on loading Excel values into a Listbox in a form (Form2) different than the main form (Form1).

    In Form 1 I have the following
    Code:
    Public xlApp As Excel.Application
    Public xlBook As Excel.Workbook
    Public xlSheetData As Excel.Worksheet
    
    Private Sub Form_Load()
    
        Set xlApp = New Excel.Application
        Set xlBook = xlApp.Workbooks.Open(App.Path & "\myFile.xls")
        Set xlSheetData = xlBook.Worksheets("input")
    
    End Sub
    I then access Form2 using:
    Code:
    Private Sub mnuData_Click()
    
        Form2.Show vbModeless, Me
    
    End Sub
    On Form2 I have a Listbox that I want to populate with values from the worksheet "input" when this form loads. I tried"
    Code:
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheetData As Excel.Worksheet
        
       Set xlSheetData = xlBook.Worksheets("input")
        List1.DataSource = xlSheetData.Cells("D26..D50").Value
    end sub
    but VB doesn't like my use of DataSource . Any suggestions?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    Montreal, Quebec
    Posts
    400
    Still muddling along.

    In the FormLoad of Form2 I tried:
    Code:
    Private Sub Form_Load()
        Dim xlApp As Excel.Application
        Dim xlBook As Excel.Workbook
        Dim xlSheetData As Excel.Worksheet
        Dim data As Long
        
       Set xlSheetData = xlBook.Worksheets("input")
       data = xlSheetData.Cells("D26").Value
        List1.AddItem data
    end sub
    But I get an error indicating that my xlSheetData is not set. How do I set a worksheet when I change to a different form (I have no problems with Form 1 - see above)? And am I using the AddItem correctly?

    Thanks - Stan

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