2014-06-27 123 views
1

我正在將一個工作表分配給一個變量以使其更易於使用。我在這個對象上進行的所有其他操作(比較和寫入)都可以工作,但是我無法將它放在範圍的周圍。它給我100範圍方法失敗的錯誤。我在這裏做錯了什麼?VBA:正在運行時間1004:對象'_Worksheet'的方法'範圍'失敗

代碼有問題(最後一行是在調試器觸發):

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

使用target.CellsCells因爲否則Cells上下文並不一定是你希望它是什麼?

+0

完美!這實際上是有道理的,這就是爲什麼我把工作表放到一個簡短的,易於輸入的變量中......謝謝。 – JSM

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 
相關問題