简体   繁体   English

VBA:Excel如何复制,包括格式和空格

[英]VBA:Excel how to copy including formatting and spacing

Right now, I'm doing this to copy a row from one sheet to another sheet and insert it at the end: 现在,我正在执行此操作以将一行从一张纸复制到另一张纸并将其插入到末尾:

            Sheets("Monthly").Range("A1").EntireRow.Copy Destination:= _
            Sheets(Name).Range("A" & rows.Count).End(xlUp).Offset(1)

But this doesn't copy any formatting/cell spacing. 但这不会复制任何格式/单元格间距。 Is that possible to do in vba? 在vba中可以这样做吗?

Copy does copy the formatting. Copy不会复制格式。 Maybe your formatting isn't what you think. 也许您的格式不是您想的那样。 I don't know what cell spacing means, but if it's column width, then Copy won't copy that. 我不知道单元格间距是什么意思,但是如果它是列宽,那么Copy将不会复制该间隔。 You'll have to set that explicitly, like 您必须明确地进行设置,例如

For i = 1 to rSrce.Cells.Count
    rDest.Cells(i).EntireColumn.ColumnWidth = rSrce.Cells(1).EntireColumn.ColumnWidth
Next i

Using the Macro Recorder will give you the syntax for other formats. 使用宏记录器将为您提供其他格式的语法。

Range("A1").NumberFormat = "General"

Or 要么

.PasteSpecial Paste:=xlPasteFormats
.PasteSpecial Paste:=xlPasteValues

Ex. 例如 of a way to solve it 解决它的方法

Sub Copy()
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")

  ws.Range("A1").EntireColumn.Copy

    With ws.Range("G2").EntireColumn
        .PasteSpecial Paste:=xlPasteFormats
        .PasteSpecial Paste:=xlPasteValues
    End With

End Sub

There are a million ways to do this try 有百万种方法可以尝试

https://www.google.com/search?q=how+to+copy+formatting+in+excel+with+vba&oq=how+to+co&aqs=chrome.0.69i59l2j69i57j69i59j69i60j0.2240j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF-8 https://www.google.com/search?q=how+to+copy+formatting+in+excel+with+vba&oq=how+to+co&aqs=chrome.0.69i59l2j69i57j69i59j69i60j0.2240j0j7&sourceid=chrome&espv=210&es_sm=93&ie=UTF -8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM