2017-06-19 35 views
0

我写在VBA这下面的代码:VBA错误 - 应用程序定义的公关对象定义的错误当使用如果

Dim Counter As Long 
Dim Counter_Two As Long 
Dim Array_Column_Letter(3) As String 

Array_Column_Letter(0) = "A" 
Array_Column_Letter(1) = "B" 
Array_Column_Letter(2) = "C" 
Array_Column_Letter(0) = "D" 

For Counter = 0 To LastRow 
    For Counter_Two = 0 To 3 
    If Worksheets("Sheet1").Cells(Counter, Array_Column_Letter(Counter_Two)).Value = IsEmpty(True) Then 
     MsgBox ("Hi") 
    End If 
    Next Counter_Two 
Next Counter 

出于某种原因,我得到了以下错误:

Application-defined pr Object-defined Error

VBA说if语句导致这个错误是有问题的。我似乎无法弄清楚为什么。有任何想法吗?

+0

你'计数器= 0到LastRow'从'0'开始,所以'细胞(计数器,Array_Column_Letter(Counter_Two))。Value'从行'0'开始,这将导致与您的错误。你也可以写这行'如果IsEmpty(工作表(“Sheet1”)。Cells(Counter,Array_Column_Letter(Counter_Two))Value)Then' –

+1

只是一个普遍的问题 - 你认为'IsEmpty(True)'做了什么? – Vityata

+0

'IsEmpty(True)'将永远是错误的,所以基本上你正在测试单元格的内容是错误的。可能不是你想要达到的目标。 –

回答

1

范围在Excel中的“A1”而不是“A0”

这条线开始得到这个错误

If Worksheets("Sheet1").Cells(Counter, Array_Column_Letter(Counter_Two)).Value = IsEmpty(True) Then 

应改为

If Worksheets("Sheet1").Cells(Counter + 1 , Array_Column_Letter(Counter_Two)).Value = IsEmpty(True) Then 

而且正确使用的isEmpty你应该使用它,如下

If IsEmpty(Worksheets("Sheet1").Cells(Counter + 1 , Array_Column_Letter(Counter_Two)).Value) Then 
0

你,因为你正在努力寻找行0更改您的第一个起始值为声明

相关问题