如何在不复制行/列宽度的情况下复制数据

时间:2019-02-04 20:48:15

标签: excel vba

我正在使用VBA在符合条件时将可变数量的行从一张纸复制并粘贴到另一张纸。

这正在工作。但是,将数据粘贴到目标工作表后,列和行的宽度将更改为与源工作表相同。

如何阻止这种情况的发生?这样就可以粘贴数据,而无需进行单元格格式化。

如果有人知道,将不胜感激。

这里是我正在使用的代码。

Sub copyOverdue()

Dim cell As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet

Set Source = ActiveWorkbook.Worksheets("Action Register")
Set Target = ActiveWorkbook.Worksheets("Sheet1")

j = 36     ' Paste to this number row
For Each cell In Source.Range("A7:A243")
    If c = "Overdue" Then
       Source.Range("A" & c.row & "," & "G" & c.row).Copy Target.Range("AD" & j)
       Source.Range("C" & c.row & "," & "F" & c.row & "," & "H" & c.row & "," & "K" & c.row).Copy Target.Range("AF" & j)
       j = j + 1
    End If
Next cell
End Sub

1 个答案:

答案 0 :(得分:2)

您只能粘贴特殊值

For Each cell In Source.Range("A7:A243")
    If cell.value = "Overdue" Then
       cell.Resize(, 7).Copy
       Target.Range("AD" & j).PasteSpecial xlValues
       Source.Range("C" & cell.Row & "," & "F" & cell.Row & "," & "H" & cell.Row & "," & "K" & cell.Row).Copy
       Target.Range("AF" & j).PasteSpecial xlValues
       j = j + 1
    End If
Next cell

您还可以完全避开剪贴板并直接传输值,这虽然效率更高,但是在处理大范围数据时可能会有些混乱。

Target.Range("AD" & j).Resize(,7).Value=cell.Resize(, 7).value