VBA:将条件为TRUE的整行复制到TempSheet2
是否有人可以帮助我使用VBA脚本将条件为TRUE的整行从TempSheet中的U列复制到TempSheet2。此宏检查U列中每行的值“TRUE”。 将U列中具有真值的列复制到另一张图纸VBA:将条件为TRUE的整行复制到TempSheet2,vba,excel,Vba,Excel,是否有人可以帮助我使用VBA脚本将条件为TRUE的整行从TempSheet中的U列复制到TempSheet2。此宏检查U列中每行的值“TRUE”。 将U列中具有真值的列复制到另一张图纸 Option Explicit Sub CopyRow() Dim Row As Integer Dim sRow As String Dim i As Long Application.ScreenUpdating = False i = 1 'To ensure
Option Explicit
Sub CopyRow()
Dim Row As Integer
Dim sRow As String
Dim i As Long
Application.ScreenUpdating = False
i = 1 'To ensure each time the macro is run it starts at row 1
For i = 1 To 1048576 'for each row in the sheet
If Range("U" & i).Value = True Then 'If the U value is true then copy it
Row = i
sRow = CStr(Row) 'convert row number to string
Rows(sRow & ":" & sRow).Select
Selection.Copy
Sheets("Sheet2").Select
Rows(sRow & ":" & sRow).Select
ActiveSheet.Paste
Sheets("Sheet1").Select
Application.CutCopyMode = False
End If
Next i
Range("A1").Select
Application.ScreenUpdating = True
End Sub
我不确定您希望如何触发宏,但可能需要更改工作表
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' trigger the macro if they are changed
Set KeyCells = Range("U:U")
Call CopyRow
End If
End Sub
注意:工作表\u更改进入sheet1的代码,宏进入模块使用类似以下内容:
Sub test()
Dim i&, z&, oCell As Range
Application.ScreenUpdating = 0
z = 1: i = Sheets("TempSheet").Cells(Rows.Count, "U").End(xlUp).Row
For Each oCell In Sheets("TempSheet").Range("U1:U" & i)
If oCell.Value = True Then
oCell.EntireRow.Copy Sheets("TempSheet2").Rows(z)
z = z + 1
End If
Next
Application.ScreenUpdating = 1
End Sub
这个网站上已经回答了数百个这样的问题。请搜索可能的重复行。可能有一种更简洁的方法可以复制行,而不首先激活行。