Extract data from another workbook using VBA - vba

I have two workbooks.
In Workbook A, I want to find the value in cell A11 (sheet 'Data') in column A of Workbook B and then return the corresponding value in column H. THis value must be pasted into Cell C11 in Workbook A.
Workbook B must be opened and closed to extract this data without the user having to manually open it.
How do I use a VBA code to achieve this? Preferably variables are built in based on values in workbook A e.g. path, file name, worksheet name, column A in above example and column H in above example).
Link to Workbook A: https://www.dropbox.com/s/e6ixr5ky4i2r6vw/Data%20extraction.xlsm?dl=0

I'm using MS Office Excel Professional Plus 2010. As long as the user won't be storing file B where he can't access it again, Excel can manage this without the need for VBA.
In cell C11 in workbook A type "=" to begin writing a formula. Now switch windows to workbook B and click cell A11 on sheet 'Data'. Hit 'Enter' on your keyboard... the computer should return to workbook A.
The formula in C11 now should show the filename of workbook B and the cell reference.
Now's when the magic starts: Excel has made this formula into a "Data connection" and will work behind the scenes to keep this cell updated. It may give you a yellow bar asking to activate automatic data updates, which you should accept.
Test and see if this method satisfies your requirement. If workbook B is stored in a shared location, do more testing to see if that doesn't mess it up.
Good luck!

Related

VBA - Pushing data from one workbook to another, irrespective of file names

Been in a pickle for a while (week or so) here and was hoping someone in this magical community could help me out. There is likely a very easy solution for an experienced individual, which I am not.
First, my goal is as follows: Push data from Workbook A to Workbook B via macro.
Conditions:
Workbook A must be able to be renamed without compromising the macro (it is a tool used in day-to-day functions and saved as a new name each use). Workbook A holds the macros.
Workbook B receives the data. Its name will also change with time, but in this case it needs to be based off written text in a cell from Workbook A (name change about yearly due to versioning) Let's just call it Cell A1 for argument's sake.
There may be additional workbooks open at the same time, related or unrelated.
To keep it simple, I will just post one line in my current macro, but I will be applying this to dozens. This works when I do not rename the files. I likely need help defining variables (strings?) and direction in what functions to use.
Windows("Workbook_B 4.7.5.xls").Activate
'*Workbook B name will ideally be derived from a cell value in Workbook A*
Range("V12") = "='[Workbook_A V1.2.5 .xlsm]SHEET_A '!R8C7"
In this chunk, the goal is to activate Workbook B and copy the value (or formula if it is easier), from cell G8 on Workbook A Sheet A to Workbook B(sheet is already active and sheet names will never change in either workbook).
Cheers!
"Workbook A holds the macros" - in this case you would use
ThisWorkbook
to refer to Workbook A
...and
Workbooks(ThisWorkbook.Sheets("Sheet1").Range("A1").Value)
to refer to Workbook B

vlookup not finding vba cells

I have a table that uses a bit of vba to populate column b with id numbers.
I then have column c as a vlookup that gets a name based off the id, from another sheet. The vlookup in the first cell works fine and returns the correct name, John Doe.
When I drag down, the rest of the cells in column c return the same name as the first, John Doe. The vlookup in the other cells is exactly the same, except the reference cell does change, as expected....so, c2 = vlookup(b2, $range, col, false), c3= vlookup(b3..), c4=vlookup(b4,..), etc.
The catch is, when I look at the vlookup in c3 and click on b3, the cell changes to find the correct name (no longer John Doe). So it works fine. And I have to do that for every cell in column c.
It's like vlookup isn't aware that column b changed? Is that something that happens with vba? Is there a refresh command or some other way for vlookup to register that column b has changed without having to click on each individual vlookup function?
If you've entered a formula in a cell, then copied the formula to another cell and Excel hasn't updated your results, that means that Calculation Mode is Manual.
To fix it (depending on version, this is for Excel 2010)
Click on Formulas in the Ribbon
Click on Calculation Options
Click on Automatic
If you need to have it calculate manually (valuable for making many formula changes in a large worksheet):
Click on Formulas in the Ribbon
Click on Calculate Now to calculate the entire workbook, or
Click on Calculate Sheet to calculate the current worksheet
You can skip all the clicking by pressing F9 to Calculate Now or Shift-F9 to Calculate Sheet.

I need to generate new workbooks using existing sheets that I will autopop with data

I have two sheets on my template. Sheet one contains a SKU in column A and a descr. in column B
On sheet 2, I have a pre-written template that has generic SKU and Description. That means under the item sku, it says D999 and under the description it says xxx.
I want to replace "999" with the value in A2 on my first sheet, but the 999 is in multiple columns through the sheet. Next, I want to replace the "xxx" with the data in B2 from sheet 1. I can't seem to find a VBA code that will do this specifically.
Lastly, once the find and replaces are done, I need to have it save me a new workbook using sheet2, in the same format, and name it as the value in A4 on sheet 2. I think you should be able to find the workbook here My Workbook
Basically, I want to be able to enter a bulk amount of new SKUs and Description, and be able to generate a new workbook for each SKU I enter. Right now I have 78 waiting to have sheets made.
In the cell to the right of the cells where you want to type in the SKU, add the following formula: =Match(A2, LookupsheetName!B:B,0). This will return the row the SKU was found on. In the next cell to the right use the Index function: =Index(LookupSheetName!A1:B1000, B2, 0). This will bring the label in column B to the current page. The Match function locates the row the SKU was found on.
Try these for looking up the information, because they are more efficient than VLookups.
The code for copying a worksheet to another workbook can be created using the Macro recorder on the Developer tab. Turn the recorder on then do the steps to manually move/copy to another workbook. When done, open the VBA editor and figure out how to save the workbook to a new name each time.
Good luck.

Hyperlink after Vlookup

This simple thing is eating my brain out. Here is the challenge:
I have 2 sheets in the same workbook. Say Sheet 1 and Sheet 2. I want to Vlookup for the value present in Sheet 1 Column A and Sheet 2 column B. If the value is found i want to put a hyperlink in Sheet 1 Column A to point it to Sheet 2 Column D. In simple words, there are program names present in both the sheets and associated risk in sheet 2. If the program is present in both the sheets then i want to navigate directly to the risk present in the Sheet 2 upon clicking the program name in Sheet1.
I am attaching the sample file with this thread. Please somebody help me. I have tried with vba and normal formulas. Nothing is being worked out.
SAmple file can be downloaded from here
Here's how you can do that with a formula. You'll need to change the workbook name in the HYPERLINK formula to match yours.
=IF(ISERROR(VLOOKUP($A1,Sheet2!$B:$B,1,0)),"",HYPERLINK("[StackTest.xlsx]'Sheet2'!D"&TEXT(MATCH($A1,Sheet2!$B:$B,0),"#"),"Click Here"))

Copy and paste between workbooks

This is a question and may or may not be achievable but any recommendation is much appreciated.
Is it possible to have one workbook (wrkbookA) open that has all the data and vba code in it. Click button to open/navigate to a directory and select another workbook (wrkbookB). wrkbookB is opened, select a column, that column is copied and pasted into wrkbookA in column A and wrkbookB is closed.
I know this can be achieved using a constant column from wrkbookB but the data is in differnet columns in each workbook in wrkbookB.
I have also looked at column headings and doing the copy that way but it is not an option as there are different columns with similar heading/words.
Thanks everyone.
Since you don't have any way of knowing which Column will need to be selected from workbook B, you're going to need user interaction. that being the case, why not write a sub that will do the following:
Have user select workbook B to open
Place a button in Workbook B attached to code in Workbook A
User then, in Workbook B, selects the column they want copied and push the button which copies that column to Workbook A and closes Workbook B without saving changes.
Sample Code Idea:
Workbooks.Open ...
ActiveSheet.Buttons.Add(427.5, 12.75, 104.25, 36).OnAction = "WorkBookA!MoveColumn"