vba 使用复制目标和选择性粘贴来保持格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14058963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:59:20  来源:igfitidea点击:

Using Copy Destination and Paste Special to keep formatting

excelvba

提问by Mike

This is the line of code that I need to, when copying the cells, keep the formatting in the destination workbook. I'm not sure how I would incorporate something like "PasteSpecial Paste:=xlPasteValues" into it.

这是我需要的代码行,在复制单元格时,将格式保留在目标工作簿中。我不确定如何将“PasteSpecial Paste:=xlPasteValues”之类的内容合并到其中。

wbkOut.Worksheets(1).Range("E44:E2000").Copy Destination:=wbkVer.Worksheets("Metabuild").Range("A" & wbkVer.Worksheets("Metabuild").Range("A65536").End(xlUp).Row + 1)

回答by Scott Holtzman

Ben was right. Since he hasn't shown you how, I will.

本是对的。既然他没有告诉你怎么做,我会的。

wbkOut.Worksheets(1).Range("E44:E2000").Copy 

With wbkVer.Worksheets("Metabuild")
  .Range("A" & .Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
End With

Another thing you may want to add in is a check to make sure there are enough rows at the bottom of your sheet to handle the row count of the copied range, if you may need it. If there aren't enough rows, the code will throw an error.

您可能想要添加的另一件事是检查以确保工作表底部有足够的行来处理复制范围的行数(如果您可能需要的话)。如果没有足够的行,代码将抛出错误。

To do that, the code will look something like this:

为此,代码将如下所示:

Option Explicit

Dim rngCopy as Range, lngCopy as Long

Set rngCopy = wkbOut.Worksheets(1).Range("E44:E2000")
lngCopy = rngCopy.Rows
rngCopy.Copy

With wbkVer.Worksheets("Metabuild")

    Dim rngCheck as Range
    Set rngCheck = .Range(.Range("A" & .Rows.Count).End(xlup).Offset(1),.Range("A" & .Rows.Count)

    If rngCheck.Rows >= lngCopy Then

       .Range("A" & .Rows.Count).End(xluP).Offset(1).PasteSpecial xlPasteValues

    Else

       Msgbox "Not enough space!"

   End If

End With