循环通过ListObject

时间:2018-06-06 01:03:23

标签: vba datetime listobject

我无法使用ListObjects遍历表格。

我有一个嵌套在For循环中的For循环嵌套在For Each循环中。我试图根据相邻列中的值向表中添加数据。我已经抛出一些Debug.print语句来确定整个嵌套是否有效但我无法使最后一个For循环运行。 " Works2的"不会打印。 VBA显示所有变量的数据,我没有收到任何错误。

Public Sub StockDays()
Dim wb As Workbook
Dim ws As Worksheet
Dim loc As Range
Dim tbl As ListObject
Dim quarter, i, countrows As Integer

Set wb = Workbooks("DISTRIBUTOR REWORK DRAFT.xlsm")
Set ws = wb.ActiveSheet
Set loc = ws.Range("A1:C50").Find("Days of Stock") 'I use this further in the program

countrows = ws.ListObjects(1).Range.Rows.count
quarter = DatePart("q", Date)

For Each tbl In ws.ListObjects
    Debug.Print "Works"
    If Mid(tbl.Name, 5, 1) = quarter Then
        For i = 2 To countrows
            Debug.Print "Works2"
            ws.ListObjects("DFCREDPAR").DataBodyRange.Cells(i, ws.ListObjects("DFCREDPAR").ListColumns("Days of Stock").Index) = ws.ListObjects("DFCREDPAR").Range.Cells(i, 2) / tbl.Range.Cells(i, 3)
        Next i
    End If
Next tbl

For Each tbl In ws.ListObjects: Debug.Print tbl.Name & vbTab & tbl.HeaderRowRange.Address & vbTab & tbl.DataBodyRange.Address: Next

End Sub

我已检查过所有数据类型并认为它们都是正确的(无类型不匹配)。

非常感谢任何帮助!

0 个答案:

没有答案