使用VBA根据父工作表中的条件将数据附加到相关工作表

时间:2019-04-11 12:00:21

标签: excel vba

根据条件,将数据从一张表追加到相关表(如果尚不存在)

我试图根据工作表1中B列的值(通过/失败),将工作表“导出”中的A列中的数据追加到工作表“通过”中的A列或工作表“失败”中的A列中

如果没有找到数据,我有附加数据的代码,但是它会附加到所有工作表中,而完全忽略了过滤器。

set[`assistant.$.profile.address.${field.key}`]
set[`assistant.$.profile.${field.key}`]

1 个答案:

答案 0 :(得分:0)

修改以下代码,然后尝试:

Option Explicit

Sub Test()

    Dim f As Range
    Dim ws1 As Worksheet, ws3 As Worksheet
    Dim c As Long '<- Change c as long NOT range

    Set ws1 = Worksheets(1) '<- Note: Here you refer to sheet with index number 1, NOT in sheet called Sheet1
    Set ws3 = Worksheets(3) '<- Note: Here you refer to sheet with index number 3, NOT in sheet called Sheet3

    For c = 1 To ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row '<- Loop row by row, starting from 1 to last row of columnA

        If ws1.Range("B" & c).Value = "Calendar" Then

            Set f = ws3.Range(ws3.Range("A1"), _
                               ws3.Cells(Rows.Count, 1).End(xlUp)).Find( _
                                          What:=ws1.Range("B" & c).Value, lookat:=xlWhole) '<- Change What

                If f Is Nothing Then

                    ws3.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Resize(1, 3).Value = ws1.Range("B" & c)

                End If
        End If
    Next c

End Sub