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

调用addSimpleBordersToAllSidesOfRange函数时出现Object Required错误

VBA调用addSimpleBordersToAllSidesOfRange函数时出现Object Required错误的解决方法

错误原因及修复步骤:

  1. 调用方式错误导致对象引用丢失
    你调用函数时给Range参数加了括号,这会强制VBA将Range对象转换为它的默认属性(即单元格值),而函数需要接收的是Range对象本身,因此触发"Object Required"错误。

    • 错误调用:addSimpleBordersToAllSidesOfRange (catSheet.range("a1:c2"))
    • 正确调用(二选一):
      ' 直接调用,不加括号
      addSimpleBordersToAllSidesOfRange catSheet.Range("A1:C2")
      ' 或者用Call关键字,此时需要括号
      Call addSimpleBordersToAllSidesOfRange(catSheet.Range("A1:C2"))
      
  2. 函数代码中的拼写错误
    你代码里的x1LineStyle是拼写错误,应该是xlLineStyle(数字1要改成小写字母l)。其实可以直接用xlContinuous常量,不用写前缀。

  3. 函数声明优化
    这个函数不需要返回值,把它声明为Sub比Function更合理。

修正后的完整代码:

修正后的Sub过程:

Public Sub addSimpleBordersToAllSidesOfRange(ByRef range2 As Range)
     With range2
        .Font.Bold = True
        .Borders(xlEdgeBottom).LineStyle = xlContinuous
        .Borders(xlEdgeLeft).LineStyle = xlContinuous
        .Borders(xlEdgeRight).LineStyle = xlContinuous
        .Borders(xlEdgeTop).LineStyle = xlContinuous
     End With
End Sub

修正后的调用代码:

Dim catSheet As Worksheet
Set catSheet = seeIfSheetIsCreatedAndIfNotCreateOneAndReturnIt(CStr(cat), prodSoldByCatMonthlyWb)
addSimpleBordersToAllSidesOfRange catSheet.Range("A1:C2")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 21:15:19