【问题标题】:Code giving Object Block Variable Error给出对象块变量错误的代码
【发布时间】:2016-11-20 08:41:51
【问题描述】:

我有一个带有一个组合框和 2 个文本框和提交按钮的小用户表单。

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

这是我的代码:

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

我在“If rng 0 Then”行中遇到错误。

请查看并建议解决此问题。

谢谢 萨尔曼

【问题讨论】:

  • 缩进你的代码,你有If rng &lt;&gt; 0 ThenElse,你的End If在哪里?
  • 在下面试试我的代码,让我知道它是否可以解决您的错误
  • 感谢它的工作...

标签: excel vba


【解决方案1】:

替换您的行代码:

If rng <> 0 Then

与:

If Not rng Is Nothing Then

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 1970-01-01
    • 2011-07-13
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    相关资源
    最近更新 更多