VBA嵌套表格数据中的循环

时间:2016-01-26 22:00:45

标签: excel vba excel-vba

我有一个非常彻底的搜索,但我仍然在努力解决这个问题。基本上,我有一个各种标题的列表,每个标题有10个变量对应,可能有也可能没有数据点。

Table

我想循环遍历第一列,每行都有一个嵌套循环来计算并记录每行中填充数据点的数量。大多数情况下,我不确定如何在第二个循环中引用单元格。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我真的不明白你的最终目标,但我希望下面的代码可以帮助你找到正确的方向。

据我所知,我编写了一个代码,用于计算每行有多少个单元格的数据。

我不确定它是否是您想要的但是让我知道,我会根据您的要求编辑我的代码。

选项明确

Sub test()

Dim wb As Workbook
Dim ws As Worksheet
Dim Lastrow As Long
Dim i As Long, j As Long, c As Long

Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1") '  Change the name of your worksheet

Lastrow = ws.Cells(Rows.Count, 1).End(xlUp).Row ' Find the las row

With ws

        For i = 1 To Lastrow 'Start at row 1 until the last row

            c = 0

                For j = 2 To 11 ' 10 Variables (until the column "L")
                        If Not IsEmpty(.Cells(i, j)) Then c = c + 1  ' Count and record the number of populated data points in each columns
                Next j

            .Cells(i, 12).Value = c 'Past the result in column "L"

        Next i

    End With

End Sub