【问题标题】:How to copy rows from one sheet to another based on criteria in column-- paste only values and formatting (not formulas)?如何根据列中的条件将行从一张表复制到另一张表 - 仅粘贴值和格式(而不是公式)?
【发布时间】:2025-12-11 12:40:02
【问题描述】:

此代码按预期工作,可复制 B 列中给定值为“xxx”的单元格。 问题是它复制了整个行的内容,包括公式。我只想复制单元格值和格式,而不是公式。

Sub CommandButton1_Click()
   Dim LastRow As Long
   Dim i As Long, j As Long

   'Find the last used row in a Column: column A in this example (source sheet = sheet2)
   With Worksheets("Sheet2")
  LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
   End With

   'Message box to confirm how many rows were scanned to ensure all rows were scanned
   MsgBox ("Number of rows scanned: " & LastRow)


   'First row number where you need to paste values in Sheet3 (destination sheet = sheet3)'
   With Worksheets("Sheet3")
  j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
   End With

   For i = 1 To LastRow
   With Worksheets("Sheet2")
       If .Cells(i, 2).Value = "xxx" Then
           .Rows(i).Copy Destination:=Worksheets("Sheet3").Range("A" & j)
           j = j + 1
       End If
   End With
   Next i
End Sub

我尝试将最后一部分修改为类似

       .Rows(i).Copy 
       .Range("A" & j).PasteSpecial xlPasteValuesAndNumberFormats

但是,这会尝试将行粘贴到同一个工作表中(可能是因为它位于“With”下)。我无法更改粘贴行的目的地。理想情况下,我希望将复制的行粘贴到 Sheet3 中。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    使用 value=value 代替复制粘贴:

    .Rows(j).value = .rows(i).value
    

    要移动到另一个工作表,您可以添加工作表引用和 lastrow:

    sheets(3).rows(sheets(3).cells(sheets(3).rows.count,1).end(xlup).offset(1,0).row).value = .rows(i).value
    

    编辑1:

    使用你的 j...

    sheets(3).rows(j).value = .rows(i).value
    

    【讨论】:

    • 感谢您的回复!我担心我对 VBA 太无知了,无法理解如何在我的代码中实现它以使其正常工作,但是......您能否澄清一下我应该将目标表的引用添加到哪些部分?
    • @Tester_Y 这将进入你的循环,替换复制/粘贴行
    【解决方案2】:
    Public Function FilterByTable(fromWs As Worksheet, destWs As Worksheet, tableFilter As String) As Boolean
        Dim copyFrom As Range
        Dim lRow As Long
        'Assume false
        FilterByTable = False
    
        With fromWs
            .AutoFilterMode = False
    
            'This gives the value for the last row in this range
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row
            With .Range("A1:A" & lRow)
                'Looking for any row that meets this filter i.e. val=tableFilter
                .AutoFilter Field:=1, Criteria1:="=" & tableFilter
                Set copyFrom = .SpecialCells(xlCellTypeVisible).EntireRow
            End With
            .AutoFilterMode = False
        End With
    
        With destWs
            'Some error checking since this will fail if you try to perform the operation on an empty data set
            If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
                lRow = .Cells.Find(What:="*", _
                              After:=.Range("A1"), _
                              Lookat:=xlPart, _
                              LookIn:=xlFormulas, _
                              SearchOrder:=xlByRows, _
                              SearchDirection:=xlPrevious, _
                              MatchCase:=False).Row
            Else
                lRow = 1
            End If
            copyFrom.Copy .Rows(lRow)
        End With
        FilterByTable = True
    End Function
    

    【讨论】:

      最近更新 更多