简体   繁体   English

Excel 2010 VBA复制范围A:Z和粘贴A2:Z

[英]Excel 2010 VBA to copy range A:Z and Paste A2:Z

I tried researching but have had no luck with what I'm trying to achieve. 我曾尝试研究,但对所要达到的目标却没有运气。 I have two different files I'm working with; 我正在使用两个不同的文件。 file1.csv(sheet1)and workbook1(sheet2) file1.csv(sheet1)和workbook1(sheet2)

file1.csv(sheet1) contains data from column A:EW, 1000+ rows, and is regularly updated with new rows file1.csv(sheet1)包含A:EW列中的数据,超过1000行,并定期用新行更新

The end goal here is to automatically copy the entire contents of file1.csv(sheet1) and paste the data from sheet 1 into workbook1(sheet2) starting at A2 on sheet2 because I need A1 to remain blank on sheet2 这里的最终目标是自动复制file1.csv(sheet1)的全部内容,并将工作表1中的数据粘贴到工作表1(sheet2)中,该工作簿始于sheet2上的A2,因为我需要A1才能在sheet2上保持空白

I should also note, I cannot provide the actual files as they are confidential, I could make an example files if necessary. 我还应注意,由于机密文件无法提供,因此我无法提供它们,如有必要,我可以提供示例文件。

This is the code I have so far in workbook1 but it does not work when copying to/from a different ranges. 这是我到目前为止在workbook1中使用的代码,但是当复制到不同的范围或从其他范围复制时它不起作用。 It works great if I change it to copy from A:EW to A:EW BUT I need to copy from A:EW on Sheet1 to A2:EW on sheet2. 如果将其更改为从A:EW复制到A:EW,效果很好, 但是我需要从Sheet1的A:EW复制到Sheet2的A2:EW。 Does anyone have any recommendations? 有人有什么建议吗?

Sub auto_open()
'
' auto_open Macro
'
ChDir "C:\"
Workbooks.Open Filename:="C:\Users\Username\Desktop\file1.csv"
Sheets("sheet1").Range("A:EW").Copy
Windows(ThisWorkbook.Name).Activate
Worksheets("sheet2").Activate
Range("A2:EW").Select
ActiveSheet.Paste
Windows("sheet1").Activate
Application.CutCopyMode = False
ActiveWorkbook.Close

' Change date/time format of column A from 20151111 090412 to 11/11/2015 9:04
Dim x As Integer
Dim y As Date
Dim z As String
Dim w As String

NumRows = Range("A2", Range("A2").End(xlDown)).Rows.Count

Windows(ThisWorkbook.Name).Activate
For x = 2 To NumRows
     z = Cells(x, 1).Value
     y = Mid(z, 5, 2) & "/" & Mid(z, 7, 2) & "/" & Left(z, 4)
     w = Mid(z, 10, 2) & ":" & Mid(z, 12, 2) & ":" & Mid(z, 14, 2)
     y = y + TimeValue(w)
     Cells(x, 1).Value = y
Next x

Range("A2").Select

End Sub

The Range.CurrentRegion property can quickly reference the 'island' of data originating in A1. Range.CurrentRegion属性可以快速引用源自A1的数据的“孤岛”。

Dim wb As Workbook
Set wb = Workbooks.Open(Filename:="C:\Users\Username\Desktop\file1.csv")

With wb
     'CSVs open with a single worksheet; usually named the same as the CSV, not Sheet1
    With .Worksheets(1)
        With .Range("A1").CurrentRegion
            'reference the destination workbook correctly (ThisWorkbook...?)
            .Copy Destination:=ThisWorkbook.Worksheets("sheet2").Range("A2")
        End With
    End With
.Close
End With

You only need to reference the cell in the top-left corner for the destination of a paste. 您只需要引用左上角的单元格即可粘贴到目标位置。

You can't copy the whole column in a column minus one row. 您不能将整列复制到一列减去一行中。 If you do that, overflow occurs. 如果这样做,则会发生溢出。 Restrict your copy a little more, eg you can try 稍微限制您的副本,例如您可以尝试

Workbooks.Open Filename:="C:\Users\Username\Desktop\file1.csv"
intersect(Sheets("sheet1").Range("A:EW"),sheets("sheet1).usedrange).Copy ThisWorkbook.Sheets("sheet2").Range("A2")

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

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