2017-06-19 189 views
0

我刚刚写了一个简单的代码,用于在名为BASE_TOTAL的工作表上从一开始到最后一列(例如,第11列)运行,并且每次找到一个空单元格时,它在它上面写上“Sem Substituto”。excel:填充单元格时运行时错误424

这是我到目前为止已经试过:

Sub FillSubs() 

Dim i As Long 
Dim Lastrow As Long 
Dim ws1 As Worksheet 

Set ws1 = Sheets("BASE_TOTAL") 
Set Lastrow = ws1.Range("A" & rows.Count).End(xlUp).Row 

For i = 1 To Lastrow 
    If ws1.Cells(i, 11).Value = "" Then 
     ws1.Cells(i, 11).Value = "Sem Substituto" 
    End If 
Next i 

End Sub 

但是当我尝试运行这段代码,它给了我:

运行时错误“424”对象所需

,并强调这一部分:

Sub FillSubs() 

虽然我不知道我的变量有什么问题,因为我真的认为我宣称他们是正确的。

我很感激任何帮助。

+1

你不应该需要设置LASTROW。如下 Lastrow = ws1.Range(“A”&rows.Count).End(xlUp).Row – 99moorem

+0

您不需要'设置'一个长。从'Lastrow'行删除'Set':'Lastrow = ws1.Range(“A”&rows.Count).End(xlUp).Row' –

+0

@ScottCraner没错!谢谢,我不知道我为什么这么做。 – paulinhax

回答

1

你不应该 “设置” LASTROW

纠正代码如下

Sub FillSubs() 

Dim i As Long 
Dim Lastrow As Long 
Dim ws1 As Worksheet 

Set ws1 = Sheets("BASE_TOTAL") 
Lastrow = ws1.Range("A" & rows.Count).End(xlUp).Row 

For i = 1 To Lastrow 
    If ws1.Cells(i, 11).Value = "" Then 
     ws1.Cells(i, 11).Value = "Sem Substituto" 
    End If 
Next i 

End Sub 
1

你的变量拉斯特罗是一个很长的,因此你不需要使用Set。只是这样做:

Lastrow = ws1.Range("A" & rows.Count).End(xlUp).Row