gpt4 book ai didi

excel - 在列中搜索 1 并在找到时将整行粘贴到另一个工作表中?

转载 作者:行者123 更新时间:2023-12-03 00:35:32 25 4
gpt4 key购买 nike

我正在努力解决一些陷入循环的代码。我试图获取代码来复制 BD 列中的值为 1 的任何行,并将整行的值粘贴到另一个工作表中的下一个空行。我使用的代码如下

Sub FindIssues()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
Sheets("Macro Worksheet").Select
If Range("BD" & i).Value = "1" Then Rows(i).Select
Selection.Copy
Sheets("Macro Worksheet 2").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(1, 0).Select
Loop
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Sheets("Macro Worksheet").Select


Next i
End Sub

感谢您的帮助

最佳答案

宏工作表

enter image description here

Option Explicit

Sub CopyEntireRow()
Application.ScreenUpdating = False
Dim src As Worksheet
Set src = Sheets("Macro Worksheet")

Dim trgt As Worksheet
Set trgt = Sheets("Macro Worksheet 2")

Dim i As Long
For i = 1 To src.Range("A" & Rows.Count).End(xlUp).Row
If src.Range("A" & i) = 1 Then
' calling the copy paste procedure
CopyPaste src, i, trgt
End If
Next i
Application.ScreenUpdating = True
End Sub

' this sub copoes and pastes the entire row into a different sheet
' below the last used row
Private Sub CopyPaste(ByRef src As Worksheet, ByVal i As Long, ByRef trgt As Worksheet)
src.Activate
src.Rows(i & ":" & i).Copy
trgt.Activate
Dim nxtRow As Long
nxtRow = trgt.Range("A" & Rows.Count).End(xlUp).Row + 1
trgt.Rows(nxtRow & ":" & nxtRow).PasteSpecial _
Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
End Sub

宏工作表 2

enter image description here

关于excel - 在列中搜索 1 并在找到时将整行粘贴到另一个工作表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19924127/

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