简体   繁体   English

使用 VBA 复制和粘贴会使格式无效

[英]Copy and pasting with VBA nullifies formatting

I've written some VBA to copy and paste some data from one workbook into another:我编写了一些 VBA 来将一个工作簿中的一些数据复制并粘贴到另一个工作簿中:

Dim x As Workbook
Dim y As Workbook

' Open both workbooks
Set y = ActiveWorkbook
Set x = Workbooks.Open("Data.csv")

' Copy data from x
x.Sheets("SourceData").Range("A1", _
    x.Sheets("SourceData").Range("A1").End(xlDown).End(xlToRight)).Copy

' Paste to y
y.Sheets("Destination").Range("C4").PasteSpecial Paste:=xlPasteValues
y.Sheets("Destination").Range("C4").PasteSpecial Paste:=xlPasteFormats

This pastes the data in the correct location.这会将数据粘贴到正确的位置。

The next step of the VBA is to filter the data table by the first column which contains a date. VBA 的下一步是按包含日期的第一列过滤数据表。 However, the paste of the data converts everything to general data type, but more importantly seems to lose the 'date value' behind the string in the field.但是,数据的粘贴会将所有内容转换为通用数据类型,但更重要的是似乎丢失了字段中字符串后面的“日期值”。 For example, when I try to format the date column following the paste, no other format type will change what appears in the cell (ie converting to number will still show 01/01/2018 rather than 43101).例如,当我尝试格式化粘贴后的日期列时,没有其他格式类型会更改单元格中显示的内容(即转换为数字仍将显示 01/01/2018 而不是 43101)。 This causes the filtering code to hide all rows because there are no dates that fall within the parameters (because there are no dates essentially).这会导致过滤代码隐藏所有行,因为没有落在参数范围内的日期(因为本质上没有日期)。 I can't even manually filter on the dates (ie without VBA).我什至不能手动过滤日期(即没有 VBA)。

This image shows how the preview for each data type is still 20/02/2018.此图显示了每种数据类型的预览仍然是 20/02/2018。

Whenever I manually copy and paste the data it works fine and I can format the dates in any way.每当我手动复制和粘贴数据时,它都可以正常工作,我可以以任何方式格式化日期。 It's just when using VBA that the formatting issues crop up.只有在使用 VBA 时,格式问题才会出现。

Amongst other things I've tried:我尝试过的其他事情包括:

  • Using VBA to format the columns in the source sheet before copy/pasting在复制/粘贴之前使用 VBA 格式化源工作表中的列
  • Using VBA to format the columns in the destination sheet after copy/pasting复制/粘贴后使用 VBA 格式化目标工作表中的列
  • Moving the whole sheet into the destination workbook and copying/pasting from within the workbook (loses formatting upon moving sheet)将整个工作表移动到目标工作簿并从工作簿内复制/粘贴(移动工作表时丢失格式)
  • Pasting values & then pasting format over the top粘贴值,然后在顶部粘贴格式
  • Using different file formats for the data file对数据文件使用不同的文件格式

I wonder if the issue could be due to a setting in Excel?我想知道问题是否可能是由于 Excel 中的设置造成的? Any help greatly appreciated.非常感谢任何帮助。

along the lines of @tigeravatar solution, but with a more concise code沿用@tigeravatar 解决方案,但代码更简洁

Dim y As Workbook

Set y = ActiveWorkbook

With Workbooks.Open("Data.csv").Sheets("SourceData") 'open source workbook and reference its "SourceData" sheet
    With .Range("A1").CurrentRegion 'reference referenced sheet range "adjacent" to cell A1
        y.Sheets("Destination").Range("C4").Resize(.Rows.Count, .Columns.Count).value = .value
    End With
    .Close False
End With

Don't copy/paste, set the values directly:不要复制/粘贴,直接设置值:

Dim x As Workbook
Dim y As Workbook
Dim rCSVValues As Range

' Open both workbooks
Set y = ActiveWorkbook
Set x = Workbooks.Open("Data.csv")

' Copy data from x
With x.Sheets("SourceData")
    Set rCSVValues = .Range("A1", .Range("A1").End(xlDown).End(xlToRight))
End With

' Paste to y
y.Sheets("Destination").Range("C4").Resize(rCSVValues.Rows.Count, rCSVValues.Columns.Count).Value = rCSVValues.Value

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

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