सोमवार, 12 अक्टूबर 2015

NCERT Solutions for Class 7th Science Chapter 13 – Motion and Time

NCERT Solutions for Class 7th Science Chapter 13 – Motion and Time
August 6, 2014 by Neepur Garg 5 Share 8 5 6013 NCERT Solutions for Class 7th Science Chapter 13 – Motion and Time National Council of Educational Research and Training (NCERT) Book Solutions for Class 7th Subject: Science Chapter: Chapter 13 – Motion and Time Class 7th Science Chapter 13 Motion and Time NCERT Solution is given below
. Question 1: Classify the following as motion along a straight line, circular or oscillatory motion: (i) Motion of your hands while running. (ii) Motion of a horse pulling a cart on a straight road. (iii) Motion of a child in a merry-go-round. (iv) Motion of a child on a see-saw. (v) Motion of the hammer of an electric bell. (vi) Motion of a train on a straight bridge. Answer: (i) Oscillatory motion While running, the hands move to and fro and repeat their motion after a given interval of time. Hence, it is an oscillatory motion. (ii)Straight line The horse is pulling a cart on a straight road. Therefore, it has a motion along a straight line. (iii)Circular motion Merry-go-round has a circular motion. Therefore, a child sitting inside it will also have a circular motion. (iv) Oscillatory motion The child on a see-saw goes up and down continuously. It oscillates up-down. Therefore, it is an oscillatory motion. (v)Oscillatory motion The hammer hits the electric bell and vibrates rapidly. Therefore, it is an oscillatory motion. (vi) Straight line The train is moving on a straight bridge. Therefore, it has a motion along a straight line.
Question 2: Which of the following are not correct? (i) The basic unit of time is second. (ii) Every object moves with a constant speed. (iii) Distances between two cities are measured in kilometres. (iv) The time period of a given pendulum is not constant. (v) The speed of a train is expressed in m/h. Answer: (i) Correct Second is the SI unit of time. (ii) Not correct An object can move with constant or variable speed. (iii) Correct The distance between two cities can be very large. Since kilometre is a bigger unit of distance, the distance between two cities is measured in kilometres. (iv) Not correct Time period of a pendulum depends on the length of the thread. Hence, it is constant for a particular pendulum. (v) Not correct The speed of a train is measured either in km/h or in m/s.

Question 3: A simple pendulum takes 32 s to complete 20 oscillations. What is the time period of the pendulum? Answer: Number of oscillations = 20 Total time taken to complete 20 oscillations = 32 s

Question 4:The distance between two stations is 240 km. A train takes 4 hours to cover this distance. Calculate the speed of the train. Answer: Distance between the two stations = 240 km Time taken = 4 h
Question 5: The odometer of a car reads 57321.0 km when the clock shows the time 08:30 AM. What is the distance moved by the car, if at 08:50 AM, the odometer reading has changed to 57336.0 km? Calculate the speed of the car in km/min during this time. Express the speed in km/h also. Answer: Initial reading of the odometer of the car = 57321.0 km Final reading of the odometer of the car = 57336.0 km Distance covered by the car = Final reading of the odometer of the car − Initial reading of the odometer of the car = 57336.0 − 57321.0 = 15 km The given car starts at 8:30 a.m. and stops at 8:50 a.m. Therefore, time taken by the car to cover the distance is (8:50 − 8:30) min = 20 min Distance covered by the car = 15 km Time taken by the car = 20 min Again, 60 min = 1 h Time taken by the car =
Question 6: Salma takes 15 minutes from her house to reach her school on a bicycle. If the bicycle has a speed of 2 m/s, calculate the distance between her house and the school. Answer: Time taken by Salma to reach her school from her home = 15 min = 15 × 60 = 900 s Speed of her bicycle = 2 m/s Distance covered = Speed × Time taken = 2 × 900 = 1800 m 1000 m = 1 km Question 7:Show the shape of the distance-time graph for the motion in the following cases: (i) A car moving with a constant speed. (ii) A car parked on a side road. Answer: (i)A car moving with a constant speed covers equal distance in equal intervals of time. Such motion of car is represented in the given distance-time graph. (ii)The distance-time graph of a car parked on a road side is such that with the increase in time, there is no change in distance, as shown in the given figure.
Question 8: Which of the following relations is correct? (i) Speed = Distance × Time Answer:(ii) Speed of an object is given by the relation
Question 9: The basic unit of speed is: (i) km/min (ii) m/min (iii) km/h (iv) m/s Answer: (iv) m/s The basic unit of distance is metre (m). The basic unit of time is second (s). Therefore, the basic unit of speed is m/s.
Question 10:A car moves with a speed of 40 km/h for 15 minutes and then with a speed of 60 km/h for the next 15 minutes. The total distance covered by the car is: (i) 100 km (ii) 25 km (iii) 15 km (iv) 10 km Answer: (ii) 25 km Case I Speed of the car = 40 km/h Time taken = 15 min = Distance covered, d1 = Speed × Time taken = 40 × 0.25 = 10 km Case II Speed of the car = 60 km/h Time taken = 15 min = Distance covered, d2 = Speed × Time taken = 60 × 0.25 = 15 km Total distance covered by the car, d = d1 + d2 = 10 + 15 = 25 km Therefore, the total distance covered by the car is 25 km.

Question 11: Suppose the two photographs, shown in Figure 1 and Figure 2, had been taken at an interval of 10 seconds. If a distance of 100 metres is shown by 1 cm in these photographs, calculate the speed of the blue car. Figure 1 Vehicles moving in the same direction of on a road Figure 2 Position of vehicles shown in Figure 1 after some time Answer: The distance covered by the blue car (as evident from the photograph) from one white strip to another, which is measured by scale is 1.4 cm. It is given that 1 cm is equivalent to 100 m. Therefore, 1.4 cm is equivalent to 140 m. Distance travelled by the car = 140 m Time interval between the two photographs = 10
s
Question 12: Figure shows the distance-time graph for the motion of two vehicles A and B. Which one of them is moving faster? Distance-time graph for the motion of two cars Answer: Vehicle A is moving faster than vehicle B. Speed is given by the relation This relation shows that speed of a vehicle is greater if it covers maximum distance in a given interval of time. To compare the distance, draw a line perpendicular to the timeaxis, as shown in the following distance-time graph. From the graph, it is evident that for a given time t, the distance covered by vehicle A is more than vehicle B. Hence, vehicle A is moving faster than vehicle B.

Question 13: Which of the following distance-time graphs shows a truck moving with speed which is not constant? Answer: Graph (iii) In a distance-time graph, the constant speed of a truck will be represented by a straight line. In a distance-time graph, a straight line parallel to the time axis indicates that the truck is not moving. A curved line on the distance-time graph indicates that the truck is moving with a speed which is not constant.

लेबल:

Consolidate data from different excel files (VBA)

Consolidate data from different excel files (VBA) Published on April 9th, 2012 Written by: Vijay Sharma Next Skip to Responses Prev This is a guest post by Vijay, our in-house VBA Expert. Last week, we learned how to use SQL and query data inside Excel. This week, lets talk about how we canuse VBA to consolidate multiple data sheets from different workbooks into one single worksheet. Consolidate Data Demo First, lets take a look at the consolidate data VBA code. Consolidating Data from different Excel files – the setup There is one master file (or sheet) which needs to be consolidated by pulling data from multiple source files containing raw data (having the same data structure). Lets try to make a generic consolidation macro so that we can use this almost anywhere. We start of by creating a simple table on our sheet, we will call this List. On this table essentially we are defining everything that our VBA code needs to know to copy and paste data. We start by telling the name of the Excel workbook and then the complete path (location) of the file. In the next 2 cells we define what are the starting cell and the ending cell that contains our data. Next we are put the name of the worksheet where the data will be pasted. In our example the sheet remains the same however as per your requirements you may put a different sheet name. The last option is to specify where to paste the copied data and we only need to tell the start cell address, the code will automatically select the next empty cell in that column and then paste the data from that point onwards. Let’s understand the code. Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strListSheet As StringstrListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value <> “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select lastRow = LastRowInOneColumn(strStartCellColName) Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select Loop Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub We have used the Workbook object to accomplish this task and also the Error handler to trap any errors that may come in case any file is missing. The current code will display a message box when it is not able to open any file and will stop. We start by assigning the workbook where we want to consolidate the date to the variable currentWB by using the statement: Set currentWB = ActiveWorkbook After this a looping construct has been used to go through all the inputs provided one by one and open the workbooks, it has been assumed these workbooks to contain on the data that we need to copy hence I did not specify the source sheet name, however this can be easily added to this code to add more functionality. Inside our loop are the 4 variables which are assigned the 1) File name, 2) Copy Range, 3) Where To Copy and 4) Which Column contains the starting cell to paste data. We open the data workbook by using the Application.Workbooks.Openmethod. Once we have our first data workbook open, we assign this to the dataWB variable so that we can easily switch between the two workbooks and close them when the operation has been completed. Next we select the data that has been assigned to the copy range and copy to the clipboard. We then switch back to our main workbook and select the sheet where we want to paste the data, I have assigned this to the variable called “strWhereToCopy”. This allows us to paste data onto separate sheets within the same workbook. I have also made use of UDF (user defined function) to find the last cell in the column that we specify. Once we have found the last row we then select the next empty cell below that and paste our data then. Additional things that may be used to enhance this code 1. Since we are using the same instance of Excel we may allow the user to preserve the format of the data being pasted. 2. Allow the user with the option to clear data before new is pasted. Download Consolidate Data from different files Demo file Click here to download the workbook. Please Note: You would need to create the data files on your system, this download only contains the code template to consolidate. More on VBA & Macros If you are new to VBA, Excel macros, go thru these links to learn more. More Examples on Consolidation What is VBA & Macros? Introduction Excel VBA Example Macros VBA tutorial videos Join our VBA Classes If you want to learn how to develop applications like these and more, please consider joining our VBA Classes. It is a step-by-step program designed to teach you all concepts of VBA so that you can automate & simplify your work. Click here to learn more about VBA Classes & join us. Share this tip with your friends Facebook9LinkedInTwitter10GoogleEmailPrint Categories: Automation, Excel Howtos, VBA Macros Share Article Save to Instapaper Next Back to Top Prev 51 Comments Alex Apr 09 - 2:01 pm Reply Chandoo, nice article as ever. I use the RDBMerge addin to achieve the same result – lots of great options for merging files/sheets. http://www.rondebruin.nl/merge.htm Alexander Van Parys May 03 - 11:40 pm Reply Fantastic Alex, thanks for the tip! J Apr 10 - 10:20 am Reply Hi, Would it be possible to make it a little bit more flexible and allow the use of wildcard characters in a file name? Jason H Apr 10 - 4:48 pm Reply @J You can use the Dir function to enumerate all the files that match a wildcard combination within a specified folder folder. Few points you’d have to compensate for in the code provided. It assumes that the default sheet that the workbook opens up with contains the data you want (“Copy From Sheet” is not specified). The data range is of fixed size, so if your source data can be variable it won’t adjust (use of CurrentRegion method is a good way to pick up tables of data) My personal preference is to not use the Selection object to manipulate ranges; although I understand this is a VBA beginners guide. Regards Jason Suggi Apr 24 - 1:22 pm Reply Hi Jason, Thanks for sharing the Macro. I am new to Macro and dont know much of the coding. You have used Application.Workbooks.Open method for opening the excel. I want to know how can we open file thru “Import Text wizard” method using same macro instead of “Application.Workbooks.Open”. There is specif purpose for me to open file thru Import text Wizard. I have recorded macro of opening the file thru Import Text Wizard. Code is Workbooks.OpenText Filename:=”C:\XYZ.xls”, Origin:=437, StartRow _ :=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _ , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _ Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _ Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _ 16, 1), Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), Array(21, 1), Array(22, 1), _ Array(23, 1), Array(24, 1), Array(25, 1), Array(26, 1), Array(27, 1), Array(28, 1), Array( _ 29, 1), Array(30, 1), Array(31, 1), Array(32, 1), Array(33, 1), Array(34, 1), Array(35, 1), _ Array(36, 1), Array(37, 1), Array(38, 1), Array(39, 1), Array(40, 1), Array(41, 1), Array( _ 42, 1), Array(43, 1), Array(44, 1), Array(45, 1), Array(46, 1), Array(47, 1), Array(48, 1), _ Array(49, 1), Array(50, 1), Array(51, 1), Array(52, 1), Array(53, 1), Array(54, 1), Array( _ 55, 1), Array(56, 1), Array(57, 1), Array(58, 1), Array(59, 1), Array(60, 1), Array(61, 1), _ Array(62, 1), Array(63, 1), Array(64, 1), Array(65, 1), Array(66, 1), Array(67, 1), Array( _ 68, 1)), TrailingMinusNumbers:=True Can you help me with the necessary modification to your macro Elmer Apr 12 - 10:13 am Reply how about copy from different workbooks? Jake Apr 17 - 5:59 pm Reply What if my data already has headers. I would like to import the data into the top row. What would i change to do this? Also, a big thank you. This is very helpful. Mike Ebert Apr 17 - 6:51 pm Reply Another add-in that might be relevant here is XLhub (http://www.xlhub.com)–XLhub lets you tie your spreadsheets to a SQL Server database so that you can share data between spreadsheets (which are kept up-to-date across all the files), enable multi-user access, and keep track of different versions. The hardest part is some configuration work with SQL Server–no VBA required. Of course, your Visual Basic solution is probably more appropriate for people who 1) can’t install new software (SQL Server) or add-ins or 2) can’t pay $99 for a license of XLhub. Thanks for the post! Sophia May 25 - 8:47 pm Reply Good afternoon, Thank you very much for sharing this information! I am very new to VBA and am wondering how to specify the source sheet in the code. My Files have multiple sheets and I only need to consolidate data from one specific sheet of each file to the Master. Can you please let me know how to do this? Thank you! Sophia Jason H May 26 - 12:05 pm Reply Sophia, Using the existing code above you would need to add a column into the reference table to specify the name and then modify the code to pick up the name of the sheet into a variable such as “strCopySheet”. If you added it at right hand end of the list then that would be: strCopySheet = ActiveCell.Offset(0, 5).Value If you wanted to add it in the middle of the current table then you’d have to change the numbers referring to cells to the right of your new column to move them over 1 also. Then where the VBA currently says: Range(strCopyRange).Select Change it to: Sheet(strCopySheet).Range(strCopyRange).Select Hope this helps. Maybe when I have a moment I’ll write a version that’s less reliant on Selection and can do pattern matching etc… JH N00b with Logic May 17 - 10:37 am Reply Hello! I also needed to select from multiple files and on a a specific datatab. For this purpose, I looked for the first time ever into macros and VBA. So I added an extra column (H) with tab names. Next I had a look into VBA, based on suggestion above. With some logic I improved the code, which seems to work just fine: ———————————— Public strFileName As String Public currentWB As Workbook Public dataWB As Workbook Public strCopyRange As String Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strListSheet As String Dim strCopySheet As String strListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strCopySheet = ActiveCell.Offset(0, 6).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Sheets(strCopySheet).Select Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select lastRow = LastRowInOneColumn(strStartCellColName) Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select Loop Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub Public Function LastRowInOneColumn(col) ‘Find the last used row in a Column: column A in this example Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).Row End With LastRowInOneColumn = lastRow End Function ———————————————————- An extra idea to make this excel sheet work is to use the =concenate formula to construct the path name. Succes! N00b with logic Patrick Aug 28 - 7:51 am Reply Hi N00b, I have try using your macro to run but it show me “It seems some file was missing. The data copy operation is not complete.” I check that the file that I want to copy is open and the range has been copied but then it’s not pasting to the MasterData? Sandhya Oct 10 - 6:32 am Reply I am very much satisfied with above a select from multiple files into specific tab, but My paste location will start from G258 and so on. can you please suggest me how to change my offset values to paste location. Bene Jul 03 - 4:55 am Reply Hi Thanks for sharing. I have used this piece of code successfully to merge over 20 files within my workbook, it is a time saver. However I have one questions regarding the Path of the file – the example use the full path but I would like to use the relative path. My excel file is called Datacollection.xls and then I have a folder called “reports” that contains my 20 files. in the Path column I used \reports\ and it worked but today I added 5 additional files and the relative path does not work for those, but it does for the previous file. I’d like to use a relative path for more flexibility. is there any specific reason why it doesn’t work (I am 100% sure that the name of my files are correct and the name of the relative path as well it just won’t Open the file – this is where it bugs and says not found) Any idea? thanks Dan Aug 28 - 5:58 pm Reply Hi Rene, I need to combine 30 files of the same format with one sheet report into one combined (consolidated)report. Can you help me with the macro? Yhank you, Dan Ron Jul 12 - 8:36 pm Reply I am running into a complie error saying “Sub of Function Not Defined” for the following: LastRowInOneColumn Could you please help me out! Thanks!!1 Addy Jul 24 - 6:36 pm Reply Hi, I have having a Data in one Excell sheet and i have to update/Import the data in other excel workbooks. However the cells in which i have to update is not consistent and it will be depending based on the name of the template. So i am looking for a Macro which should identify the cell headers and update the data from the Main Template. Could any one help out me please. Thanks in Advance vashisth Aug 21 - 10:04 am Reply Hey what if the source data is present in different tabs of the same excel file??? e.g. for the above example, imagine i have 20 tabs in abc.xlsx file and i need to copy data from all those tabs to some other file. Awaiting a helpful reply Thanks in advance! Even faster ways to Extract file name from path [quick tip] | Chandoo.org - Learn Microsoft Excel Online Oct 24 - 8:02 am […] Extract data from multiple files & place in one sheet […] Sabir Nov 09 - 7:48 am Reply Hi Jason H, can you please explain this to me, i have tried placing in the name of the shet using your method but bot ways do not work on my end… Can you please assist me with this or if anyone can tell me how to include the name of the actual sheet in the “List” tab and then change the code accordingly to retrieve information specific to a named sheet Chris Nov 13 - 5:24 pm Reply Hi, Jason H. (and/or anyone else who solved this) My question is similar to those posed by Sophia and Sabir. I need to specify the sheet from which the specified ranges will be copied. I attempted to use the method you suggested, but running the macro then produces the error message. My code is as follows. My offsets are different because I have a total of 9 columns (I specified a specific range into which the data should be pasted. Headings are as follows: Item No File Name Full Path Data Range Start Cell Data Range End Cell Copy to Sheet Copy To Location(Start Cell Only) Copy To Location(End Cell Only) Which Sheet Copy Sub GetData() Dim strWhereToCopy As String, strStartCellRange As String Dim strListSheet As String, strWhichSheetCopy As String strListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value <> “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strStartCellRange = ActiveCell.Offset(0, 5) & “:” & ActiveCell.Offset(0, 6) strWhichSheetCopy = ActiveCell.Offset(0, 7).Value Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Sheets(strWhichSheetCopy).Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select Loop Exit Sub ErrH: MsgBox “It seems one or more files were missing. The data copy operation is not complete.” Exit Sub End Sub Sabir Nov 14 - 5:52 am Reply HI Chris, I got this to ork at my PC at home and it works brilliantly, when i take bring in to work it fails for some reason, just a snippet of my code below, in your code i can see where your error may lie strStartCellRange = ActiveCell.Offset(0, 5) & “:” & ActiveCell.Offset(6, 0) the underlined should be (2, 1) and you should end with your last offset value of 6 and not 7, i placed my copysheet in the middle and it changes the code… Can someone please advise further on how we can troubleshoot this, what a useful script, you rock Chandoo… Dim strListSheet As String, strcopysheet As String strListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value <> “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strcopysheet = ActiveCell.Offset(0, 4).Value strWhereToCopy = ActiveCell.Offset(0, 5).Value strStartCellColName = Mid(ActiveCell.Offset(0, 6), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Sheets(strcopysheet).Range(strCopyRange).Select Chris Nov 14 - 2:24 pm Reply Sabir- Would you mind pasting your full code? I changed some things to try to match what was working for you, but I am still getting the error message when I use “Sheets(strCopySheet).Range(strCopyRange).Select” instead of “Range(strCopyRange).Select” I think maybe the problem is something to do with the sheets not being activated…? So close, yet so far away! Nitesh Kotian Nov 21 - 12:25 pm Reply Dear Sir, I have a master sheet with 23 columns which needs to be updated on daily basis and, for this 23 cloumns i receive data from different departments, in 5 diffeent sheets, Further, 5 different sheets data are huge, so i want to know how to update my master sheet from the 5 different sheet. I want such formula that without touching my mater file it should get updated automatecially, from the data i received from different sheets. Andrew Dec 29 - 12:16 pm Reply I have to consolidate a number of separate files which will have varying number of rows, also each new data needs to be appended onto the last, so that at the end of each month I have collected all the data for that month. I intend using this to create chart. How can I change the vb code in this article to do this? Cho7tom Feb 08 - 2:11 pm Reply Thanks for this great article. I tried to adapt the code to my own needs and I encounter an error with the ‘workbooks.open()’ function. For test purposes, I wrote this sub : Sub TEST() Dim testWB As Workbook Dim strFileName As String strFileName = ActiveWorkbook.Path & “\test.xlsm” Set testWB = Workbooks.Open(strFileName) MsgBox “opened!” testWB.Close MsgBox “closed!” End Sub This sub procedure works fine the first time I launch it after having opened my Excel file. The issue appear when I launch it several time (like if I wanted to open / close the file several times), I obtain the following error : “runtime error 1004. Method open of object workbook failed”. Do you have any explaination / way to improve this TEST sub? Thank you in advance for any help! Cho7tom Kiran Apr 10 - 9:09 am Reply Iam getting Error as Userdefined Type not Defined,even though i have defined,kindly help me rectifying this Craig Harman Apr 24 - 2:55 pm Reply Hi Guy’s A quick one hopefully. I love this and have got it to work in a way but i really need help with this….. I have Supplier’s name in col A and then from Col B-M the headings are April to March. I have separate files for each month, I want to copy the data for each month when I press update to the relevant column. Each File is exactly the same but obviously all difference spend figures. Hope this makes sense and someone can help! Thank you Amr Oct 17 - 12:50 am Reply Hi Guys this is awesome, this is exactly what i want but i need to add another field List sheet for the sheet name, can anyone help Smallman Nov 02 - 11:49 pm Reply Hi All The above code in the Blog Post can be made more efficient. Here is a link to a thread on the Chandoo forum with a file to show workings. Below is the code. Sub ConsolidateDta() Dim i As Integer Dim fil As String Dim Col As String Dim cpy As String Dim ws As Worksheet Dim twb As Workbook Set ws = Sheet1 ‘ List sheet Application.DisplayAlerts = False Set twb = ThisWorkbook On Error GoTo Err ‘This is just in case a muppet mistypes a path or file name. For i = 2 To ws.Range(“B65536”).End(xlUp).Row ‘Sheet1 is MasterSheet fil = ws.Range(“C” & i) & ws.Range(“B” & i) ‘File Location plus XL name cpy = ws.Range(“D” & i) & “:” & ws.Range(“E” & i) ‘Copy Range Col = Left(ws.Range(“B” & i), 1) ‘Col to paste to Workbooks.Open fil, 0, 1 ‘Open Read Only Range(cpy).Copy twb.Sheets(ws.Range(“F” & i).Value).Cells(Rows.Count, Col).End(xlUp)(2).PasteSpecial 12 ‘Vals only ActiveWorkbook.Close False ‘Close no save Next i Application.DisplayAlerts = False Exit Sub Err: ‘Mup Mup MsgBox “The file ” & ws.Range(“b” & i) & ” is missing. Operation incomplete.” End Sub Take care Smallman Kazdima Nov 05 - 10:10 pm Reply Hi Smallman-:)) would you please send me an excel file on file e-mail with this macro? Thank you Kazdima Nov 05 - 10:13 pm Reply My e-mal: kazdima@yahoo.ca Thank you. Smallman Nov 05 - 11:14 pm Reply Hi The file is on the Chandoo Forum. Post 4 you can find the file. http://forum.chandoo.org/threads/question-about-chandoo-example-macro-consolidate-data-from-different-excel-files-vba.13033/#post-76891 Take care Smallman Pooja Dec 30 - 5:34 am Reply Hi, I am using Chandoo’s code (as below) and it is working great. But i have additional requirement on this code. I am using this code for a Dahsboard where I need to refresh and run the macro multiple times. Here each time i run the macro the new data is pasted one below another, creating duplications. Is it possible to erase the previous data and paste the new one each time I run the macro. Please help as I am stuck here. Public strFileName As String Public currentWB As Workbook Public dataWB As Workbook Public strCopyRange As String Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strLinkSheet As String Dim sheetname As String strLinkSheet = “Link” On Error GoTo ErrH Sheets(strLinkSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select lastRow = LastRowInOneColumn(strStartCellColName) Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strLinkSheet).Select ActiveCell.Offset(1, 0).Select Loop ‘activates sheet of specific name Worksheets(“Dashboard Project view”).Activate Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub Patrick Aug 27 - 8:12 am Reply Hi, This macro is great for grabbing data from different workbook. If I would like to grab data from different workbook and also different worksheet is it possible? Please help and advice. Thanks in advance. Aman Sep 11 - 3:37 pm Reply Hi, I am having trouble with this macro. The error dialog box keeps popping up. Please help me find the error. Thanks. Public strFileName As String Public currentWB As Workbook Public dataWB As Workbook Public strCopyRange As String Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strListSheet As String Dim strCopySheet As String strListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“H2”).Select Set currentWB = ActiveWorkbook Do While ActiveCell.Value “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strCopySheet = ActiveCell.Offset(0, 6).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Sheets(strCopySheet).Select Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select lastRow = LastRowInOneColumn(strStartCellColName) Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select Loop Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub Public Function LastRowInOneColumn(col) ‘Find the last used row in a Column: column A in this example Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, col).End(xlUp).Row End With LastRowInOneColumn = lastRow End Function Hui... Sep 12 - 2:32 am Reply @Aman The Code looks ok, but it may be that your data isn’t in the correct format ? Can you post the file or email it too me Rajesh Sep 16 - 12:38 pm Reply Hi… Need to extract the specific cells from different files and consolidate in single sheet. 3 Cells like A2, D2, E2 and put in consolidated file. Only one Row of data need to be fetched from each file. Can you help to share a code for that? Thanks in advance. Regards, Rajesh Misterman Nov 24 - 10:13 am Reply Hi Chandoo, Thank you for posting this. Having trouble trying to add another function in. I am trying to have the Data Range End Cell to not be set. So all the files will only have Data Range Start Cell. I have tried to add new range but it seems like I’m not understanding the scope of the ActiveWorksheets. Can anyone help with this? Thanks Wen Dec 10 - 1:52 pm Reply Dear Sir I try to use your code but find there is error 1 Red words for Dim strListSheet As StringstrListSheet = “List” 2 and Range(“B2?).Select 3 and then it said it said no this sub or function LastRowInOneColumn(strStartCellColName) the code I used from above- Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strListSheet As StringstrListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2?).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select lastRow = LastRowInOneColumn(strStartCellColName) Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select Loop Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub Smallman Dec 11 - 3:50 am Reply Hi Wen 1 – 1 Red words for Dim strListSheet As StringstrListSheet = “List” Should be; Dim strListSheet As Stringstr ListSheet = “List” 2. Range(“B2?).Select In the context of the blog post this should be; Range(“B2″).Select 3. and then it said it said no this sub or function LastRowInOneColumn(strStartCellColName) This most likely does not work because it stems from part 2 where a cell needs to be selected in the first place. strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) See the word Activecell above? If cell B2 never gets selected then I assume this part will fail. Take care Smallman Smallman Dec 11 - 4:03 am Reply Sorry I made a mistake. The first line of 1 should be this. It is a string. Dim strListSheet As String Smallman M B Sridharan Jan 21 - 12:57 pm Reply I wanted to consolidated text comments for financial variance. If i update comments for the months it should consolidated the comments in Ytd(For eg., i am working for the month May my variance 9k this is relates to volume increase till april i have around 15k it should add the 9k and display as 24k Volume variance. is it possible in Excel. kiran Feb 05 - 11:23 am Reply Hi, How can I copy a value from one excel sheet to another sheet of a different workbook. Could you please illustrate with an example. Thanks! Kiran.. Mukesh Feb 23 - 1:55 pm Reply i have used your macro and it did as i wanted, however i have small twist.. i have added a additional column for Yes or no… i want to macro to run only when it has “yes” in the row for that particular file and do nothing if has “no”. there are multiple file from which i extract my data but few files i dont want them to copy paste to master file.. Please help. Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strListSheet As String Dim strCopySheet As String Dim StrRunmacro As String Dim Cell As Range strListSheet = “Macro List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook For Each Cell In Columns(“B”).Cells.SpecialCells(xlCellTypeConstants) If Cell.Value Like “?*.?*” And _ LCase(Cells(Cell.Row, “i”).Value) = “yes” Then strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strCopySheet = ActiveCell.Offset(0, 6).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) StrRunmacro = ActiveCell.Offset(0, 7).Value Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Sheets(strCopySheet).Select Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select End If Next Cell Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub geetu May 14 - 10:18 pm Reply I am align to macros. Isit possible to include the tab name and copy rest of the information and what needs to be added if I am not sure about the data end cell range. L3g4to May 15 - 11:08 am Reply First to say – I really appreciate your site! Now on the subject: VBA is in my opinion too often the first tool most Excel users reach out to in such cases. Which is WRONG IMHO. Consolidating/joining/deduplicating workbooks/worksheets/data are typical applications for SQL. Consolidating 2 worksheets into 1 can be done as easy as in one line of SQL! See below SELECT * FROM [Sheet1$] UNION ALL SELECT * FROM [Sheet2$] And what is more you can refresh the Query with 2 clicks of the mouse and don’t need to save the file as a less-secure XLSM! What is more (OLEDB/ADODB) is something NATIVELY available and supported in Excel! I welcome you to see my AddIn which I hope which aid some in the journey of learning to use SQL in Excel. http://www.analystcave.com/excel-tools/excel-sql-add-in-free/ I intend to also elaborate more on this in my VBA tutorial: http://www.analystcave.com/tutorials/excel-vba-tutorial/#Excel_VBA_Tutorial_What_next Sandhya Oct 10 - 8:37 am Reply Hi Friends, I am using the below code i.e. consolidate the different excel files into one excel sheet, but consolidate location would start from Column G or H it will vary, kindly suggest me how to modify this code; Sub GetData() Dim strWhereToCopy As String, strStartCellColName As String Dim strListSheet As StringstrListSheet = “List” On Error GoTo ErrH Sheets(strListSheet).Select Range(“B2”).Select ‘this is the main loop, we will open the files one by one and copy their data into the masterdata sheet Set currentWB = ActiveWorkbook Do While ActiveCell.Value “” strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value strCopyRange = ActiveCell.Offset(0, 2) & “:” & ActiveCell.Offset(0, 3) strWhereToCopy = ActiveCell.Offset(0, 4).Value strStartCellColName = Mid(ActiveCell.Offset(0, 5), 2, 1) Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True Set dataWB = ActiveWorkbook Range(strCopyRange).Select Selection.Copy currentWB.Activate Sheets(strWhereToCopy).Select lastRow = LastRowInOneColumn(strStartCellColName) Cells(lastRow + 1, 1).Select Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone Application.CutCopyMode = False dataWB.Close False Sheets(strListSheet).Select ActiveCell.Offset(1, 0).Select Loop Exit Sub ErrH: MsgBox “It seems some file was missing. The data copy operation is not complete.” Exit Sub End Sub Hui... Oct 11 - 9:05 am Reply @Sandhya Can you please post the question in the Chandoo.org Forums http://forum.chandoo.org/ Please attach a file so that a specific answer can be delivered. Sandhya Oct 11 - 11:12 am Reply Thanks you soooooooooooo much for your response. It is an urgent requirement. Kindly suggest me how to post the forum, i am unable to post my code or requirement. Kindly do the needful Hui... Oct 11 - 1:40 pm Reply @Sandhya You have to register to post questions there Goto http://forum.chandoo.org/ Goto Ask an Excel Question Post New thread Type your question Attach a file Please attach a file so that a specific answer can be delivered. Leave a Reply Name* E-Mail* Website Notify me of when new comments are posted via e-mail Notify me of follow-up comments by email. Notify me of new posts by email.

लेबल:

रविवार, 11 अक्टूबर 2015

Consolidating Data from Multiple Worksheets into a Summary Worksheet in Excel Office 2007 Summary

Consolidating Data from Multiple Worksheets into a Summary Worksheet in Excel Office 2007 Summary: Microsoft Office Excel MVP Ron de Bruin provides a number of samples to merge data from multiple worksheets into one summary worksheet. After you have all the data on one worksheet, you can do things such as build a PivotTable report based on your specific criteria or use the filter options in Excel 2007 to get the results you want. (13 printed pages) Ron de Bruin, Microsoft Office Excel MVP Frank Rice, Microsoft Corporation August 2008 Applies to: Microsoft Office Excel 2007, Microsoft Office Excel 2003, Microsoft Excel 2002, Microsoft Excel 2000 Contents Overview Copying a Range from Multiple Worksheets Copying All Data Except Column Headers from Multiple Worksheets Appending Data After the Last Column in the Summary Worksheet Conclusion Additional Resources About the Authors Overview When you use workbooks that contain multiple worksheets, a common task is to roll up or consolidate the data in each worksheet into a summary worksheet. The samples described in this article add a worksheet to the active workbook and then copy a range of cells from every worksheet to the summary worksheet. The different procedures demonstrate techniques for copying varying size ranges as well as placing the data at specific locations in the summary sheet. You can download a workbook that contains the code in this article at Ron de Bruin's Web site. Note The code in the following examples use the ActiveWorkbook object to work in the active workbook. If you want to ensure that the code will work only in the workbook that contains the code, replace every instance of ActiveWorkbook with ThisWorkbook. First, you need to add functions that are common to all of the samples in this article. To add functions that are common to all samples Open a new workbook in Excel. Press Alt+F11 to open the Visual Basic Editor. On the Insert menu, click Module to add a module to the workbook. In the module window, type or paste the following functions. VB Function LastRow(sh As Worksheet) On Error Resume Next LastRow = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row On Error GoTo 0 End Function Function LastCol(sh As Worksheet) On Error Resume Next LastCol = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 End Function These two functions are used to find the last row and column, respectively, with data. Copying a Range from Multiple Worksheets In the following steps, you copy a range of data from all worksheets in a workbook and consolidate the data into a summary worksheet. To copy data from all rows in multiple worksheets Type or paste the following code into the module code window. VB Sub CopyRangeFromMultiWorksheets() Dim sh As Worksheet Dim DestSh As Worksheet Dim Last As Long Dim CopyRng As Range With Application .ScreenUpdating = False .EnableEvents = False End With ' Delete the summary sheet if it exists. Application.DisplayAlerts = False On Error Resume Next ActiveWorkbook.Worksheets("RDBMergeSheet").Delete On Error GoTo 0 Application.DisplayAlerts = True ' Add a new summary worksheet. Set DestSh = ActiveWorkbook.Worksheets.Add DestSh.Name = "RDBMergeSheet" ' Loop through all worksheets and copy the data to the ' summary worksheet. For Each sh In ActiveWorkbook.Worksheets If sh.Name <> DestSh.Name Then ' Find the last row with data on the summary worksheet. Last = LastRow(DestSh) ' Specify the range to place the data. Set CopyRng = sh.Range("A1:G1") ' Test to see whether there are enough rows in the summary ' worksheet to copy all the data. If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then MsgBox "There are not enough rows in the " & _ "summary worksheet to place the data." GoTo ExitTheSub End If ' This statement copies values and formats from each ' worksheet. CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With ' Optional: This statement will copy the sheet ' name in the H column. DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name End If Next ExitTheSub: Application.Goto DestSh.Cells(1) ' AutoFit the column width in the summary sheet. DestSh.Columns.AutoFit With Application .ScreenUpdating = True .EnableEvents = True End With End Sub Press Alt+Q to exit the Visual Basic Editor. Press Alt+F8 to run the code. The code at the beginning of this procedure (as well as the code in the samples that follow) disables screen updating so that the screen does not flicker when the code is running. It also deletes the summary worksheet RDBMergeSheet, if it exists, and then adds a new sheet to the workbook. This ensures that the data is always up-to-date after you run the code. Next, the code loops through the range on each worksheet and copies the values and formatting to the summary worksheet. Code is also included to copy the name of each worksheet to the H column in the summary worksheet. Finally, the summary worksheet is resized to fit just the inserted data. There are other options available to you to change the areas in the worksheets that you working with. Following are some changes you can make to the previous code. To copy all cells with data on the source worksheets, use the following line of code. VB Set CopyRng = sh.UsedRange To copy the current region of cell A1, use the following line of code. The current region is a range bounded by any combination of blank rows and blank columns. VB Set CopyRng = sh.Range("A1").CurrentRegion To copy a complete row, use the following line of code. VB Set CopyRng = sh.Rows("1") To copy a subset of rows, use the following line of code. This example copies rows 1 through 8. VB Set CopyRng = sh.Rows("1:8") To copy only the data without the formatting, locate the following lines in the preceding module code block. VB CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With Replace the lines with the following code. VB With CopyRng DestSh.Cells(Last + 1, "A").Resize(.Rows.Count, _ .Columns.Count).Value = .Value End With To copy all values, formatting, formulas, data validation, and comments, locate the following lines in the preceding module code block. VB CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With Replace the lines with the following code. VB CopyRng.Copy DestSh.Cells(Last + 1, "A") To copy only from worksheets with a specific name (for example, worksheets that start with the word “week”), locate the following line in the preceding module code block. VB If sh.Name <> DestSh.Name Then Replace the line with the following code. VB If LCase(Left(sh.Name, 4)) = "week" Then To copy only from the visible worksheets in your workbook, locate the following line in the preceding module code block. VB If sh.Name <> DestSh.Name Then Replace the line with the following code. VB If sh.Name <> DestSh.Name And sh.Visible = True Then To copy data from the worksheets into an array, locate the following line in the preceding module code block. VB For Each sh In ActiveWorkbook.Worksheets Replace the line with the following code. VB For Each sh In ActiveWorkbook.Sheets(Array("Sheet1", "Sheet3")) And delete the following two lines. VB If sh.Name <> DestSh.Name Then End If To include more worksheets than the summary worksheet, locate the following line in the preceding module code block. VB If sh.Name <> DestSh.Name Then Replace the line with the following code. VB If IsError(Application.Match(sh.Name, _ Array(DestSh.Name, "Total Sheet", "Menu Sheet"), 0)) Then Copying All Data Except Column Headers from Multiple Worksheets In the following steps, you copy all of the data except column headers from multiple worksheets when you copy data into the summary worksheet. To copy data from ranges without headers from multiple worksheets Type or paste the following code into the module code window. VB Sub CopyDataWithoutHeaders() Dim sh As Worksheet Dim DestSh As Worksheet Dim Last As Long Dim shLast As Long Dim CopyRng As Range Dim StartRow As Long With Application .ScreenUpdating = False .EnableEvents = False End With ' Delete the summary sheet if it exists. Application.DisplayAlerts = False On Error Resume Next ActiveWorkbook.Worksheets("RDBMergeSheet").Delete On Error GoTo 0 Application.DisplayAlerts = True ' Add a new summary worksheet. Set DestSh = ActiveWorkbook.Worksheets.Add DestSh.Name = "RDBMergeSheet" ' Fill in the start row. StartRow = 2 ' Loop through all worksheets and copy the data to the ' summary worksheet. For Each sh In ActiveWorkbook.Worksheets If sh.Name <> DestSh.Name Then ' Find the last row with data on the summary ' and source worksheets. Last = LastRow(DestSh) shLast = LastRow(sh) ' If source worksheet is not empty and if the last ' row >= StartRow, copy the range. If shLast > 0 And shLast >= StartRow Then 'Set the range that you want to copy Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast)) ' Test to see whether there are enough rows in the summary ' worksheet to copy all the data. If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then MsgBox "There are not enough rows in the " & _ "summary worksheet to place the data." GoTo ExitTheSub End If ' This statement copies values and formats. CopyRng.Copy With DestSh.Cells(Last + 1, "A") .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With End If End If Next ExitTheSub: Application.Goto DestSh.Cells(1) ' AutoFit the column width in the summary sheet. DestSh.Columns.AutoFit With Application .ScreenUpdating = True .EnableEvents = True End With End Sub Press Alt+Q to exit the Visual Basic Editor. Press Alt+F8 to run the code. This code copies all of the data from each worksheet except that the starting row in the source worksheets is set to the second row. This copies just the data, minus the column headers, to the summary worksheet. Appending Data After the Last Column in the Summary Worksheet The following procedure pastes the data from the source worksheets after the last column with data in the summary worksheet. Note Excel 2003 has a maximum of 256 columns. Excel 2007 has a maximum of 16,384 columns. To copy data from multiple worksheets and append it after the last column in a summary worksheet Type or paste the following code into the module code window. VB Sub AppendDataAfterLastColumn() Dim sh As Worksheet Dim DestSh As Worksheet Dim Last As Long Dim CopyRng As Range With Application .ScreenUpdating = False .EnableEvents = False End With ' Delete the summary worksheet if it exists. Application.DisplayAlerts = False On Error Resume Next ActiveWorkbook.Worksheets("RDBMergeSheet").Delete On Error GoTo 0 Application.DisplayAlerts = True ' Add a worksheet with the name "RDBMergeSheet" Set DestSh = ActiveWorkbook.Worksheets.Add DestSh.Name = "RDBMergeSheet" ' Loop through all worksheets and copy the data to the ' summary worksheet. For Each sh In ActiveWorkbook.Worksheets If sh.Name <> DestSh.Name Then ' Find the last column with data on the summary ' worksheet. Last = LastCol(DestSh) ' Fill in the columns that you want to copy. Set CopyRng = sh.Range("A:A") ' Test to see whether there enough rows in the summary ' worksheet to copy all the data. If Last + CopyRng.Columns.Count > DestSh.Columns.Count Then MsgBox "There are not enough columns in " & _ "the summary worksheet." GoTo ExitTheSub End If ' This statement copies values, formats, and the column width. CopyRng.Copy With DestSh.Cells(1, Last + 1) .PasteSpecial 8 ' Column width .PasteSpecial xlPasteValues .PasteSpecial xlPasteFormats Application.CutCopyMode = False End With End If Next ExitTheSub: Application.Goto DestSh.Cells(1) With Application .ScreenUpdating = True .EnableEvents = True End With End Sub Press Alt+Q to exit the Visual Basic Editor. Press Alt+F8 to run the code. This procedure determines the last column in the summary workbook that contains data and then appends the column A source data after that column. The notation A:A copies the entire column, but you can also specify a range such as A1:A10. You can use notation such as A:C to copy additional columns. To make these changes, change the following statement in the code. VB Set CopyRng = sh.Range("A:A") Conclusion In this article, you saw several code samples that you can use to merge data from all or some worksheets into one summary worksheet. Exploring and implementing these techniques into your own applications can help make your job as a developer easier and make your solutions more versatile. Additional Resources You can find more information about the techniques and methods discussed in this article at the following locations. Developers Guide to the Excel 2007 Range Object Workbook Object Worksheet Object Create a summary worksheet from all worksheets (with formulas) Microsoft Office Developer Center Excel Developer Portal About the Authors Ron de Bruin is an Excel Most Valuable Professional (MVP) and a frequent contributor to the newsgroups. For more information, see Ron's Excel Web page. Frank Rice is a programming writer and frequent contributor to the Microsoft Office Developer Center.

लेबल:

Copy Data from one Worksheet to Another in Excel VBA

Copy Data from one Worksheet to Another in Excel VBA
Previous Next
Description:

When we are dealing with many worksheet, it is a routine thing to copy data from one worksheet to another in Excel VBA. For example, we may automate a task which required to get the data from differen worksheets (some times different workbooks). In this situation, we need to copy the some part the worksheet and paste it in a target worksheet.

Copy Data from one Worksheet to Another in Excel VBA – Solution(s):

We can use Copy method of a range to copy the data from one worksheet to another worksheet.

Copy Data from one Worksheet to Another in Excel VBA – An Example

The following example will show you copying the data from one sheet to another using Excel VBA.

Code:

'In this example I am Copying the Data from Sheet1 (Source) to Sheet2 (Destination)
Sub sbCopyRangeToAnotherSheet()

'Method 1
Sheets("Sheet1").Range("A1:B10").Copy Destination:=Sheets("Sheet2").Range("E1")

'Method 2
'Copy the data
Sheets("Sheet1").Range("A1:B10").Copy
'Activate the destination worksheet
Sheets("Sheet2").Activate
'Select the target range
Range("E1").Select
'Paste in the target destination
ActiveSheet.Paste

Application.CutCopyMode = False

End Sub
Instructions:

Open an excel workbook
Enter some data in Sheet1 at A1:B10
Press Alt+F11 to open VBA Editor
Insert a Module for Insert Menu
Copy the above code and Paste in the code window
Save the file as macro enabled workbook
Press F5 to run it
Now you should see the required data (from sheet1) is copied to the target sheet (sheet2).

Explanation:

We can use two methods to copy the data:
Method 1: In this method, we do not required to activate worksheet. We have to mention the source and target range. This is the simple method to copy the data.
Method 2: In this method, we have to activate the worksheet and paste in a range of active worksheet.

The main difference between two methods is, we should know the destination worksheet name in the first method, in second method we can just activate any sheet and paste it.

Download the Example Macro Workbook:

Download the Example VBA Macro File and Explore the code:

Example file to download: http://analysistabs.com/download/copy-data-one-sheet-another/

By Valli|June 14th, 2013|Excel VBA|63 Comments
Share This Story, Choose Your Platform!

About the Author: Valli
Excel VBA Developer having around 8 years of experience in using Excel and VBA for automating the daily tasks, reports generation and dashboards preparation. Valli is sharing useful VBA examples ad Tips to helps us automating daily tasks.
Related Posts
63 Comments
Ron Brown December 4, 2013 at 10:40 PM - Reply
I am trying to copy specific ranges from 12 worksheets on from the “Source” workbook to the same specific ranges on 12 worksheets to the “Target” workbook. How can I set up a “loop” to accomplish this in a VBA code? Thanks!

– Ron –
PNRao December 4, 2013 at 11:05 PM - Reply
‘If your input files are always same then store them in an Array
mySource=Array(“File1.xlsx”,”File1.xlsx”,…,”File12.xlsx”)

‘If they are not same always then read your workbook names and store it in an Array (mySource)

'open each workbook

For iCntr=1 to 12
set sourceWB=Workbooks.Open(mySource(iCntr-1)) ' since array starts from 0
sourceWB.Sheets("YourSheetName").Range("YourRange").copy ThisWorkbook.Sheets("YourTargetSheet").Range("TargetRange")
'Example:sourceWB.Sheets("Sheet1").Range("A1:A10").copy Destination:=ThisWorkbook.Sheets("YourTargetSheet").Range("A" &iCntr*10+1)
next

Search with a keyword ‘downloads’ in our site, you will get the working file to see the code.

Hope this helps-PNRao!
Ping January 22, 2014 at 6:15 AM - Reply
How can we select user choice range and paste it in user defined range? Can we give ability to transpose the data if user wants? Do u gave any vba script that can help me with that?
PNRao January 22, 2014 at 11:13 PM - Reply
Hi,

We give the user to select a range to copy and range to paste in two dif

I am trying to copy specific ranges from 12 worksheets on from the “Source” workbook to the same specific ranges on 12 worksheets to the “Target” workbook. How can I set up a “loop” to accomplish this in a VBA code? Thanks!

– Ron –
PNRao December 4, 2013 at 11:05 PM - Reply
‘If your input files are always same then store them in an Array
mySource=Array(“File1.xlsx”,”File1.xlsx”,…,”File12.xlsx”)

‘If they are not same always then read your workbook names and store it in an Array (mySource)

'open each workbook

For iCntr=1 to 12
set sourceWB=Workbooks.Open(mySource(iCntr-1)) ' since array starts from 0
sourceWB.Sheets("YourSheetName").Range("YourRange").copy ThisWorkbook.Sheets("YourTargetSheet").Range("TargetRange")
'Example:sourceWB.Sheets("Sheet1").Range("A1:A10").copy Destination:=ThisWorkbook.Sheets("YourTargetSheet").Range("A" &iCntr*10+1)
next

Search with a keyword ‘downloads’ in our site, you will get the working file to see the code.

Hope this helps-PNRao!
Ping January 22, 2014 at 6:15 AM - Reply
How can we select user choice range and paste it in user defined range? Can we give ability to transpose the data if user wants? Do u gave any vba script that can help me with that?
PNRao January 22, 2014 at 11:13 PM - Reply
Hi,

We give the user to select a range to copy and range to paste in two different ways.

1. You can use two input boxes: one is to accept the ranges from users and other one is to choose the range to paste.
Please check this link to for advanced input box examples: http://analysistabs.com/excel-vba/inputbox-accept-values/

2.The other method is, creating userform and place two RefEdit controls. one is for to select the range to copy and the other one is the for accepting the range to paste.

So, now you know the range to copy and the range to paste the data. Then you can use, pastespecial method to transpose the data

Example: Let’s say you accepted two ranges from user and stored as rngToCopy and rngToPaste

Range("rngToCopy").Copy
Range(rngToPaste).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= False, Transpose:=True

Hope this helps.
Thanks-PNRao!
Matt May 6, 2014 at 11:36 PM - Reply
Hey! I work for a transportation company and have a workbook in which I keep track of trailers parked at a lot daily. Every day I copy the previous day’s sheet and rename to today’s date.

So I have sheet “05-05” and I copy it and rename the new sheet to “05-06”. I have been Googling this for hours now to no avail: I am trying to create a button that runs a macro that creates a copy of the active sheet, and renames it to today’s date. If I have to include the year in my sheet names that is fine. Can anyone help?
vasu April 1, 2015 at 11:07 AM - Reply
DEAR Matt,
pls explain ur need in detail. i will help you. I am also in Logistic service. Its a favour for me to help anotherf logistics man. Thank u. Vasu
Aliya May 8, 2014 at 1:46 PM - Reply
Hi there,

Could you please help me with the following:

I have two worksheets named Data and Levels. In Data worksheet I have thousands of rows and three columns ( region, ID, and sum). In Levels worksheet I have only hundreds of rows and two columns (ID and Level). What I need is to put for each row in the first sheet its corresponding level from the second sheet.

I’m only a beginner, so detailed explanation would be much appreciated,

Thank you
Josh August 13, 2014 at 12:16 AM - Reply
I am working on a macro to transfer data from one spreadsheet to another. I would like ot transfer the data to the next open row within the second spreadsheet. Basically I want to take electronic batch sheets, with data entered in them, and transfer that data to a central source for record keeping. Is this doable?

Thanks for any help you can offer.
PNRao August 17, 2014 at 11:34 AM - Reply
Hi Josh,
We can do this, every time you need to find the last row in the common file and update the data from that particular row. You can search for the examples provided to fi

लेबल:

मंगलवार, 6 अक्टूबर 2015

Excel farmulas

How to get the day of the week as text in Excel 2010
january 4, 2013 by admin
Excel WEEKDAY function will return the day of the week as a number. See details here:

How-to-get-the-day-of-the-week-in-excel-2010

To return the day of the week as text (name of the day), we combine the WEEKDAY function with the TEXT function as follows:

TEXT(WEEKDAY(A1;2);”ddd”) will give the abbreviated name of the day : Sun , Mon, Tue … etc.

TEXT(WEEKDAY(A1;2);”dddd”) will give the full name of the day : Sunday , Monday … etc.

Examples:

Cell A1 have the date: 25/11/2012, which is Sunday.

1. Cell C1 have the formula : =TEXT(WEEKDAY(A1;1);”ddd”). The value in C1 will be “Sun”

2. Cell C1 have the formula : =TEXT(WEEKDAY(A1;17);”ddd”). The value in C1 will be “Sun”

3. Cell C1 have the formula : =TEXT(WEEKDAY(A1;2);”ddd”). The value in C1 will be “Sat”

Why the returned day of the week is “Sat” instead of “Sun” in example No. 3 above? This is because the TEXT function will always assume 1 as Sunday, 2 as Monday and so on. This is only compatible with WEEKDAY return type of 1 and 17. So you have to be careful if you are using a return type other than these two. You have to use some arithmetic to get the correct name of the day in this case.

filed under: excel 2007, excel 2010, formulas, functions tagged with: excel 2007, excel 2010, excel formulas, excel functions
How to change the range of a defined name in Excel 2010
december 5, 2012 by admin
In a previous post I explained how to define named ranges in Excel 2010.

OK, you have defined a name for a specified range. Later on you decided to change the size of that range either by expanding it or reducing it. Here is how you do it:

1. In the Formulas tab, Defined Names group click Name Manager. The Name Manager window will open. It will list all your defined names.

2. Select the name you want to change its range then click Edit. The Edit Name window will open.

3. Click in the Refers To edit box. Now you can enter the new range through the keyboard, or you can highlight your range using the mouse and EXCEL will update the range for you automatically.

4. Click OK.

filed under: excel 2007, excel 2010, formulas tagged with: excel 2007, excel 2010, formulas
How to define named ranges in Excel 2010
november 25, 2012 by admin
If you have a range of cells that hold a specific type of data e.g. Sales, No. of students etc. then you can give that range a meaningful name. Later if you want to refer to the range you can do that by specifying its name instead of using its cell reference.

In the example below we have two columns, Month (column A) and Sales Value (column B).

If we want to sum the total sales for the 12 months then we will use this formula:

=sum(B2:B13)
Alternatively we can give the range B2:B13 a name like this “SalesValue” and then use the sum formula as follows:

=sum(SalesValue)
The names will make it easier for you to refer to a range and to make your formulas more meaningful.

How do you define a named range?

1. Select the range that you want to name.

2. In the Formulas tab, Defined Names group click Define Name. The New Name window will open.

3. In the Name field enter the name you want to give to your range.

4. In the Scope combo box, if you want this name to be known to other sheets in the workbook, select Workbook. If the name will only be used in the current worksheet, then select the name of the worksheet.

5. In the Refers To field make sure that the required range is specified.

6. Click OK when you are done. Your defined name is now ready to be used in any formula.

filed under: excel 2007, excel 2010, formulas tagged with: excel 2007, excel 2010, formulas
How to calculate age in Excel 2010
november 16, 2012 by admin
To calculate a person (or anything else) age you basically subtract his date of birth from today’s date. However which date functions to use depends on the amount of precision and the output format you want.

Suppose t

लेबल: