简体   繁体   English

1.使用单元格值查找范围内的值,2.复制并在右边添加3个单元格,然后3.将其粘贴到另一个范围内4.循环

[英]1. Use cell value to find value in range, 2. copy it & 3 cells to the right then 3. paste it in another range 4. loop

I've been creeping this forum for ages but haven't yet commented as normally the questions have been asked and answered! 我已经在这个论坛上爬行了很长时间,但是由于通常已经提出并回答了问题,所以还没有发表评论! I'm a complete newbie so my code will look awful. 我是一个完整的新手,所以我的代码看起来很糟糕。

The title is the basis of what I want to do. 标题是我要做的基础。 Other questions have allowed me to figure out the first 3. points but I'm unable to loop it. 其他问题使我能够找出前三点,但我无法循环。

  1. Cell E4:E (last row with data in) is the value I am using to find 单元格E4:E(包含数据的最后一行)是我要查找的值
  2. In N4:N (Last row with data in). 在N4:N中(最后一行输入数据)。 Once found, select and copy that cell and the next three cells to the right. 找到后,选择并复制该单元格以及右边的下三个单元格。
  3. Paste these in H:K (last row with data in). 将它们粘贴到H:K(最后一行中有数据)。
  4. Loop back to 1. but lower the row by 1 ie E5 and continue 2. & 3. until there is no data in the row of E:E. 循环回到1.,但将行降低1,即E5,然后继续2.和3.,直到E:E行中没有数据。

     Sub Copy_cells() ' Copy_cells Macro Dim FindString As String Dim Rng As Range Dim Rng1 As Range Dim lastrow As Long FindString = ActiveSheet.Range("E4").Value lastrow = ActiveSheet.Range("H65536").End(xlUp).Row + 1 If Trim(FindString) <> "" Then With ActiveSheet.Range("N:N") Set Rng = .Find(What:=FindString, _ After:=.Cells(.Cells.Count), _ LookIn:=xlValues, _ Lookat:=xlWhole, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False) If Not Rng Is Nothing Then Application.Goto Rng, True ActiveCell.Resize(1, 4).Copy Destination:=ActiveSheet.Range("H" & lastrow) Else MsgBox "Nothing found" End If End With End If End Sub` 

Try: 尝试:

Sub test()

Dim FindString As String
Dim Rng As Range
Dim Rng1 As Range
Dim lastrow As Long


lastrow_E = ActiveSheet.Range("E65536").End(xlUp).Row 'Set last row of E
First_E = 2 'First row in E
ActiveSheet.Range("$E$" & First_E & ":$E$" & lastrow_E).RemoveDuplicates Columns:=1, Header:=xlNo 'Remove duplicates from E

lastrow_E = ActiveSheet.Range("E65536").End(xlUp).Row 'reSet last row of E

lastrow = ActiveSheet.Range("H65536").End(xlUp).Row + 1 'Set last row of H
lastrow_checker = lastrow

lastrow_N = ActiveSheet.Range("N65536").End(xlUp).Row + 1 'Set last row of N

For Each c In Range("E" & First_E & ":E" & lastrow_E) 'loop E
    Found = 0
    FindString = ActiveSheet.Range(c.Address).Value 'Set findstring
    If Trim(FindString) <> "" Then 'make sure not blank
        For Each cell In Range("N1:N" & lastrow_N) 'loop N
            If cell.Value = FindString Then 'if cell = findstring
                Found_H = 0
                For Each ce In Range("H1:H" & lastrow) 'loop H if found
                    If ce.Value = FindString Then 'search for findstring in current list
                        Found_H = 1 'found set variable
                        Exit For
                    End If
                Next
                If Found_H = 0 Then 'if not found put it in
                    Range(cell.Address).Resize(1, 4).Copy Destination:=Range("H" & lastrow) 'resize, copy and paste
                    lastrow = lastrow + 1 'Increment lastrow for next value
                    Found = 1
                End If
                Found_H = 0
            End If
        Next
        If Found = 0 Then
            lastrow = lastrow + 1 'leave a blank row
        End If
    End If
Next

If lastrow = lastrow_checker Then
    MsgBox "Nothing Found"
End If
End Sub

This should get you on your way to be able to build your code 这应该使您能够构建代码

I've copied your code and stuck in the loop I think you're after, plus a couple of minor changes. 我已经复制了您的代码,并陷入了我想找的循环中,还有一些小改动。

Sub Copy_cells()
' Copy_cells Macro

    Dim FindString As String
    Dim Rng As Range
    Dim Rng1 As Range
    Dim lastrow As Long
    Dim startrow As Long
    Dim intCount As Long

    startrow = 4
    lastrow = ActiveSheet.Range("H65536").End(xlUp).Row + 1

    For intCount = startrow To lastrow

        FindString = ActiveSheet.Range("E" & intCount).Value

        If Trim(FindString) <> "" Then
            With ActiveSheet.Range("N:N")
                Set Rng = .Find(What:=FindString, _
                                After:=.Cells(.Cells.Count), _
                                LookIn:=xlValues, _
                                Lookat:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)

                If Not Rng Is Nothing Then
                    Application.Goto Rng, True
                    ActiveCell.Resize(1, 4).Copy Destination:=ActiveSheet.Range("H" & lastrow)
                    'I'm not quite sure where you want to paste this back to??
                    'lastrow doesn't seem correct.. maybe use intCount?

                    'also this code probably works but could probably be rewritten as:
                    'Rng.Resize(1, 4).Copy Destination:=ActiveSheet.Range("H" & lastrow)
                Else
                    MsgBox "Nothing found"
                End If

            End With
        End If
    Next intCount

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在单元格区域中单击任何单元格时,将其复制并粘贴为值 - Copy and Paste as Value when any cell is clicked within a range of cells 将单元格值复制到一系列单元格 - Copy cell value to a range of cells 如何通过匹配单元格值将范围复制并粘贴到另一个工作表 - How to copy and paste a range to another worksheet by matching a cell value 查找具有值,偏移和复制范围的单元格,然后粘贴基础数据的日期,然后循环到findnext - Find cell with value, offset and copy range then paste basing data's date, then loop to findnext 如果某个范围中的单元格的值为“ x”,则将某个范围的单元格复制并粘贴到另一张工作表中 - If cell in a range has value “x” copy and paste a range of cells into a different sheet 宏以基于单元格值复制范围和粘贴 - Macro to Copy Range and Paste Based on Cell Value 根据单元格值复制/粘贴列范围 - Copy/Paste column range based on cell value 如果区域中只有一个单元格具有值,则将单元格区域中的值复制到另一个单元格 - Copy the value in a range of cells to another cell if only one cell in the range has a value 如果范围内只有一个单元格有值,如何将单元格范围内的值复制到另一个单元格? - How to copy the value in a range of cells to another cell if only one cell in the range has a value? 如果一个单元格等于条件,则查找范围内的单元格将其值复制到另一列 - Find cells within a range if one cell equals a criteria copy its value to another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM