Copy and paste between workbooks - vba

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"

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

Extract data from another workbook using 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!

Transferring information from input sheet to master sheet

I am trying to move data from one workbook 'input worksheet' to another workbook 'master workbook'. Both sheets are in the same file and if possible, it would be great if both files didn't have to be open at the same time in order to transfer the data but the master workbook would autosave once the data was transferred across. Links to images of the files below to make it easier to understand what I am trying to do.
The data in the input worksheet is in row 6, columns A-J with each user inputting details of the tasks they get asked to complete. I would like when a button is clicked, the data from the input worksheet is transferred into row 2, columns B-K in the master workbook so that each time a new task is entered and transferred across, it appears in the row below (so that it can be pivoted later, etc.).
http://i.stack.imgur.com/b2cyI.jpg - input sheet
http://i.stack.imgur.com/JZr0a.jpg - master sheet
Use the macros here to get the last row in the master sheet.
Then simply write the values from the input sheet to the corresponding cell in the master sheet.
That is all. This is how you refer cells:
tbl_master.cells(1,3) = tbl_input.cells(3,5).value
Make sure that the row in the tbl_input is a variable, coming from the function, calculating the last row. Give it a try!
Edit:
This is what I use for last row:
Public Function last_row_with_data(ByVal lng_column_number As Long, shCurrent As Variant) As Long
last_row_with_data = shCurrent.Cells(Rows.Count, lng_column_number).End(xlUp).Row
End Function
If you want to find the last row of column B of sheet "tbl_main" you call it like this:
last_row_with_data(2,tbl_main)
Edit2:
Change the names of your sheets here, and reference them by their names.
In order to get this window, select the sheet on the left and press F4.

Update master workbook with another workbook

There is lots of questions about copying the content of one workbook to another that I'm aware of.
I would like to make a code that updates the workbook by importing the content from the other workbook without rewriting cells, just adding values. I want to make it that way in case the other workbook is corrupted or have some data deleted.
Some rows can be partially filled but the macro will fit the correct information in the right row.
Have you considered trying to copy over the workbook as a different name?
Imagine you start with
MasterWB -> sheet1
ChildWB -> sheet1
You copy over such that you have this worksheets.
MasterWB -> sheet1 , sheet2(childbook)
From there it is pretty easy to do a concat on a third page. First create a third sheet. Next, concat the values on sheet3 with values from sheet2 and 1
MasterWB.sheet3.cellA1.value = concat(sheet2.cellA1.value, sheet1.cellA1.value)
Sorry for the bad syntax, hope this helps!

Bind a cell in one Excel sheet to a cell in another

Say I had two Excel sheets (ONE.xlsx & TWO.xlsx). Would it be possible to have a certain cell in ONE to be changed at the same time as a certain cell in TWO?
For example:
B2 in ONE is equal to "Tuesday". Is there a way to make D10 in TWO also equal to "Tuesday" without having to go into TWO and manually changing it.
The more I think about this problem the more I think that it's not likley to work. The only way I can think of it working is by connecting them both up to the same database somehow. Thought I'd try you guys before I give up try something else.
Thanks in advance!
I've found a solution that works very well.
Open both the workbooks.
In the source workbook, copy the cell(s) that you wish to link.
In the destination workbook, right click and select "Paste Link".
The cells in the destination workbook should now be linked so that when changes are made to the source workbook the changes are also made in the destination workbook.
Sure, open both workbooks.
put = in workbook 2 minimize and find the sheet/cell in workbook one
select it.
close workbook 2 and save.
change the value in workbook 1
open workbook to (enable the security to look at workbook 1)
see the updated value.