[英]Search, Copy, Insert row, Paste and Change Value
我試圖找到一個值“ PLGDY”,從該行復制數據,在找到的行上方插入新行,將數據粘貼到新行,將值“ PLGDY”替換為“ PLGDN”。
我編寫了一個宏,而不是將數據復制到新行中,而是將其粘貼到右側的單元格中。 還將兩行的值都更改為“ PLGDN”。
我想使用For Next循環,因為我需要更改很多值。 是否可以檢查要更改的值? 我想用這個數字作為計數器。
Sub Find_and_Change()
'
'Find a "PLGDY" and set an active cell
Cells.Find(What:="PLGDY", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
'select a block of data in a row
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
Range(Selection, Selection.End(xlToLeft)).Select
'copy selected block of data
Selection.Copy
'insert a row above active cell
ActiveSheet.Cells(ActiveCell.Row, 1).Select
ActiveCell.EntireRow.Insert
'set an active cell at the beginig of a row and move into column A
ActiveSheet.Cells(ActiveCell.Row, 1).Select
' paste copied data into this cell
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
'Now I select whole row
ActiveCell.EntireRow.Select
'I need to replace PLGDY with PLGDN in this row
Selection.Replace What:="PLGDY", Replacement:="PLGDN", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'I need to move active cell 10 columns right and one row down because I want to find next PLGDY
ActiveCell.Offset(1, 10).Select
End Sub
mrbungle的答案就在眼前! 效果很好。
對於發現此問題並打算使用此代碼復制具有多個值的行的任何人,都需要進行一項調整。 我能夠復制/粘貼循環並更改變量以適合,只有例外是我需要在ActiveCell.EntireRow.Select
之后添加ActiveCell.EntireRow.Insert
否則,當第二個循環以新值通過時,將其替換為原始值值以及新行均未選中。 我更新的代碼是:
Private Sub LT2V()
Dim vCount As Integer
'Add Lesser tier of 2V
vCount = Application.WorksheetFunction.CountIf(Range("D:D"), "2V")
Do Until vCount = 0
Cells.Find(What:="2V", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert
ActiveCell.EntireRow.Select
Selection.Replace What:="2V", Replacement:="1V", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Offset(1, 10).Select
vCount = vCount - 1
Loop
End Sub
您是新手,我了解您可能不知道所有快捷鍵和內置函數。 我一直都在學習新知識。 在這種情況下,我使用了內置的工作表函數CountIf
來獲取值出現的次數。 然后循環遍歷,我喜歡使用Do Until Loop
並在每個循環中減去1,直到達到0。
Sub Find_and_Change()
vCount = Application.WorksheetFunction.CountIf(Range("A1:Z100"), "PLGDY")
Do Until vCount = 0
Cells.Find(What:="PLGDY", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
ActiveCell.EntireRow.Copy
ActiveCell.EntireRow.Insert
Selection.Replace What:="PLGDY", Replacement:="PLGDN", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
ActiveCell.Offset(1, 10).Select
vCount = vCount - 1
Loop
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.