将工作表 2 中的特定单词的数据粘贴到工作表 1 的数据下
Pasting data with a specific word from worksheet 2 under data from worksheet 1
所以我试图将工作表 2 中的数据粘贴到工作表 1 中的数据下,如果它在 A 列中有单词 "New"。
我有这个代码:
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 中的数据,而不是粘贴到下面的下一个可用空白点。
如有任何帮助,我们将不胜感激!
您需要将 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
因为你每次都是从 1 开始迭代 x
。要粘贴到下一个可用行 - 您需要计算 sheet 1 的最后一行,类似于 bottomL
.
的计算
x = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
所以我试图将工作表 2 中的数据粘贴到工作表 1 中的数据下,如果它在 A 列中有单词 "New"。
我有这个代码:
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 中的数据,而不是粘贴到下面的下一个可用空白点。
如有任何帮助,我们将不胜感激!
您需要将 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
因为你每次都是从 1 开始迭代 x
。要粘贴到下一个可用行 - 您需要计算 sheet 1 的最后一行,类似于 bottomL
.
x = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1