Excel VBA Job : Updation of master sheet - vba

Scenario :
I had 2 workbooks, First one is a "master" workbook and other one is a "servant" workbook or in other terms "servant" workbook is a subset of "master" workbook, both workbooks contain a column known as "Commodity", "master" workbook contains exhaustive list of all items where "servant" workbook has few of those items, but it contains new updated values corresponding to those few items.
Job given to me :
I had to manually first pick the commodity from "servant" workbook find that "commodity" in "master" workbook and update the master workbook with the new values which are populated in "servant" workbook.
Why I had to find each commodity manually is because sorting of data in both sheets doesn't match.
My Code :
Sub Macro4()
'
' Macro4 Macro
'
Windows("test2.xlsx").Activate
Range("A2:A15").Select
Selection.Copy
Windows("Test.xlsx").Activate
Range("B2:B15").Select
Windows("test2.xlsx").Activate
Range("B2:B15").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Test.xlsx").Activate
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub

I think I understand what you seek but I do not believe anyone could give it to you.
What you are asking for appears to be a very simple VBA macro. If you do not know enough VBA to write that macro, I do not believe you would understand it if it was given to you.
You must learn the basics. Search the web for “Excel VBA tutorial”. There are many to choose from so try a few and complete the one that matches your learning style. I prefer books. I visited a good library, reviewed their Excel VBA Primers and borrowed those I preferred to try at home. Finally I bought the one I liked the best as a permanent reference book.
I do not know how quickly you will absorb the key concepts of programming in Excel VBA. Some find those concepts obvious while for others programming is an alien world where they will never be happy. I suggest you invest half-a-day in studying VBA. If you are finding VBA reasonably easy to understand then start thinking about your macro.
The code you present has been created by the Excel Macro Recorder. The macro recorder is a great way of identifying the VBA statements corresponding to a keyboard function but it is not a good way to learn VBA. It will not help much with your macro.
The tutorial should explain the difference between “workbook” and “worksheet”. A workbook is a file saved to disk. “test2.xlsx” and “Test.xlsx” are workbooks. A workbook will contain one or more worksheets. A worksheet contains row and columns. In your question you use “workbook” for both workbooks and worksheets.
You will need to design your macro. For a simple macro, the easiest approach if often to record how you would approach the task manually:
Open master workbook.
Open servant workbook.
Within Servant workbook look at worksheet containing Commodity column.
For each entry in this column:
Search for matching entry in Community column of master workbook.
If matching entry found
Copy select values from servant row to master row
If matching entry not found
Record error or take corrective action.
Next
Save master workbook.
Assuming I have understood your question, the above list is the structure of your macro. Most lines in the list correspond to one, or a few, simple VBA statements that you should have learnt from your tutorial.
In your macro you have already opened the master and servant workbooks. A VBA macro in one workbook can open other workbooks. Once you have a little experience, you will probably find this the most convenient approach. However, I suggest you leave multi-workbook macros to stage 2. I suggest you create a development workbook containing a copy of the master worksheet and the servant worksheet. Get your macro working within that single workbook first then update for multiple workbooks. I usually find it best to have macros in a workbook not assessable by the regular users. This macro will open both the master and servant workbooks.
I hope this gives you a start. Most people do not find programming in Excel VBA too difficult once they have mastered the basics. However, trying to find snippets of code on the web before you have mastered those basics is at best difficult and slow and often impossible.

Related

collate multiple openpyxl worksheets into a different workbook?

I've written some code that runs a number of simulations (a few dozen), and for each run of the sim it builds an openpyxl worksheet with the result data. I'm having trouble figuring out an effective mechanism to put each of these worksheets back together into a single workbook.
I found Copy whole worksheet with openpyxl -- which is disheartening as it led me down a road to probably not being able to "just copy a worksheet as an object into a new workbook".
Are there other mechanisms that might work here? Is it even legal (sensible?) to pass just a worksheet object around from one function to another, or is this totally barking up the wrong tree?
I suppose I could instead create a whole separate workbook in each simulation run, and then collect all those workbooks, and then for each one do a brute-force copy of the data in each worksheet over to a new worksheet in the main workbook... but that just SEEMS like the wrong way to approach it. Is there something more elegant that I haven't considered?

Copying multiple results based on a drop-down list

I'm pretty new to creating Macros in Excel and using VBA.
Let's say I have a model based on a drop-down list. Based on the item selected from that list, model analyses according data and creates results. My point is to have these results compared for all the components from the list, in another sheet. The problem is that list includes around 200 item numbers.
What I need my macro to do is:
Choose the item number from the drop-down list in cell A1 in the "Sheet1"
Copy the range A1:E2 in the "Sheet1"
Paste the results to the Range A1:E1 in "Sheet2"
Go back to "Sheet1" and choose next item from the list
Copy the same range
Paste it to "Sheet2" one row below the previous one (creating a list)
And repeat the action until the last item on the list is used.
Does anybody have any advises how to write this macro down?
The task is by far exceeding my current programming skills. I searched everywhere, but couldn't find anything what would guide me into solution.
I implemented what you want on an Excel workbook with two sheets. Download it here:
http://s000.tinyupload.com/?file_id=05985444391023940738
Try this. Does it do what you need?

Excel: How to compare sheets from 2 different workbooks for differences

I have an original excel file that I have ran a simulation that inputs financial data. I made a copy of this file, and wired the formulas up differently to try and increase calculation performances.
I now have 2 workbooks, the original and the final. I want to compare each sheet from each of the workbooks together to make sure that the financial numbers have remained the same, to make sure the new formulas are not effecting the numbers received.
I have tried to put copies of the two sheets into one workbook, name them April12 and April15. Then insert a third sheet. In cell A1 of the third sheet, I wanted to use the formula
=April12!A1=April15!A1
to get TRUE/FALSE values. But the formulas in these sheets reference many other sheets that are not in this new workbook, so all of my numbers turn up as #REF.
Iv googled many different ways of approaching this but I cant seem to get any of them to work. Does anyone know a simple way I can compare just the values from 2 sheets from 2 different workbooks to find out if the numbers have remained the same or have changed?
Note:I am using excel 2010.
I think you already know how to verify data using formula so is the problem to refer to a row in a different workbook ? if so, following might be helpful :
=[yourFile.xls]SheetName!$Col$Row
this way you can update your formula like(yourFile.xls refers to the complete path including the file name) :
=[file1.xls]April12!A1=[file2.xls]April15!A1

VBA script to copy specific rows in Excel from a given workbook to a new workbook if specific value is found in a given column

I have been looking for a solution to my problem for the past 48 hours and I couldn't find anything. Keep in mind I have very basic programming knowledge and I might have overlooked something to fit my needs without understanding the code behind it.
I have an excel workbook with a sheet containing all of the data.
It's a list of shipments going in and out of a port.
Now when something is loaded, a "Date Loaded" cell is populated with the date automatically.
What I need for every row that has a blank "Date Loaded" cell to be copied to a new workbook which will be named by the date it was generated (i.e.:031814-PM) depending on the time of the day, PM could be AM.
Now I might not need all the columns to be copied to the new workbook. I need a way to exclude those columns during the copying process.
I want the VBA script to be called with a button that will be at the top of the data worksheet so that when we click on it, the new workbook is generated.

Copy Excel worksheets, macros, and graphs from one workbook to another, moving links to the new workbook

I have an Excel workbook with a number of features:
One main user-facing sheet
One summary sheet based on the user-facing sheet's data
A number of graphs based on the user-facing sheet's data (as in, the type of graphs with a separate tab for them, rather than objects within a worksheet - I'm not sure if they have a special name or special properties)
A series of 'background' worksheets that calculate the values for the user-facing sheet
A macro to allow the user to sort the user-sheet by any column they wish, which is referenced in the user-facing sheet's Worksheet_SelectionChange event
However, for distribution I'd like to cull the sheets for simplicity (and file size - the entire data query is included on one of the sheets). I still need to calculate the values for the user-facing sheet, but it's only done once per dataset, so that can quite happily be copied as formatting then values.
The trouble, however, is transferring the dependent sheet, graphs and macros across to a new workbook so that instead of referencing the old workbook, they reference the new versions of the sheet. Ideally I'd like to do this with VBA or something, but my Google searches thus far don't seem to have turned up much of relevance.
Does anyone know how to do this?
I'm not sure I entirely understand your question, but what I think that you want to do is to create a new version of your workbook, with less worksheets in it??
To do that in VBA, this code snippet is a good place to start:
Sub Macro1()
ActiveWorkbook.SaveAs Filename:= _
[your path here]\Book2.xls", FileFormat:=xlNormal, _
Sheets("Sheet2").Select
ActiveWindow.SelectedSheets.Delete
End Sub
Creating a copy of the entire workbook, and then deleting what you don't need will make sure that the links do not reference the old workbook.
Once you have created the new workbook, with links intact, it is then pretty easy to remove all the things (calculations, etc.) that you don't need.