因此,我尝试将工作表2中的数据粘贴到工作表1中的数据下,如果它在A列中包含“新建”一词。
我有此代码:
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
但是它一直从上至下粘贴工作表1中的数据,而不是粘贴在下面的下一个可用空白处。
任何帮助将不胜感激!
答案 0 :(得分:3)
因为您每次都从1迭代x
。要粘贴到下一个可用行中,您需要计算工作表1的最后一行,类似于对bottomL
的计算。
x = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
答案 1 :(得分:1)
您需要将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