gpt4 book ai didi

excel - 使用搜索和循环从一张纸复制并粘贴到另一张纸

转载 作者:行者123 更新时间:2023-12-04 22:29:52 25 4
gpt4 key购买 nike

我实际上是在尝试从 sht1 搜索关键字,将整个范围从 sht1 复制到 sht2 并确保 sht2 从 rowp 10 开始粘贴.然后循环应该通过 sht1 搜索相同的关键字,对于 sht2 继续循环到 rowp 11直到循环结束。对于 sht1 来说没问题,但是当它粘贴到 sht2 上时,结果不是循环的,而是以某种方式得到相同的编号。与 sht1 相比的空间。不知道如何解决它。
以下是我的代码。将不胜感激一些建议=)

Sub shipshore()

Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim row As String
Dim rowp As String

row = 2
rowp = 10


Set wb = ThisWorkbook
Set sht1 = wb.Sheets("datasheet")
Set sht2 = wb.Sheets("Sheet1")

Do Until sht1.Range("G" & CStr(row)) = ""

If sht1.Range("F" & CStr(row)) = "abc" Then
Sheets("datasheet").Range("A" & CStr(row) & ":J" & CStr(row)).Copy

Sheets("Sheet1").Range("A" & CStr(rowp) & ":J" & CStr(rowp)).PasteSpecial _
Paste:=xlPasteValues



End If

row = row + 1
rowp = rowp + 1
Loop

End Sub

最佳答案

如果我正确理解您的需求,您应该增加 rowp只有在肯定匹配后才能继续复制 sht2 的先行行之后.试试这个方法:

Sub shipshore()

Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Dim row As String
Dim rowp As String

row = 2
rowp = 10


Set wb = ThisWorkbook
Set sht1 = wb.Sheets("datasheet")
Set sht2 = wb.Sheets("Sheet1")

Do Until sht1.Range("G" & CStr(row)) = ""

If sht1.Range("F" & CStr(row)) = "abc" Then
Sheets("datasheet").Range("A" & CStr(row) & ":J" & CStr(row)).Copy

Sheets("Sheet1").Range("A" & CStr(rowp) & ":J" & CStr(rowp)).PasteSpecial _
Paste:=xlPasteValues
rowp = rowp + 1
End If

row = row + 1

Loop

End Sub

关于excel - 使用搜索和循环从一张纸复制并粘贴到另一张纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com