Results 1 to 5 of 5

Thread: Using VB in Excel

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2000
    Posts
    35

    Question

    It's the first tiem I try to write VB code in an Excel spreadsheet and can't get it to work.
    I have a book with a number of sheets, in one of them is a combo I'd like to have automatically filled with the names of all the sheets when the file is opened.
    I've tried to add some code to the Worksheet_activate event, yet to no avail. What is the right object and event to put my code in?

  2. #2
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Right click on an empty error next to the toolbar. Select the FORM toolbar. Drop either a listbox or combobox on the form and right click to select FORMAT CONTROL. Go to the CONTROL tab and select the INPUT RANGE icon. Click on COLUMN A and hit ENTER. Close the dialog box. Run the following codes!

    Code:
    Sub Macro1()
      Cells.Clear
      Dim int_X As Integer
      For int_X = 1 To Worksheets.Count
        Sheets("Sheet1").Select
        Cells(int_X, 1).Value = Sheets(int_X).Name
      Next
    End Sub
    Chemically Formulated As:
    Dr. Nitro

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2000
    Posts
    35

    Arrow More on using VB in Excel

    Thanks for the hint, but how do I have the macro automatically execute when I open the spreadsheet file?

    Originally posted by Nitro
    Right click on an empty error next to the toolbar. Select the FORM toolbar. Drop either a listbox or combobox on the form and right click to select FORMAT CONTROL. Go to the CONTROL tab and select the INPUT RANGE icon. Click on COLUMN A and hit ENTER. Close the dialog box. Run the following codes!

    Code:
    Sub Macro1()
      Cells.Clear
      Dim int_X As Integer
      For int_X = 1 To Worksheets.Count
        Sheets("Sheet1").Select
        Cells(int_X, 1).Value = Sheets(int_X).Name
      Next
    End Sub

  4. #4
    Fanatic Member coox's Avatar
    Join Date
    Oct 1999
    Posts
    550
    Right. So, for example, you've got your workbook and it's got three sheets. Your combo is on sheet 1 which is called, for the sake of argument, "Sheet1", and the combo is called "ComboBox1". Ok so far? (you'll substitute your own names here later).

    Open the VB editor (open Excel, then hit Alt+F11), and you'll see the guts of your workbook. In the Project Explorer window, double-click on "ThisWorkbook", pick "Workbook" from the top left drop-down and the top-right drop-down will probably default to "Open", which is fine, and it should give you the following:
    Code:
    Private Sub Workbook_Open()
    
    End Sub
    Right, stick the following in there:
    Code:
     With Sheet1.ComboBox1
      For Each sheet In ActiveWorkbook.Worksheets
       .AddItem sheet.Name
      Next sheet
      .ListIndex = 0 
     End With
    Now save your workbook, close it, and then open it again, and that should give you what you want. I think...

  5. #5
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Coox is right, Private Sub Workbook_Open() is like Form_load. Place your codes in this event and do an

    application.run your macro
    Chemically Formulated As:
    Dr. Nitro

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