2016-11-20 54 views
0

我有一个小的用户窗体,有一个组合框和两个文本框并提交按钮。给出对象块变量错误的代码

按下提交按钮时,我得到的对象变量未设置错误。

这里是我的代码:

Private Sub CommandButton1_Click() 

Dim ws As Worksheet, tbl As ListObject, row As ListRow 

Set ws = Sheets("Create Account Heads") 
Set tbl = ws.ListObjects(Me.TextBox2.Value) 

Dim intValueToFind As String, rng As Range 

Set rng = tbl.ListColumns(1).DataBodyRange  
intValueToFind = LCase(Me.TextBox3.Value) 

If rng <> 0 Then 
    For Each rng In rng 
     If LCase(rng.Value) = intValueToFind Then 
      MsgBox ("Account Head with this Name Already Exists.") 
      Exit Sub 
     End If 
    Next rng 
Else 
    'Unprotect the Worksheet 
    ws.Unprotect Password:="google" 
End if 

End Sub 

我得到的错误 “如果RNG <> 0,则” 行。

请仔细阅读并提出建议以解决此问题。

感谢 萨尔曼

+0

缩进你的代码,你有'如果RNG <> 0 Then'和'Else' ,你的'End If'在哪里? –

+0

试试我下面的代码,让我知道它是否工作解决了您的错误 –

+0

感谢它的工作... –

回答

0

替换你的行代码:

If rng <> 0 Then 

有:

If Not rng Is Nothing Then 
相关问题