Monday, 11 September 2017

excel - VBA copy and paste multiple cells, without overwriting existing information

I have multiple excel books that i need to extract data from and put it in a consolidated view. I have been trying to copy and paste different cells from one workbook and paste them to another. I managed to do that with one cell but i am not too sure how to do it for multiple cells (not a range, though)?



Lets say I have 5 files. I loop through them and I want Cell F18 to be copied to Cell A1 and Cell F14 to be copied to B1...Then go to the next file and do the same but append the information on the next blank row.



Here is the code i am using



Sub AllFiles()
Dim folderPath As String
Dim filename As String

Dim wb As Workbook

folderPath = "C:\Users\enchevay\Desktop\automation\WeeklyReports\"

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

filename = Dir(folderPath & "*.xlsx")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)



'copy name (cell F18 and paste it in cell A2)
Range("F18").Copy

'copy client (cell F14 and paste it to B2)

Application.DisplayAlerts = False
ActiveWorkbook.Close


emptyRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(emptyRow, 1), Cells(emptyRow, 2))

filename = Dir
Loop


Application.ScreenUpdating = True
End Sub

No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...