想要使用VBA在另一个工作表中创建动态条目表

时间:2018-04-14 16:17:10

标签: excel vba excel-vba

我有一项任务是根据使用VBA和ActiveX Button的条件,使用一张表中的值来创建表格。

This are table entries from which I need to pick values based on condition Cell(J, 62).values = "Not Compliant" or "Partially Compliant".

为了更好地理解它,在J列中,对于行的值是“不合规或部分合规”我希望某些细节作为动态条目包含在新工作表中。下面是我想要创建的新表的格式。我在新表中提到了我想要的列名。

C列|序列号(基于值的条目数)| L栏| M栏

值的数量可能取决于不合规数和部分合规数。

任何人都可以帮我获取此代码。我无法为这种情况编写逻辑。

1 个答案:

答案 0 :(得分:0)

假设您的工作簿中有2张表,其中第一张表是您的表,第二张表是数据不符合的项目的目的地:

Option Explicit
Sub RunComplianceCheck()

Dim c As Range 'Each cell that the loop will look at to determine Compliance Status
Dim rng As Range 'The range of all the entries in the table
Dim totNC 'The total Non or Partially Compliance entries on the second tab

Sheet2.Activate 
Sheet2.Range(Cells(2, 1), Cells(2, 1).SpecialCells(xlLastCell)).ClearContents 'Clear all previous data

Sheet1.Activate
With Sheet1
    Set rng = .Range(Cells(2, 10), _
    Cells(Application.WorksheetFunction.CountA(.Range("J:J")), 10)) 'Set the range of all the entries in the table
End With

For Each c In rng 'Loop through each entry
    If c.Value = "Not Compliant" Or c.Value = "Partially Compliant" Then 'If Not Compliant or Partially Complaint execute the below
        With Sheet2
            totNC = Application.WorksheetFunction.CountA(.Range("A:A")) + 1 'Get the first empty row on the second Sheet of Non or Partially Compliant data
            .Cells(totNC, 1).Value = c.Offset(0, -7).Value 'Get data in column C
            .Cells(totNC, 2).Value = c.Offset(0, -9).Value 'Get data in column A
            .Cells(totNC, 3).Value = c.Offset(0, 2).Value 'Get data in column L
            .Cells(totNC, 4).Value = c.Offset(0, 3).Value 'Get data in column M
        End With
    End If
Next c 'Next cell

Sheet2.Activate

End Sub

此子网将循环遍历数据集的每一行并查找标记为“不合规”或“部分合规”的条目,然后将获取每个项目的问题,序列号,查找/差距说明和查找评级数据。一个(根据要求)并将它们放在第二张纸上。