[英]Copy and Paste as Value when any cell is clicked within a range of cells
[英]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
我已经在这个论坛上爬行了很长时间,但是由于通常已经提出并回答了问题,所以还没有发表评论! 我是一个完整的新手,所以我的代码看起来很糟糕。
标题是我要做的基础。 其他问题使我能够找出前三点,但我无法循环。
循环回到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`
尝试:
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
这应该使您能够构建代码
我已经复制了您的代码,并陷入了我想找的循环中,还有一些小改动。
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.