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) contains data from column A:EW, 1000+ rows, and is regularly updated with new rows
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
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. 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. 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.
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.