|
-
Jul 27th, 2000, 10:37 AM
#1
Thread Starter
Member
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?
-
Jul 27th, 2000, 12:11 PM
#2
Fanatic Member
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
-
Jul 31st, 2000, 10:45 AM
#3
Thread Starter
Member
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
-
Jul 31st, 2000, 11:37 AM
#4
Fanatic Member
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...
-
Jul 31st, 2000, 12:13 PM
#5
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|