根据单元格值将单元格值复制到另一个工作表

问题描述 投票: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

enter image description here

excel vba spreadsheet copy-paste
1个回答
1
投票

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

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.