【问题标题】:How to copy paste data based on row value with conditions如何根据带有条件的行值复制粘贴数据
【发布时间】:2019-12-06 23:16:25
【问题描述】:

我目前有一个如下所示的数据集:

         A     B     C     D     E     F     G 
     1   x     x     x     x     x     x     *
     2   a     a     a     a     a     a     
     3   c     c     c     c     c     c     %

我需要代码来根据column 中是否有文本复制数据集底部的粘贴行。然后我需要column G 中的文本出现在column F 中,而行中的其他所有内容保持不变。例如,结果将是:

        A     B     C     D     E     F     G
    1   x     x     x     x     x     x     *
    2   a     a     a     a     a     a     
    3   c     c     c     c     c     c     %
    4   x     x     x     x     x     *
    5   c     c     c     c     c     %

我的代码目前如下所示:

Public Sub CopyRows()
     Sheets("Exposure Distribution").Select
     ' Find the last row of data
     FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
     ' Loop through each row
     For x = 2 To FinalRow
        ' Decide if to copy based on column H
        ThisValue = Cells(x, 8).Value
        If Application.WorksheetFunction.IsText(ThisValue) = "True" Then
            Cells(x, 1).Resize(1, 33).Copy
            Sheets("Exposure Distribution").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Exposure Distribution").Select
        End If
        ThisValue = Cells(x, 9).Value
        If Application.WorksheetFunction.IsText(ThisValue) = "True" Then
            Cells(x, 1).Resize(1, 33).Copy
            Sheets("Exposure Distribution").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Exposure Distribution").Select
        End If
       ThisValue = Cells(x, 10).Value
        If Application.WorksheetFunction.IsText(ThisValue) = "True" Then
            Cells(x, 1).Resize(1, 33).Copy
            Sheets("Exposure Distribution").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            ActiveSheet.Paste
            Sheets("Exposure Distribution").Select
        End If
     Next x
End Sub


However I don't know how to accomplish the final part of what I'm looking for, which is moving data from column G to column F based on if there's text in `column G`.

【问题讨论】:

  • StackOverflow 不是免费的编码服务,如果您可以生成代码的最小可重现示例,那将是一个好的开始。因为这太广泛了,不太可能得到太多积极的关注。stackoverflow.com/help/how-to-ask

标签: excel vba


【解决方案1】:

看看这是否有帮助:

Sub CopyPasteWithConditions()

Dim wb As Workbook: Set wb = ActiveWorkbook 'declare and set the workbook
Dim ws As Worksheet: Set ws = wb.Sheets("SheetNameHere") 'declare and set the worksheet

Dim lRow As Long: lRow = ws.Cells(Rows.Count, "A").End(xlUp).Row 'get the last row of current data
Dim cntTxts As Long: cntTxts = WorksheetFunction.CountA(Range("G1:G" & lRow)) 'get the number of times there is any text in G


Dim arrData As Variant: arrData = ws.Range("A1:G" & lRow + cntTxts) 'create an array of current data + number of rows required for the copied data

Dim R As Long, C As Long, X As Long
For R = LBound(arrData) To lRow 'for each row in current data
    If arrData(R, 7) <> "" Then 'if there is any text in G
        X = X + 1
        For C = LBound(arrData, 2) To UBound(arrData, 2) - 1 'for each column in data, except last
            If C = 6 Then 'if we are on the last column, get the extra text instead
                arrData(lRow + X, C) = arrData(R, 7) 'add the value to the row equal to last row + value of X (pretty much the next free row)
            Else 'else the other values
                arrData(lRow + X, C) = arrData(R, C) 'add the value to the row equal to last row + value of X (pretty much the next free row)
            End If
        Next C
    End If
Next R

ws.Range("A1:G" & lRow + cntTxts) = arrData 'put the data back on the sheet

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 2015-07-17
    • 2020-11-24
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多