vba在工作簿打开时自动加载

时间:2016-04-18 23:40:26

标签: excel vba excel-vba listbox

我有代码

Private sub Workbook_open()
 ListBox1.AddItem "2017"
 ListBox1.AddItem "2018"
 ListBox1.AddItem "2019"
 ListBox1.AddItem "2020"
 ListBox1.AddItem "2021"
 ListBox1.AddItem "2022"
 ListBox1.AddItem "2023"
 ListBox1.AddItem "2024"
 ListBox4.AddItem "0%"
 ListBox4.AddItem "10%"
 ListBox4.AddItem "20%"
 ListBox4.AddItem "30%"
 ListBox4.AddItem "40%"
 ListBox4.AddItem "50%"
 ListBox4.AddItem "60%"
End Sub

但是当我打开工作簿时,列表框不会自动填充。有没有办法在没有命令按钮的情况下自动加载列表框项目?

1 个答案:

答案 0 :(得分:1)

我发现定义列表框所在的工作表是否有效

一开始的.clear是为了防止创建多个相同的选项。

Sub Workbook_open()

Sheet1.ListBox1.Clear
Sheet1.ListBox2.Clear

With Sheet1.ListBox1
    .AddItem "2017"
    .AddItem "2018"
    .AddItem "2019"
    .AddItem "2020"
    .AddItem "2021"
    .AddItem "2022"
    .AddItem "2023"
    .AddItem "2024" 
End With

With Sheet1.ListBox4
    .AddItem "0%"
    .AddItem "10%"
    .AddItem "20%"
    .AddItem "30%"
    .AddItem "40%"
    .AddItem "50%"
    .AddItem "60%" 
End With

End Sub