You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用函数传递ListBox时出现类型不匹配错误求助

解决VBA类型不匹配错误
  • 问题原因:你的listAvailableFiles函数中,参数DropDown声明为ListBox,但用户窗体上的ListBox控件属于MSForms.ListBox类型,两者并非同一类型,导致传递控件时触发类型不匹配错误。

  • 修复方案:将函数的参数类型修改为MSForms.ListBox,确保类型匹配。

修改后的代码如下:

Function listAvailableFiles(FolderPath As String, DropDown As MSForms.ListBox)
    Dim File As String
    File = Dir(FolderPath & "\*.cdr")
    Do While File <> ""
        DropDown.AddItem File
        File = Dir
    Loop
End Function

Private Sub UserForm_Activate()
    Dim DimensionFolder As String
    DimensionFolder = "C:\Users\cwats\OneDrive - Watson Memorials\Watson Memorials\Designs Template and Drawings\MD UK\Memorial Shapes\Foundations"
    listAvailableFiles DimensionFolder, ListDimensionFilesDropdown
End Sub
  • 额外说明:
    • VBA中调用函数时可省略Call关键字,直接以函数名 参数1, 参数2的形式调用,这是更常用的写法。
    • 确保你的VBA项目已引用Microsoft Forms 2.0 Object Library(创建用户窗体时通常会自动添加该引用)。

内容的提问来源于stack exchange,提问作者Craig Waton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 10:35:25