2016-03-03 58 views
0

我想要實現的目標是讓Excel搜索E行中的每個單元格以獲取「GR」。如果它找到GR,將在其下創建一個新行,來自上一行的數據將被複制到新行,並且發現數據的單元格將更改爲G,並且新行的單元格更改爲R.根據單元格數據創建新行的VBA代碼

例如:

當前的數據代碼運行

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 

基本上只是創建一個新的行,複製該行的數據和SP

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

後放棄遺傳資源。

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 
+0

請分享你已經嘗試過的代碼。 –

+0

我在這篇文章中看不到一個問題 –

+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 
+0

這工作完美。謝謝Oligg! –

相關問題