VBA:获取运行时间1004:方法'范围'对象' _Worksheet'失败

时间:2014-06-27 18:57:21

标签: excel-vba runtime-error vba excel

我正在为变量分配工作表,以便更容易使用。我对这个对象(比较和写入)所做的所有其他操作,但我不能让它在一个范围内放置一个边框。它给出了Range方法失败的1004错误。我在这里做错了什么?

有问题的代码(最后一行是调试器触发的地方):

Dim destRow As Range
    Dim lastRow As Long
    Dim target As Worksheet
    Dim listSize As Long

    listSize = Me.FeatureNumber_ListBox.listCount
    Set target = ActiveWorkbook.Worksheets("mySheet")
    lastRow = target.Cells(Rows.Count, "A").End(xlUp).Row + 1

    ' put borders around whole row
    target.Range(Cells(lastRow, 1), Cells(lastRow, 19)).Borders.LineStyle = xlContinuous

由于

2 个答案:

答案 0 :(得分:2)

使用target.Cells而不是Cells,否则Cells上下文不一定是您想要的位置?

答案 1 :(得分:0)

更正后的代码:

with target

    lastRow = .Cells( .Rows.Count, "A").End(xlUp).Row + 1  'added 2 dots (".")

    ' put borders around whole row
    .Range( .Cells(lastRow, 1), .Cells(lastRow, 19)).Borders.LineStyle = xlContinuous  ' Added 3 Dots (".")

    '...
End With