简体   繁体   English

按行将包含文本的单元格复制到另一个工作表上的列

[英]Copy cells by row containing text to column on another worksheet

I'm fairly new to macros etc..and I've been trying to figure this problem out for a few days now! 我对宏等还不是很陌生,而且我已经尝试解决这一问题了几天了!
I'm trying to go from a large spreadsheet of data, selecting specific cells based on the contents of specific cells, and paste into another worksheet. 我正在尝试从大型数据电子表格开始,根据特定单元格的内容选择特定单元格,然后粘贴到另一个工作表中。

Source spreadsheet: 源电子表格:

Columns go: Site, Sub-location, Date, Month, Inspector, Action 1, Action 2 etc up to a max of 67 actions for each inspection. 列为:站点,子位置,日期,月份,检查员,操作1,操作2等,每次检查最多67个操作。 Each row is a separate inspection submission 每行是单独的检查提交

Target spreadsheet: 目标电子表格:

Columns go: Site, Sub-location, Date, Month, Inspector, Action, Due date of Action where each row is a separate action. 列为:站点,子位置,日期,月份,检查员,操作,操作的到期日期,其中每一行都是单独的操作。 I want it to skip pasting any values from the actions columns that would be blank (since no action is required). 我希望它跳过从动作列中粘贴为空的任何值(因为不需要任何动作)。 When it pastes the actions, it will also paste the first 5 columns (with site name, location, date etc), so that the action can be identified to the right site, date etc. 粘贴操作时,还将粘贴前5列(带有网站名称,位置,日期等),以便可以将操作标识到正确的网站,日期等。

Hopefully that makes sense. 希望这是有道理的。 By the end, I want the target spreadsheet to be able to be filtered by whatever the people need, eg by due date, or by location etc. 最后,我希望目标电子表格能够根据人们的需要进行过滤,例如按到期日或按地点等。

Code that I tried my hardest to get working...Unfortunately I can only get it working for the first row, and then it still pastes the blank (or zero) values and I need to filter them out. 我竭尽全力地工作的代码...不幸的是,我只能使它在第一行工作,然后仍然粘贴空白(或零)值,我需要过滤掉它们。 I'm thinking some sort of loop to do all the rows. 我在想某种循环来完成所有行。

Sub test1257pm()
Application.ScreenUpdating = False
    Sheets("Corrective Actions").Select
    Range("A3:E3").Select
    Selection.Copy
    Sheets("Corrective Actions Tracker").Select
    Range("A3").Select
    ActiveSheet.Paste

    Sheets("Corrective Actions").Select
    Range("F3").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Sheets("Corrective Actions Tracker").Select
    Range("F3").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=True

.Cells(Rows.Count, "F").End(xlUp).Offset(1, 0).PasteSpecial
    Rows("2:2").Select
    Selection.AutoFilter
    Range("F4").Select
    ActiveSheet.Range("$A$2:$L$300").AutoFilter Field:=6, Criteria1:=Array( _
        "CMC to conduct clean of ceiling fans. Close out by 17/04/2014", _
        "Provide bins", "Send to contractor", "="), Operator:=xlFilterValues

Application.ScreenUpdating = True
End Sub

Many thanks to anyone that can give me any assistance! 非常感谢任何可以给我任何帮助的人! :) :)

Edit:24-4-2014 Okay so after L42's code, it works fine if I could just consodidate my data first before putting it in the 1 column (stacking). 编辑:24-4-2014好吧,所以在L42的代码之后,如果我可以先将我的数据放在第1列中(堆叠),那么它可以很好地工作。 The code I tried (using Macro recorder) is: 我尝试(使用宏记录器)的代码是:

Sub Macro2()

Dim r As Range
Dim i As Integer

For i = 3 To 10

Range("P" & i).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

Range("F" & i).Select
ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=True, _
    IconFileName:=False

   Next i

End Sub

My problem with this is that it gives unexpected results...it doesn't consolidate it all into rows how I would expect. 我的问题是,它给出了意外的结果……它并没有像我期望的那样将所有内容合并到行中。 I'm thinking that this isn't the best solution...and probably the original macro needs to be changed..however I'm not sure how. 我在想这不是最好的解决方案……可能需要更改原始宏。但是我不确定如何。

Overhaul #1: Using the provided sample data 大修#1:使用提供的样本数据

Option Explicit '~~> These two lines are important
Option Base 1

Sub StackMyActions()

Dim sourceWS As Worksheet, targetWS As Worksheet
Dim staticRng As Range, copyRng As Range
Dim inspCnt As Long, i As Long, fRow As Long, tRow As Long
Dim myactions

Set sourceWS = ThisWorkbook.Sheets("Corrective Actions")
Set targetWS = ThisWorkbook.Sheets("Corrective Actions Tracker")

With sourceWS
    '~~> count the total inspection
    '~~> here we incorporate .Find method finding the last cell not equal to 0
    inspCnt = .Range("A3", .Range("A:A").Find(0, [a2], _
        xlValues, xlWhole).Offset(-1, 0).Address).Rows.Count
    '~~> set the Ranges
    Set copyRng = .Range("F3:BT3")
    Set staticRng = .Range("A3:E3")
    '~~> loop through the ranges
    For i = 0 To inspCnt - 1
        '~~> here we use the additional code we have below
        '~~> which is GetCARng Function
        myactions = GetCARng(copyRng.Offset(i, 0))
        '~~> this line just checks if there is no action
        If Not IsArray(myactions) Then GoTo nextline
        '~~> copy and paste
        With targetWS
            fRow = .Range("F" & .Rows.Count).End(xlUp).Offset(1, 0).Row
            tRow = fRow + UBound(myactions) - 1
            .Range("F" & fRow, "F" & tRow).Value = Application.Transpose(myactions)
            staticRng.Offset(i, 0).Copy
            .Range("A" & fRow, "A" & tRow).PasteSpecial xlPasteValues
        End With
nextline:
    Next
End With

End Sub

Function to get the actions: 获得动作的功能:

Private Function GetCARng(rng As Range) As Variant
Dim cel As Range, x
For Each cel In rng
    If cel.Value <> 0 Then
        If IsArray(x) Then
            ReDim Preserve x(UBound(x) + 1)
        Else
            ReDim x(1)
        End If
        x(UBound(x)) = cel.Value
    End If
Next
GetCARng = x
End Function

Results: 结果:
1: Using your sample data which looks like below: 1:使用如下所示的示例数据:

纠正措施表

2: Which after running the macro stacks the data like below: 2:运行宏后,哪个将按如下所示堆叠数据:

纠正措施跟踪器

Above code only stack inpections with at least 1 Action. 上面的代码仅堆叠带有至少1个Action的检查。
For example, Site 3 which was conducted by MsExample do not reflect on the Corrective Actions Tracker Sheet since no action was posted. 例如,由MsExample进行的站点3不会反映在“纠正措施跟踪表”上,因为没有发布任何措施。
Well I really can't explain it enough, all the properties and methods used above. 好吧,我真的不能对它进行足够的解释,上面使用的所有属性和方法。
Just check out the links below to help you understand most parts: 只需查看以下链接即可帮助您了解大部分内容:

Avoid Using Select 避免使用选择
Using .Find Method 使用.Find方法
Returning Array From VBA Function 从VBA函数返回数组

And of course practice, practice, practice. 当然还有练习,练习,练习。

暂无
暂无

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

相关问题 如果发现文本,则在另一工作表中复制同一行的特定单元格 - Copy specific cells of same row in another worksheet if text found EXCEL-是否有公式将包含特定文本的所有单元格复制到另一列? - EXCEL - is there a formula to copy all the cells containing specific text to another column? 根据不同工作表的 A 列将一个工作表的整行复制到另一个工作表 - Copy entire row of one worksheet to another based on column A of different worksheet 将某些单元格复制到另一个工作表中的下一个空白行 - Copy certain cells to the next blank row in another worksheet 根据列中的日期将行中的特定单元格复制到月工作表 - Copy specific cells in a row to month worksheet based on date in column 根据条件将单元格从特定列复制到另一个工作表 - Copy cells from a specific column to another worksheet based on criteria 如果列A值匹配,则VBA将源工作表中的行中的选择单元格复制到目标工作表中 - VBA Copy select cells from row in source worksheet to target worksheet if column A values match 将包含特定值的单元格复制到另一列,跳过空格 - Copy cells containing specific values into another column skipping blanks 从一行复制特定的单元格,然后粘贴到另一工作表的不同单元格中 - Copy specific cells from one row and paste into different cells on another worksheet 如何将特定的数据单元格(跳过空白单元格)复制到另一个工作表中的下一个空白行中? - how to copy specific cells of data (skipping blank cells) into another worksheet into next blank row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM