为什么删除使用变量的listrows不起作用?

时间:2016-04-23 00:29:30

标签: excel excel-vba vba

我想知道为什么当我使用变量从表中删除给定行时,如果我在括号中放入一个数字,我的代码不起作用。 代码没有做任何特别的事情。它经历一个循环并找到我想要删除的行,然后返回它的编号。

我遇到的问题是: Inventory.ListRows(Talalat).Delete'库存是Listobject而Talalat是长的

我肯定从我想要删除的行的代码中获取正确的数字,并且它运行良好而没有错误消息,但它只是没有做它所拥有的。但是,当我手动将数字插入代码时,它工作得很好......有什么想法吗?

PS:如果你想查看它,整个代码都在下面。

Private Sub CommandButton17_Click()
Dim CounterA As Long
Dim Talalat As Long
Dim ANsWR As Integer
Set invent = Sheets("CORE").ListObjects("Inventory")
If Not ComboBox4.Value = "" And Not ComboBox4.Value = "<Új tárgy>" Then
    Talalat = 0
        For CounterA = 1 To invent.Range.Rows.Count
            If ComboBox4.Value = invent.Range.Cells(CounterA, 1) Then Talalat = CounterA
        Next
        If Talalat > 0 Then
            ANsWR = MsgBox("Biztosan törli a(z) " & invent.Range.Cells(Talalat, 1) & Chr(13) & "nevű tárgyat az adatbázisból?", vbYesNo, "Tuti?")
            If ANsWR = vbNo Then
                Exit Sub
            Else
                Sheets("CORE").Unprotect
                invent.ListRows(Talalat).Delete 'Where the glitch is
            End If
        End If
End If
End Sub

3 个答案:

答案 0 :(得分:1)

您是否确定要为要删除的行获取正确的数字?尝试添加&#34; -1&#34;后:

If ComboBox4.Value = invent.Range.Cells(CounterA, 1) Then Talalat = CounterA

答案 1 :(得分:1)

你的桌子上有标题吗?假设答案是&#34;是&#34;,下面说明了为什么你应该使用.DataBodyRange代替.Range

这是一些测试代码,类似于你的......

Sub myTest()
Dim iLoop As Long
Dim theRow As Long
Dim userAns As Integer
Dim myList As ListObject
    Set myList = Sheets("Sheet5").ListObjects("Table1")
    theRow = 0
    For iLoop = 1 To myList.Range.Rows.Count
        If IsDate(myList.Range.Cells(iLoop, 1)) And myList.Range.Cells(iLoop, 1) > Now() Then
            theRow = iLoop
            Exit For
        End If
    Next iLoop

    If theRow > 0 Then
        userAns = MsgBox("Found " & myList.Range.Cells(theRow, 1) & " on Row " & theRow & ". Should I delete?", vbYesNo)
        If userAns = vbNo Then
            Exit Sub
        Else
            myList.ListRows(theRow).Delete
        End If
    End If
End Sub

针对下表运行此代码,并带有标题,以提供正确的数字。 A列中第一个大于NOW的日期在第18行。

enter image description here

然而,当说&#34;是&#34;在对话框中,我们看到它实际上删除了第19行。

enter image description here

以下代码相同,但myList.Range的所有实例都已更改为myList.DataBodyRange。此外,IsDate(myList.DataBodyRange.Cells(iLoop, 1))被删除,因为循环不再检查标题...

Sub myTest()
Dim iLoop As Long
Dim theRow As Long
Dim userAns As Integer
Dim myList As ListObject
    Set myList = Sheets("Sheet5").ListObjects("Table1")
    theRow = 0
    For iLoop = 1 To myList.DataBodyRange.Rows.Count
        If IsDate(myList.DataBodyRange.Cells(iLoop, 1)) And myList.DataBodyRange.Cells(iLoop, 1) > Now() Then
            theRow = iLoop
            Exit For
        End If
    Next iLoop

    If theRow > 0 Then
        userAns = MsgBox("Found " & myList.DataBodyRange.Cells(theRow, 1) & " on Row " & theRow & ". Should I delete?", vbYesNo)
        If userAns = vbNo Then
            Exit Sub
        Else
            myList.ListRows(theRow).Delete
        End If
    End If
End Sub

当它用于同一个表时,它表示NOW之后的第一个数据在第17行。这是数据的第17行,不包括标题。

enter image description here

单击“是”时,将删除正确的行...

enter image description here

答案 2 :(得分:0)

当ListObject上定义了AutoFilter时,我们鼓励删除ListRows的问题。 我们能够通过在删除行之前删除过滤器来解决这个问题:

myList.AutoFilter.ShowAllData
myList.ListRows(theRow).Delete