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

VBA中ListBox绑定表格数据时RowSource无效问题求助

解决VBA ListBox RowSource 运行时错误'380'的方案

1. 修复带空格的工作表名引用格式

你的工作表名包含空格,直接写Primary Data!A1:K...会被VBA识别为无效引用,必须给工作表名包裹单引号:

.RowSource = "'Primary Data'!A1:K" & last_Row

2. 验证last_Row的有效性

错误大概率是last_Row值无效(比如为0、空值或超出工作表实际行数)。先确认你获取最后一行的代码逻辑正确,推荐用稳定的写法:

Dim last_Row As Long
With ThisWorkbook.Worksheets("Primary Data")
    last_Row = .Cells(.Rows.Count, "A").End(xlUp).Row
    ' 空表时强制设为1,避免生成无效范围
    If last_Row = 0 Then last_Row = 1
End With

如果last_Row为0,会导致RowSource变成'Primary Data'!A1:K0,这显然是无效单元格范围。

3. 改用List属性替代RowSource(更适合新手)

RowSource对格式要求严苛,新手容易踩坑。直接用List属性赋值数据数组,稳定性更高,还能规避引用格式问题:

Dim ws As Worksheet
Dim dataRange As Range
Dim last_Row As Long

Set ws = ThisWorkbook.Worksheets("Primary Data")
With ws
    last_Row = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set dataRange = .Range("A1:K" & last_Row)
End With

' 直接将区域数据赋值给ListBox
Me.ListBox1.List = dataRange.Value

4. 额外检查项

  • 确认ListBox的ColumnCount属性设置正确(你的数据是11列,需将该属性设为11),否则数据加载后显示会异常。
  • 核对工作表名拼写完全一致(VBA不区分大小写,但严格匹配能避免不必要的问题)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 10:45:37