Excel VBA 根据单元格值将单元格值复制并粘贴到另一个工作表

问题描述 投票:0回答:1

我正在尝试将单元格值(A641)从工作表(“WIP”)复制到工作表(“报告”)(A3),如果工作表(“WIP”)中的(N641)是特定值(“文本”)。

问题是我想循环执行此操作,因为我希望系统检查整个 N 列(也低于 641)。

仅供参考,主表不断扩展,所以我希望每次运行它时都检查到 N 列的末尾。

如有任何帮助,我们将不胜感激。亲切的问候。我从下面的另一个线程复制并玩了一下,但这不是我想要的。

Sub test()
    Dim i As Long, lastRow As Long

    Set wip = Sheets("WIP")
    Set report = Sheets("Report")

    lastRow = wip.Cells(Rows.Count, "N").End(xlUp).Row

    For i = 641 To lastRow
       
        If wip.Range("N" & i).Value = "test" Then
          
            report.Range("A" & i - 638).Value = wip.Range("A" & i).Value
            
        End If
    Next i
End Sub
excel vba spreadsheet copy-paste
1个回答
0
投票

类似这样的事情应该做到:

Sub test()
    Dim i As Long, lastRow As Long, wip As Worksheet, report As Worksheet
    Dim wb As Workbook, v, c As Range

    Set wb = ActiveWorkbook 'or (eg) ThisWorkbook
    Set wip = wb.Worksheets("WIP")
    Set report = wb.Worksheets("Report")

    lastRow = wip.Cells(wip.Rows.Count, "N").End(xlUp).Row
    Set c = report.Cells(Rows.Count, "A").End(xlUp).Offset(1) 'first destination cell
    
    For i = 641 To lastRow
        If wip.Range("N" & i).Value = "test" Then
            v = wip.Range("A" & i).Value 'ColA value
            If Len(v) > 0 Then           'anything to copy?
                c.Value = v              'copy value
                Set c = c.Offset(1)      'next destination cell
            End If
        End If
    Next i
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.