简体   繁体   English

Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸

[英]Excel Macro to copy select cells from a row from one sheet to another based upon a cell value

I'm trying to do the following with an excel macro. 我正在尝试使用excel宏执行以下操作。 When a date is entered into a cell on sheet 1 I want to take specific cells from that row and copy them to a new row on sheet 2. Here is an example. 当在工作表1的单元格中输入日期时,我要从该行中提取特定的单元格并将其复制到工作表2的新行中。这是一个示例。

This would be sheet 1. 这将是工作表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  

Sheet 2 should then look like this since two of them have dates and I only want to bring over specifically the cells from row A,C and D. 第二张纸应该看起来像这样,因为其中两个有日期,我只想专门移出A,C和D行中的单元格。

A         B            C
proj2     complete     2/7/2014
proj2     complete     2/9/2014

With the following code I found I'm able to bring over an entire row based on a word value in a cell but not just the specific ones I want and of course I want it to trigger based on a date being entered not the words "reply" or "response". 通过以下代码,我发现我可以基于单元格中的单词值而不是我想要的特定单词来显示整行,当然我希望它根据输入的日期而不是单词“”触发回复”或“回复”。 Any help would be appreciated. 任何帮助,将不胜感激。

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

This may help. 这可能会有所帮助。 It needs to be placed in the Worksheet_Change section. 它需要放置在Worksheet_Change部分中。

This assumes your data is in Columns A:E in Sheet1 and you want to copy data over to Sheet2 once you have entered a date. 这假定您的数据在Sheet1 A:ESheet1并且您想在输入日期后将数据复制到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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel宏-根据值将单元格从一张纸复制到另一张纸 - Excel Macro - copy cells from one sheet into another based on value Macro Excel可根据单元格匹配将范围单元格从一张纸复制到另一张纸,如果不匹配,则跳过单元格 - Macro Excel to copy range cells from one sheet to another based on cell match and skip cell if no match 如何根据单元格值将行内的范围从一个 Excel 工作表复制到另一个 - How to copy a range within a row from one excel sheet to another based on a cell value Excel-将单元格+宏+ VBA从一张纸复制到另一张纸? - Excel - copy cells+macro+VBA from one sheet to another? 根据另一个单元格中的条件将单元格从一个工作表复制到另一个工作表 - Copy cells from one sheet to another based on criteria in another cell 需要一个宏以根据单元格中的数据将一个工作表中的行中的特定单元格复制到另一工作表中的特定单元格 - Need a macro to copy specific cells from a row in one worksheet to a specific cells in another worksheet base upon a data in the cell 根据单元格内一个段落中的单个单词,将整行从一张纸复制到另一张纸 - Copy an entire row from one sheet to another based upon a single word, within a paragraph, inside a cell Excel基于一个列的值将整个行从一张纸复制到另一张纸 - Excel Copy whole row from a sheet to another sheet based on one column value Excel宏,根据另一个单元格值复制和粘贴多个单元格? - Excel macro, to copy and paste a multiple cells based on another cell value? Excel VBA 根据第二列单元格值将列从一张表复制到另一张表 - Excel VBA to Copy Column from one sheet to another based on a second columns cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM