[英]Pasting data with a specific word from worksheet 2 under data from worksheet 1
So I'm trying to have the data from worksheet 2 pasted under the data from worksheet 1 if it has the word "New" in column A. 因此,我尝试将工作表2中的数据粘贴到工作表1中的数据下,如果它在A列中包含单词“ New”。
I have this code: 我有以下代码:
Sub CopyRows()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim bottomL As Integer
Dim x As Integer
Dim c As Range
Set ws1 = Sheets("Sheet1")
Set ws2 = Sheets("Sheet2")
bottomL = ws2.Range("A" & Rows.Count).End(xlUp).Row: x = 1
For Each c In ws2.Range("A1:A" & bottomL)
If c.Value = "New" Then
c.EntireRow.Copy ws1.Range("A" & x)
x = x + 1
End If
Next c
End Sub
But it keeps pasting over the data from worksheet 1 from the top down instead of pasting in the next available blank spot below. 但是它会一直从上至下粘贴工作表1中的数据,而不是粘贴在下面的下一个可用空白处。
Any help would be appreciated! 任何帮助,将不胜感激!
Because you are iterating x
from 1 every time. 因为您每次都从1迭代
x
。 To paste in the next available row - you need to calculate lastrow of sheet 1 similar to your calculation of bottomL
. 要粘贴在下一个可用行中,您需要计算工作表1的最后一行,类似于您对
bottomL
的计算。
x = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
You need to assign the x
as the last row of the first worksheet and then increment it: 您需要将
x
分配为第一个工作表的最后一行,然后将其递增:
Option Explicit
Sub CopyRows()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim bottomL As Long
Dim x As Long
Dim c As Range
Set ws1 = Worksheets(1)
Set ws2 = Worksheets(2)
bottomL = ws2.Range("A" & Rows.Count).End(xlUp).Row
x = ws1.Range("A" & Rows.Count).End(xlUp).Row
x = x + 1
For Each c In ws2.Range("A1:A" & bottomL)
If c.Value = "New" Then
c.EntireRow.Copy ws1.Range("A" & x)
x = x + 1
End If
Next c
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.