我对VBA比较陌生,正在慢慢学习。
我有一个非常基本的宏,它将在我的第一个工作表中的PASS
列中查找单词C
,然后将整行复制到辅助工作表中,该工作表也称为{{1} }。
我正在尝试仅从列PASS
复制并粘贴该行中的数据。以下是我目前的代码。我尝试添加A:E
,但它什么也没做。
有关如何将此内容仅复制到该特定行的.range("A:E")
列中的信息的任何帮助,我们将不胜感激。
A-E
答案 0 :(得分:1)
我已经调整了您的代码并更改了以下内容:
Range
变量以使代码更简单Target
单元格为我们提供行PASS
工作表上找到目标行
Target
行但A
列到E
以下是代码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngSource As Range
Dim lngTargetRow As Long
Dim rngTarget As Range
'was change in column C ?
If Intersect(Target, Me.Range("C:C")) Is Nothing Then
Exit Sub
End If
'change was in column C - was PASS entered
If Target.Text = "PASS" Then
'get row to copy to on PASS sheet
lngTargetRow = Worksheets("PASS").Cells(Rows.Count, "C").End(xlUp).Row + 1
'get source range
Set rngSource = Me.Range("A" & Target.Row & ":E" & Target.Row)
'set target range
Set rngTarget = Worksheets("PASS").Cells(lngTargetRow, 1)
'do the copy
rngSource.Copy Destination:=rngTarget
End If
End Sub
答案 1 :(得分:0)
试试这个
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
If Intersect(Target, Me.Range("C:C")) Is Nothing Then Exit Sub
For Each C In Intersect(Target, Me.Range("C:C")).Cells
If C.Text = "PASS" Then
cells(C.Row,1).resize(,5).Copy Worksheets("PASS").Cells(Rows.Count, "C").End(xlUp).Offset(1).EntireRow
End If
Next
End Sub