• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Need help with copying and pasting last 80 rows of data into my master excel sheet

Buddi

New Member
Hey Guys,

I am a newbie to these Macros. I am a intern at a medical company and they assigned me with a project that involves macro. They asked me to take this project a challenge even though, I am not familiar with these macros at all. But for past month I have been struggling with this project. I need desperate help from the EXPERTs. Help save my internship guys.
icon_eek.gif


A few things I am looking for my macro are:

1. I have multiple data sheets saved into a folder (for example: M1, M2, M3, M4, M5, M6 CSV spread sheets saved into a Error data folder), I need a macro that can grab all the 6 CSV sheets data. However I want each spreadsheet (M1-M6 sheets) to be opened in different tabs of my master copy. (I found a Macro to do this. Nonetheless, every time I run this code, it asks me to save the file it opens. But I want to remove the option of saving but I am not sure where in this code to delete that option. Also, if there is an easier way to do this, please share.)

Sub GetSheet()

Dim temp As String
Path = "13. Error Data Crunch\Suite 1" This is the folder where I have saved my 6 (M1, M2, M3, M4, M5, M6) csv files saved
Filename = Dir(Path & "*.csv??")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
temp = ActiveWorkbook.Name
ActiveSheet.Name = ActiveSheet.Name
ActiveWorkbook.Sheets(ActiveSheet.Name).Copy After:=ThisWorkbook.Sheets(1)
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub

2. These data sheets keeps updating every 10 min, as in for every 10 min, a data point will be added to each of the 6 excel sheets. Therefore, I am looking for a Macro where I can pull in the last 80 active data points to be pasted into my master sheet. Simple put, I want only the last 80 data points to be pasted into different tabs of my main excel sheet.

I found a macro that is able to select last 10 rows, however I don't know how to change the code, where I can have it to select the last 80 rows and paste it into my main spreadsheet.
Sub CopyLastTenRows()
MyLastRow = Range("A65536").End(xlUp).Row
Rows((MyLastRow - 10) & ":" & MyLastRow).Copy
' Enter the rest of your paste code here (The code I found says to enter the rest of paste code here but since I am not an expert in coding, I don't know what the paste code might be. So I can select the last 10 rows but I don't know how to use this code to have it paste into my master copy.)
End Sub

I know I would have to combine the 2 macros (I have above) to get what I want.
In my master copy this is what I am looking for:
a. I am looking for 6 different tabs, with my last 10 data points inserted from the 6 csv sheets I have saved in the error data folder i have.

For example: in sheet 1-I would have M1 last 10 data points
in sheet 2- M2 last 10 data points
in sheet 3- M3 last 10 data points
"
in sheet 6- M6 last 10 data points

If you guys are confused with what exactly I need help with, please ask me to clarify and I will try my best to answer your questions. BUT PLEASE HELP.....THANK YOU SOO MUCH!
icon_smile.gif
 
Take your spreadsheet, sanitize it so that there's nothing proprietary in it, and then post it all here. It's so much easier to understand your challenge by seeing the file - you'll get a quicker and better answer that way ...
 
Take your spreadsheet, sanitize it so that there's nothing proprietary in it, and then post it all here. It's so much easier to understand your challenge by seeing the file - you'll get a quicker and better answer that way ...

Hi David,
I wanted to upload the excel file but it wont let me because it is a csv file. When I tried uploading it says this:

The following error occurred:
The uploaded file does not have an allowed extension.

Therefore I will try to explain this as simple as i can with an example:

So lets say for ex: the following is my data,
2.21 2.27 -2.25 -2.31 3.33 3.37 -3.07 -3.13
2.21 2.27 -2.25 -2.3 3.32 3.38 -3.07 -3.13
2.21 2.28 -2.25 -2.32 3.33 3.38 -3.07 -3.14
2.13 2.26 -2.24 -2.3 3.33 3.38 -3.07 -3.12
2.13 2.19 -2.25 -2.31 3.33 3.38 -3.07 -3.12
2.13 2.21 -2.25 -2.33 3.33 3.38 -3.07 -3.12
2.13 2.22 -2.25 -2.31 3.33 3.39 -3.07 -3.12
2.13 2.21 -2.25 -2.3 3.33 3.38 -3.07 -3.13
2.13 2.2 -2.25 -2.3 3.32 3.37 -3.07 -3.13
2.13 2.2 -2.25 -2.3 3.32 3.37 -3.07 -3.13
2.13 2.21 -2.25 -2.32 3.33 3.37 -3.07 -3.13
2.13 2.2 -2.26 -2.3 3.32 3.37 -3.07 -3.13
2.13 2.19 -2.25 -2.3 3.32 3.37 -3.07 -3.12
2.12 2.19 -2.25 -2.3 3.33 3.38 -3.07 -3.14
2.13 2.19 -2.25 -2.3 3.32 3.39 -3.07 -3.12


So, there are 8 columns with 15 rows of data. In my actual spreadsheet, I have 19 columns and over 5,000 rows of data. So now what I am trying to do is make a macro where I can select only the last 80 sets of data points because the spreadsheet keeps updating every 10 min. So, taking a look at my ex: data, lets say I want to select only the last 5 rows of data.

So In my master copy spread sheet, I have labeled each sheet as (M1, M2, M3, M4, M6)
upload_2017-10-1_8-14-16.png (MASTER COPY SPREAD SHEET)

So Now, I want the last 5 rows of data (shown below) which I grabbed from M1 file to be pasted into M1 sheet.
2.132.21-2.25-2.323.333.37-3.07-3.13
2.132.2-2.26-2.33.323.37-3.07-3.13
2.132.19-2.25-2.33.323.37-3.07-3.12
2.122.19-2.25-2.33.333.38-3.07-3.14
2.132.19-2.25-2.33.323.39-3.07-3.12

So in my actual post, I have a macro where I am able to select the last 5 rows but what I dont have is, once I select the last 5 rows of data, how do I get it to paste it into my specified sheet of my master copy.

Also, I have 6 sets of csv files saved in a folder. So the data I have shown in the example is the data I obtained from one of the folder. What I want is to have a macro where I am able to copy last 10 rows of my data from each of the 6 files and have it copied into my master copy in different tabs like shown below:
upload_2017-10-1_8-14-16.png (MASTER COPY SPREAD SHEET)

So you see how I have different tabs named with M1-M6 (missing M5). So i want the data I saved in the folder shown below into my master copy shown above in different tabs.

upload_2017-10-1_8-15-14.png

I hope this helps clear out some confusion. Perhaps can you help me now??
 
Save your file as an Excel file, and then upload it.
File-Save As and then change the file type from .csv to .xlsx
 
I was able to save the files as excel and upload, however it only allowed me to upload my master and 2 of my sample files data.

Below, I have a macro that I am currently using to get to my goal. But I was wondering if there is an easier way to run this macro without having to go through so many tedious steps. (I just figure this out last friday, so I am very happy that I am able to do this without having any knowledge in coding or macros.)

Also, I was wondering if you can share a sample macro of a simple conditional formatting that I can use to run for all the sheets in my master copy?

Thank you and let me know if there is are any questions that I can answer.



Sub RunAll()
Call Rectangle2_Click
Call CopyLastTenRows
Call DeleteTabs
End Sub
Sub Rectangle2_Click()
' I am able to paste the original data in different sheets using this macro
' but everytime this macro runs, it always asks me to whether i should save
' I want to remove that option
Dim temp As String
Path = "C:\Users\fjaha\Desktop\Practice Data"
Filename = Dir(Path & "*.csv??")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
temp = ActiveWorkbook.Name
ActiveSheet.Name = ActiveSheet.Name
ActiveWorkbook.Sheets(ActiveSheet.Name).Copy After:=ThisWorkbook.Sheets(1)
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
Sub CopyLastTenRows()
'Using the above macro, i am able to select the last 80 data rows and
'paste it into the labeled sheets
Sheets("M1").Select
MyLastRow = Range("A65536").End(xlUp).Row
Rows((MyLastRow - 80) & ":" & MyLastRow).Copy
Sheets("(M1)").Range("A" & Rows.Count).End(xlUp).Offset(3).PasteSpecial Paste:=xlPasteValues
Sheets("M2").Select
MyLastRow = Range("A65536").End(xlUp).Row
Rows((MyLastRow - 80) & ":" & MyLastRow).Copy
Sheets("(M2)").Range("A" & Rows.Count).End(xlUp).Offset(3).PasteSpecial Paste:=xlPasteValues
Sheets("M3").Select
MyLastRow = Range("A65536").End(xlUp).Row
Rows((MyLastRow - 80) & ":" & MyLastRow).Copy
Sheets("(M3)").Range("A" & Rows.Count).End(xlUp).Offset(3).PasteSpecial Paste:=xlPasteValues
Sheets("M4").Select
MyLastRow = Range("A65536").End(xlUp).Row
Rows((MyLastRow - 80) & ":" & MyLastRow).Copy
Sheets("(M4)").Range("A" & Rows.Count).End(xlUp).Offset(3).PasteSpecial Paste:=xlPasteValues
Sheets("M6").Select
MyLastRow = Range("A65536").End(xlUp).Row
Rows((MyLastRow - 80) & ":" & MyLastRow).Copy
Sheets("(M6)").Range("A" & Rows.Count).End(xlUp).Offset(3).PasteSpecial Paste:=xlPasteValues
End Sub
Sub DeleteTabs()
'Since I don't need the original files anymore, i chose to delete them
' I was wondering if there is an easier way to do all this without going
'through so many steps.
Sheets("M1").Select
ActiveWindow.SelectedSheets.Delete
Sheets("M2").Select
ActiveWindow.SelectedSheets.Delete
Sheets("M3").Select
ActiveWindow.SelectedSheets.Delete
Sheets("M4").Select
ActiveWindow.SelectedSheets.Delete
Sheets("M6").Select
End Sub
 

Attachments

  • Master Copy.xlsm
    21.8 KB · Views: 2
  • M1.xlsx
    887.4 KB · Views: 1
  • M2.xlsx
    997.3 KB · Views: 1
Hi Rahul,

I am not sure what Power query Solution is. .
I am using 2017 Excel and my data files are csv files.
Where as my master copy is a macro-enable file.

I am not sure, if I answered your questions. But like I mentioned
before, I am not an expert in these macros. I am just a beginner.

Thanks,
Buddi
 
Back
Top