简体   繁体   English

搜索,复制,插入行,粘贴和更改值

[英]Search, Copy, Insert row, Paste and Change Value

I am trying to find a value "PLGDY", copy data from this row, insert a new row above the one found, paste the data into new row, replace value "PLGDY" with "PLGDN". 我试图找到一个值“ PLGDY”,从该行复制数据,在找到的行上方插入新行,将数据粘贴到新行,将值“ PLGDY”替换为“ PLGDN”。

I wrote a macro that instead of copying data into new row it pastes into cells to the right. 我编写了一个宏,而不是将数据复制到新行中,而是将其粘贴到右侧的单元格中。 It also changes values in both rows to "PLGDN". 还将两行的值都更改为“ PLGDN”。

I would like to use For Next loop, because I have plenty of values to change. 我想使用For Next循环,因为我需要更改很多值。 Is it possible to check how many values to change? 是否可以检查要更改的值? I would like to use this number as counter. 我想用这个数字作为计数器。

 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's Answer is spot on! mrbungle的答案就在眼前! Works great. 效果很好。

For anyone finding this and intends to use this code to duplicate rows with multiple values, there is one tweak to be made. 对于发现此问题并打算使用此代码复制具有多个值的行的任何人,都需要进行一项调整。 I was able to copy/paste the loop and change the variables to suit, only exception was I needed to add ActiveCell.EntireRow.Select after the ActiveCell.EntireRow.Insert otherwise when the second loop came through with the new values it replaced the original value as well as the new row was not selected. 我能够复制/粘贴循环并更改变量以适合,只有例外是我需要在ActiveCell.EntireRow.Select之后添加ActiveCell.EntireRow.Insert否则,当第二个循环以新值通过时,将其替换为原始值值以及新行均未选中。 Updated code for my purposes is: 我更新的代码是:

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

You being new I understand you might not know all the shortcuts and built in functions. 您是新手,我了解您可能不知道所有快捷键和内置函数。 I still learn new ones all the time. 我一直都在学习新知识。 In this case I used the built in worksheet function CountIf to get the number of times the values appear. 在这种情况下,我使用了内置的工作表函数CountIf来获取值出现的次数。 Then to loop through I like to use Do Until Loop and just subtract 1 through each loop until I reach 0. 然后循环遍历,我喜欢使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM