Excel VBA-根据单元格值在VBA中复制和粘贴循环 [英] Excel VBA - Copy and Paste Loop in VBA based on cell value

查看:179
本文介绍了Excel VBA-根据单元格值在VBA中复制和粘贴循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提出一个宏,该宏检查单元格中是否存在任何数字值.如果存在数字值,请复制该行的一部分并将其粘贴到同一电子表格内的另一个工作表中.

I am trying to come up with a macro that checks if any numeral value exists in a cell. If a numeral value exists, copy a portion of that row and paste it into another worksheet within the same spreadsheet.

Sheet1是其中包含我所有数据的工作表.我试图查看R列中是否有任何值.如果是这样,请复制该单元格及其左侧的四个相邻单元格,然后将其粘贴到Sheet2中.

Sheet1 is the sheet that has all my data in it. I am trying to look in column R if there is any values in it. If it does, copy that cell and the four adjacent cells to the left of it and paste it into Sheet2.

到目前为止,这是我根据杂凑其他人的代码而提出的,尽管它只满足我想要的一部分.它只是复制一行的一部分,然后将其粘贴到另一个工作表中,但不会首先检查R列中的值.它只是复制和粘贴而已,一旦完成就不会移动到下一行.我需要它继续到下一行才能继续查找:

This is what I have come up with so far based on mish-mashing other people's code though it only does a part of what I want. It just copies part of a row then pastes it into another worksheet but it does not check column R for a value first. It just copies and pastes regardless and does not move onto the next row once it has done that. I need it to continue onto the next row to continue looking:

Sub Paste_Value_Test()

Dim c As Range
Dim IRow As Long, lastrow As Long
Dim rSource As Range
Dim wsI As Worksheet, wsO As Worksheet

On Error GoTo Whoa

'~~> Sheet Where values needs to be checked
Set wsI = ThisWorkbook.Sheets("Sheet1")
'~~> Output sheet
Set wsO = ThisWorkbook.Sheets("Sheet2")

Application.ScreenUpdating = False

With wsI
    '~~> Find Last Row which has data in Col O to R
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        lastrow = .Columns("O:R").Find(What:="*", _
                      After:=.Range("O3"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row
    Else
        lastrow = 1
    End If

    '~~> Set you input range
    Set rSource = .Range("R" & lastrow)

    '~~> Search for the cell which has "L" and then copy it across to sheet1
    For Each c In rSource
    Debug.Print cValue
        If c.Value > "0" Then
            .Range("O" & c.Row & ":R" & c.Row).Copy
            wsO.Cells(5 + IRow, 12).PasteSpecial Paste:=xlPasteValues
            IRow = IRow + 1
        End If
    Next
End With

LetsContinue:
Application.ScreenUpdating = True
Application.CutCopyMode = False
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub

推荐答案

下面是一些代码,有望实现我认为的目标.我在整篇文章中都发表了评论,指出了自己的更改:

Below is some code which hopefully achieves what I think you are trying to do. I have included comments throughout stating what I changed:

Sub Paste_Value_Test()

    Dim c As Range
    Dim IRow As Long, lastrow As Long
    Dim rSource As Range
    Dim wsI As Worksheet, wsO As Worksheet

    On Error GoTo Whoa

    '~~> Sheet Where values needs to be checked
    Set wsI = ThisWorkbook.Sheets("Sheet1")
    '~~> Output sheet
    Set wsO = ThisWorkbook.Sheets("Sheet2")

    Application.ScreenUpdating = False

    With wsI
        '~~> Find Last Row which has data in Col O to R
        If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
            'You specified "After" to be cell O3.  This means a match will
            '  occur on row 2 if cell R2 (or O2 or P2) has something in it
            '  because cell R2 is the cell "after" O3 when
            '  "SearchDirection:=xlPrevious"

            '             After:=.Range("O3"), _

            lastrow = .Columns("O:R").Find(What:="*", _
                          After:=.Range("O1"), _
                          Lookat:=xlPart, _
                          LookIn:=xlFormulas, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlPrevious, _
                          MatchCase:=False).Row
        Else
            lastrow = 1
        End If

        'This was only referring to the single cell in column R on the
        '  last row (in columns O:R)
        'Set rSource = .Range("R" & lastrow)
        'Create a range referring to everything in column R, from row 1
        '  down to the "last row"
        Set rSource = .Range("R1:R" & lastrow)

        'This comment doesn't seem to reflect what the code was doing, or what the
        'question said
        '~~> Search for the cell which has "L" and then copy it across to sheet1
        For Each c In rSource
            'This is printing the variable "cValue", which has never been set
            'Debug.Print cValue
            'It was probably meant to be
            Debug.Print c.Value
            'This was testing whether the value in the cell was
            '  greater than the string "0"
            'So the following values would be > "0"
            '  ABC
            '  54
            '  ;asd
            'And the following values would not be > "0"
            '  (ABC)
            '  $523   (assuming that was as text, and not just 523 formatted as currency)
            'If c.Value > "0" Then
            'I suspect you are trying to test whether the cell is numeric
            '  and greater than 0
            If IsNumeric(c.Value) Then
                If c.Value > 0 Then
                    'This is only copying the cell and the *three* cells
                    ' to the left of it
                    '.Range("O" & c.Row & ":R" & c.Row).Copy
                    'This will copy the cell and the *four* cells
                    ' to the left of it
                    '.Range("N" & c.Row & ":R" & c.Row).Copy
                    'wsO.Cells(5 + IRow, 12).PasteSpecial Paste:=xlPasteValues
                    'But this would avoid the use of copy/paste
                    wsO.Cells(5 + IRow, 12).Resize(1, 5).Value = _
                         .Range("N" & c.Row & ":R" & c.Row).Value
                    IRow = IRow + 1
                End If
            End If
        Next
    End With

LetsContinue:
    Application.ScreenUpdating = True
    Application.CutCopyMode = False
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

这篇关于Excel VBA-根据单元格值在VBA中复制和粘贴循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆