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

VBA中修改字符串数组元素时出现类型不匹配错误求助

VBA类型不匹配错误原因及解决方法

你的错误根源在于类型不匹配:

  • quotes是一维字符串数组,其中每个元素quotes(i)是单个字符串类型
  • Split()函数的返回值是字符串数组(而非单个字符串),你试图把一个数组赋值给单个字符串变量,自然触发类型不匹配错误。

根据你的需求,有两种常见解决方案:

方案1:仅保留分割后的第一个元素

如果只需要取Subject按/分割后的第一部分,新增一个数组变量接收Split结果,再将目标元素赋值给quotes(i):

Dim i As Integer
Dim quotes() As String
Dim splitParts() As String ' 用于存储Split的结果

While TypeName(currentAppointment) <> "Nothing"
     ReDim Preserve quotes(0 To i) As String
     quotes(i) = currentAppointment.Subject
     
     splitParts = Split(quotes(i), "/")
     ' 确保分割后有元素,避免下标越界
     If UBound(splitParts) >= 0 Then
         quotes(i) = splitParts(0)
     End If
     
     i = i + 1
    Set currentAppointment = myAppointments.FindNext
Wend

方案2:保存分割后的所有元素数组

如果需要完整保留每个Subject分割后的所有部分,需要将quotes定义为Variant类型数组(因为它要存储数组的数组):

Dim i As Integer
Dim quotes() As Variant ' 改为Variant类型,可存储数组
Dim splitParts() As String

While TypeName(currentAppointment) <> "Nothing"
     ReDim Preserve quotes(0 To i) As Variant
     splitParts = Split(currentAppointment.Subject, "/")
     quotes(i) = splitParts ' 将分割后的数组存入Variant数组
     
     i = i + 1
    Set currentAppointment = myAppointments.FindNext
Wend

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:03:14