VBA代码,用于根据单元格数据创建新行

时间:2016-03-03 15:28:16

标签: vba excel-vba excel

我想要实现的是让Excel在行E中搜索“GR”的每个单元格。如果找到GR,将在其下创建一个新行,前一行的数据将被复制到新行中,找到数据的单元格将更改为G,新行的单元格将更改为R. / p>

例如:

当前数据集

 Friday B Fr GR
 Friday B Fr R
 Friday B Fr GR
 Friday B Fr G
 Friday B Fr G

代码运行后

 Friday B Fr G
 Friday B Fr R
 Friday B Fr R
 Friday B Fr G
 Friday B Fr R
 Friday B Fr G
 Friday B Fr G

基本上只是创建一个新行,复制行数据并拆分GR。

Sub Method2()
    Dim rng1 As Range
    Dim strSearch As String
    strSearch = "GR"
    Set rng1 = Range("E:E").Find(strSearch, , xlValues, xlWhole)

For Each Cell In rng1
        With strSearch = True
        Active.Cell Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    'At this point I can't figure out how to put in the letters

        End With

End Sub

1 个答案:

答案 0 :(得分:0)

有一些代码应该为你完成这项工作

Sub SplitColE()
Dim cel As Range
For Each cel In Range("E:E") 'You can change the range to suit your data range
    If cel.Value = "GR" Then
        Rows(cel.Offset(1, 0).Row).Insert
        Rows(cel.Row).Copy Destination:=Rows(cel.Offset(1, 0).Row)
        cel.Value = "G"
        cel.Offset(1, 0) = "R"
    End If
Next
End Sub