2016-02-25 85 views
0

我在第28行(如果语句)出现“”错误。我不确定是什么问题,因为我已经正确地定义了一切,并确保路径得到更正。有人可以帮忙吗?我试图从一张纸到另一张纸上为每个用户抓取相应的组。这里是我的代码:对于excel,vbscript未知运行时错误

for each ColValue1 in objWorksheet1.Range("A1:A" & intlastrow1) 
    introw1= introw1+1 
    for each ColValue2 in objWorksheet2.Range("A1:A" & intLastRow2) 
     introw2 = introw2+2 
     if ColValue1 = ColValue2 then 
      ... 
+1

ColValue1和ColValue2声明为什么?如果您将它们声明为范围,请尝试使用ColValue1.Value = ColValue2.Value,然后使用 – MatthewD

+0

VBScript或VBA?我认为它是VBA,VBS是不同的。另外,错误是什么意思? – vacip

+2

您无法比较对象,只能对这些对象的属性进行比较。如果这些是范围,请尝试比较它们的.Value或.Address属性,具体取决于您的需要。 – vacip

回答

1

您正在循环的单元应该被声明为一个范围。然后比较它们的值。

Dim ws As Excel.Worksheet 
Set ws = ActiveWorkbook.Sheets("Sheet1") 

Dim ColValue1 As Range 
Dim ColValue2 As Range 

Set ColValue1 = ws.Range("A1") 
Set ColValue2 = ws.Range("A2") 

If ColValue1.Value = ColValue2.Value Then 
    MsgBox "They are the same" 
Else 
    MsgBox "They are not the same" 
End If