我正在尝试使用excel宏执行以下操作。当在工作表1的单元格中输入日期时,我要从该行中提取特定的单元格并将其复制到工作表2的新行中。这是一个示例。
这将是工作表1。
A B C D E
proj1 mary started - fiber
proj2 jack complete 2/7/2014 fiber
proj3 kim started - cat6
proj2 phil complete 2/9/2014 fiber
第二张纸应该看起来像这样,因为其中两个有日期,我只想专门移出A,C和D行中的单元格。
A B C
proj2 complete 2/7/2014
proj2 complete 2/9/2014
通过以下代码,我发现我可以基于单元格中的单词值而不是我想要的特定单词显示整个行,当然我希望它根据输入的日期而不是单词“”触发回复”或“回复”。任何帮助,将不胜感激。
Sub Foo()
Dim i As Long, iMatches As Long
Dim aTokens() As String: aTokens = Split("reply,response", ",")
For Each cell In Sheets("sheet1").Range("D:D")
If (Len(cell.Value) = 0) Then Exit For
For i = 0 To UBound(aTokens)
If InStr(1, cell.Value, aTokens(i), vbTextCompare) Then
iMatches = (iMatches + 1)
Sheets("sheet1").Rows(cell.Row).Copy Sheets("sheet2").Rows(iMatches)
End If
Next
Next
End Sub
这可能会有所帮助。它需要放置在该Worksheet_Change
部分中。
这假设您的数据在“列”A:E
中,Sheet1
并且您想在Sheet2
输入日期后将数据复制到。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nextRow As Long
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If VBA.IsDate(Target) Then
With Worksheets("Sheet2")
nextRow = IIf(VBA.IsEmpty(.Range("A1048576").End(xlUp)), 1, .Range("A1048576").End(xlUp).Row + 1)
.Range("A" & nextRow) = Target.Offset(0, -3)
.Range("B" & nextRow) = Target.Offset(0, -1)
.Range("C" & nextRow) = Target
End With
End If
End If
End Sub
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句