运行时错误'1004'Range.boarders

时间:2013-12-19 22:21:39

标签: excel vba excel-vba

使用此段代码接收错误。我在Excel 2013计算机上使用Excel 2010工作表中的代码,它可以正常工作。我的所有库都已正确更新,我们不确定为什么这似乎不是向前兼容的。

With Me.Range("T3:Y196")

    .borders(xlEdgeBottom).Weight = xlThick
    .borders(xlEdgeLeft).Weight = xlThick
    .borders(xlEdgeRight).Weight = xlThick    '<--- error here
    .borders(xlEdgeTop).Weight = xlThick      '<--- once above code is commented out error occurs here

End With

此处也收到错误:

With Range(ConvertColumnNumberToLetter(questionTextCol - 1) & CStr(myRow) & ":" & ConvertColumnNumberToLetter(instructionsCol) & CStr(myRow)).borders(xlEdgeTop)
'above Range gives same result as "T3:Y196" but I hard coded it to do some testing in the previous With Statement
    .LineStyle = xlContinuous    '<--- Error
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium           '<--- Error
End With

现在,.LineStyle.Weight都在代码的其他区域工作,因此我们不确定该如何处理。

当我录制宏时,我得到了这个:

With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With

当我添加范围时,它完美地工作(再次,这是在一张干净的纸上):

With Range("C2:I20")
    .Borders(xlEdgeBottom).Weight = xlMedium
    .Borders(xlEdgeRight).Weight = xlMedium
    .Borders(xlEdgeLeft).Weight = xlMedium
    .Borders(xlEdgeTop).Weight = xlThick
End With

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

我不知道这是否会有所帮助,而不是与Me.Range一起使用,尝试设置一个工作表对象并从那里开始工作。我倾向于以这种方式减少故障。示例

    Dim TheSheet as Worksheet
    Set TheSheet = Sheets("Your Sheet Name")
    With TheSheet.Range("T3:Y196")
         .borders(xlEdgeBottom).Weight = xlThick
         .borders(xlEdgeLeft).Weight = xlThick
         .borders(xlEdgeRight).Weight = xlThick    '<--- error here
         .borders(xlEdgeTop).Weight = xlThick  
    End With

我能想到的另一件事是合并的单元格问题,其中一半的单元格在设定的范围内,而一半的单元格不在。