关于查找特定单词,插入行和粘贴到所述行中的Excel VBA查询

时间:2017-03-05 01:21:52

标签: excel vba

Sub Worksheet_Change(ByVal Target As Range)

Application.ScreenUpdating = False
If Target.Cells.Count > 1 Then Exit Sub

Dim OutputFile As Workbook
Dim Inputpath As String
Dim Outputpath As String
Dim del1 As Variant

    If Not Intersect(Target, Range("G:G")) Is Nothing Then
        Application.ScreenUpdating = False
        Set InputFile = ActiveWorkbook
        Set OutputFile = Workbooks.Open("\\SHKFS1\Shared\MONAHAN\1st watch files\inst status sheet\STATUSSHEETINSTITUTIONAL.xlsm")
        del1 = Target.Offset(0, -5).Value
        If Target.Validation.Type = 3 Then
            If Target.Value = "" Then
                OutputFile.Sheets("UI").Cells.Find(del1, After:=ActiveCell, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).EntireRow.Delete
            Else
                OutputFile.Sheets("UI").Cells(Rows.Count, 1).End(xlUp)(2).Resize(, 2) = Target.Offset(, -5).Resize(, 2).Value
                OutputFile.Sheets("UI").Cells(Rows.Count, 1).End(xlUp).Offset(, 3) = Target.Value
                OutputFile.Sheets("UI").Cells(Rows.Count, 1).End(xlUp).Offset(, 4).FormulaR1C1 = "Monahan"
            End If
            OutputFile.Close savechanges:=True
        End If
        Application.ScreenUpdating = True
    End If
End Sub

我希望能够找到单词“UI”并在其下方插入一行并将上面的信息(在代码中的“Else”之后)粘贴到插入的那一行中。同样的事情将是“LOP”。

UI

梁,吉姆123456

琼斯,吉姆123456

LOP

戴维斯,贝蒂456789

Crack,Donald 456789

1 个答案:

答案 0 :(得分:0)

你可能在此之后:

        ...
        Else
            Dim f As Range
            With OutputFile.Sheets("UI")
                Set f = .Range("A1", .Cells(.Rows.count, 1).End(xlUp)).Find(what:="UI", lookat:=xlWhole, LookIn:=xlValues)
            End With
            If Not f Is Nothing Then
                Application.EnableEvents = False
                f.Offset(1).EntireRow.Insert
                f.Offset(1).Resize(, 3) = Array(Target.Offset(, -5).Resize(, 2).Value, Target.Value, "Monahan")
                Application.EnableEvents = True
            End If
        End If
        ...