Excel VBA如果有2条条件,则else起作用

时间:2018-11-08 10:11:42

标签: excel vba

我有2个文件(+工作表名称): 1.生产力(注册TAA) 2.运营商模板TAA(出口注册TAA)

我想编写一个执行以下操作的宏: 在“ registratie TAA”列A中搜索“ Export registratie TAA $ A $ 2”的值 在“ registratie TAA”列D中搜索“ Export registratie TAA $ D $ 2”的值

如果在同一行中找到两个值,则: 将“操作员模板TAA第2行”复制到如上所述的注册TAA中找到的行

如果未在同一行中找到两个值: 在注册TAA的第5行插入新行 将“操作员模板TAA第2行”复制到注册TAA中的新第5行

我复制了以下代码作为示例以及新代码的开头,但是我不知道如何同时搜索两个值。

  Dim WbO As Workbook
    Dim WbW As Workbook
    Dim i As Long
    Dim LRA As Long
    Dim LRB As Long
    Dim RowToCopy As Long
    Dim Rowstr As Long

    Dim Searchstr As Long
    Dim Address As Range
    Dim Searchrng As Range

Dim x As Long
Dim Rowstr1 As Long
Dim searchstr1 As Long
Dim address1 As Range
Dim searchrng1 As Range

    Set WbO = Workbooks("productiegegevens.xlsm") '<= Set workbook to variables
    Set WbW = Workbooks("operator template TAA.xlsm")

    LRA = WbW.Worksheets("export registratie TAA").Range("A" & Rows.Count).End(xlUp).Row '<= Find Lastrow
LRB = WbW.Worksheets("export registratie TAA").Range("D" & Rows.Count).End(xlUp).Row

    For i = 2 To LRA '<= Loop column A (Workbook:operator template TAA)
        Searchstr = WbW.Worksheets("export registratie TAA").Range("A" & i).Value '<= Set what to search for
        Rowstr = i '<= Searchstr row
        Set Searchrng = WbO.Worksheets("registratie TAA").Columns("A") '<= Set where to search for
        Set Address = Searchrng.Find(What:=Searchstr, LookAt:=xlWhole) '<= Result of the search

    If Address Is Nothing Then
        'If what we search for not found
        WbO.Worksheets("registratie TAA").Rows("5").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        WbW.Worksheets("export registratie TAA").Rows(Rowstr).EntireRow.Copy
        WbO.Worksheets("registratie TAA").Rows(5).PasteSpecial Paste:=xlPasteValues
Else
End If
Next i

    For x = 2 To LRB '<= Loop column D (Workbook:operator template TAA)
        searchstr1 = WbW.Worksheets("export registratie TAA").Range("D" & x).Value '<= Set what to search for
        Rowstr1 = x '<= Searchstr1 row
        Set searchrng1 = WbO.Worksheets("registratie TAA").Columns("D") '<= Set where to search for
        Set address1 = Searchrng.Find(What:=searchstr1, LookAt:=xlWhole) '<= Result of the search

If Address Is Nothing Then
        'If what we search for not found
        WbO.Worksheets("registratie TAA").Rows("5").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        WbW.Worksheets("export registratie TAA").Rows(Rowstr).EntireRow.Copy
        WbO.Worksheets("registratie TAA").Rows(5).PasteSpecial Paste:=xlPasteValues

    Else
        'If what we search for found
        RowToCopy = Address.Row '<= Where we find the Searchstr
        WbW.Worksheets("export registratie TAA").Rows(x).EntireRow.Copy
        WbO.Worksheets("registratie TAA").Rows(RowToCopy).PasteSpecial Paste:=xlPasteValues
    End If

    Next x


End Sub

0 个答案:

没有答案