调用addSimpleBordersToAllSidesOfRange函数时出现Object Required错误
VBA调用addSimpleBordersToAllSidesOfRange函数时出现Object Required错误的解决方法
错误原因及修复步骤:
调用方式错误导致对象引用丢失
你调用函数时给Range参数加了括号,这会强制VBA将Range对象转换为它的默认属性(即单元格值),而函数需要接收的是Range对象本身,因此触发"Object Required"错误。- 错误调用:
addSimpleBordersToAllSidesOfRange (catSheet.range("a1:c2")) - 正确调用(二选一):
' 直接调用,不加括号 addSimpleBordersToAllSidesOfRange catSheet.Range("A1:C2") ' 或者用Call关键字,此时需要括号 Call addSimpleBordersToAllSidesOfRange(catSheet.Range("A1:C2"))
- 错误调用:
函数代码中的拼写错误
你代码里的x1LineStyle是拼写错误,应该是xlLineStyle(数字1要改成小写字母l)。其实可以直接用xlContinuous常量,不用写前缀。函数声明优化
这个函数不需要返回值,把它声明为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
相关产品推荐
相关产品推荐

