Importing data into a master worksheet - vba

I have a doubt about my program to the VBA/EXCEL
I made a file called "MASTER WORKSHEET" with various data from a client, my goal is: To create a VBA to open any file in Excel and that I import chosen cells to fill out all data of the "MASTER WORKSHEET", for example, all excel file I open with the button "IMPORT DATA", it will automatically capture only those cells that I choose, but my program it captures but is with configuration problems on VALUE or NUMBER.

If your question is simply "how do I copy a cell from one place to another", an example would be:
Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Copy(Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4"))
If you just want to copy the value, without the formula and formatting, you could use
Workbooks("Master Workbook").Worksheets("Master Worksheet").Range("A4").Value = Workbooks("Input Workbook").Worksheets("Input Worksheet").Range("J1").Value
Obviously, the workbook, sheet, and range names need to be changed to your particular situation.

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

Point to the Excel sheet where the Macro actually stored

I have a sheet named "State List Generation" in which I have written a code which can extract City and State list from some text. I wanted to run this macro in another sheet which contains the text. I will be able to run this through macro window of excel if the first file is open. The problem is my state and city list is in the first sheet (State List Generation). When i run the macro from the second sheet which has data, the second sheet becomes the active sheet!!. I am not able to point the first excel where the the actual macro is present. Is there any way??
I have found the answer from https://support.microsoft.com/en-us/help/291308/how-to-select-cells-ranges-by-using-visual-basic-procedures-in-excel
I have just used Workbooks("FT_State_update.xlsm") to point the particular workbook. Thanks to Maddy Nikam

Excel sheet Deletes the formulas present in the sheet when I open it. How to avoid this?

I'm uploading an excel file that contains sheets, to my server which encodes to base 64 so I decode it as required and process it by adding data in sheet 5 as column1 and column2 with certain number of rows. At the time of uploading, this sheet has some specific formulas on sheet 5 that makes changes in other sheets. So on opening the file which I send as response after editing from server, There comes this prompt that reads
"Excel Found unreadable content in 'MyDownloadedExcelData.xlsx'. Do you want to recover the contents of this workbook?If you trust the source of this workbook, click Yes', with Yes and no buttons
and when I click on yes and open the sheet, all the formulas are deleted.
I see something like
Excel was able to open the file by repairing ot removing the unreadable content.
Removed Records :Formula from /xl/calcChain.xml Part
Repaired Records : Cell Information from /xl/worksheets/sheet1.xml part etc
So, How do I make sure my formulas in the sheet are retained?
Using VBA you could have an on close event that pastes values and an on open event that recreates the formulas. Your file would essentially save with static data, but then be used with functions intact.
If this solution is of interest I can help provide some coding framework.

Excel Reference External Sheet

An excel question for you gurus. I've tried searching high and low and haven't come up with an effective solution.
I'm trying to create a formula that will lookup a value in an external sheet. I'm using the SUMPRODUCT formula and it works perfectly. Formula is below:
=SUMPRODUCT(--('File\Path\[file.xlsx]SheetName!$D$1:$D$1000=$B3), --('File\Path\[file.xlsx]SheetName'!$O$1:$O$1000=$A3), 'File\Path\[file.xlsx]SheetName'!$Q$1:$Q$1000)
The issue I'm running into, however, is that the source file is updated every day. Although the workbook name stays the same, the sheet name changes. A random string gets assigned to the source sheet name each time it is updated. As such SheetName becomes SheetName ase341.
Is there a way to have the formula read the external sheet number instead of the name? I want the formula to update regardless of the sheet name. If there's no way to read the sheet position is there a way to change the sheet name via a formula in an external workbook?
Usage Example
I have a workbook (analysis) and it pulls data from another workbook (source). Source is updated every day with new data. The data in Source is updated by downloading a report from the internet and saving over the old source file. As such, the file name stays the same but whatever is inside the file is always different (including the sheet name). There is always only ever one sheet in the Source with the same number of columns, always in the same position.
There is a really neat way to refer to a block of cells in an external workbook in which the sheetname or even the block address may vary. Say we have:
=SUM('C:\Users\James\Desktop\[Book1.xlsx]Sheet1'!$B$2:$B$9)
however the sheetname may vary. First assign a Defined Name to the block in Book1 (say XXX)
Then we can use:
=SUM('C:\Users\James\Desktop\Book1.xlsx'!XXX)
It does not matter if the sheetname changes, the Defined Name will change with it!
Your issue would be most efficiently solved with VBA, but if you're just getting started this might not be the best route.
You can get the sheetname or filename with just a formula, though:
http://www.ozgrid.com/VBA/return-sheet-name.htm

Update hyperlinks in shapes when file location changes

I have a process flow diagram that uses various excel shapes to visually represent a data production process from start to finish, I.e. from data input to analytic environment to data output to submission file. I have used vba to hyperlink many of the shapes in the diagram to another sheet in the workbook (using thisworkbook.fullname) that contains definitions for abbreviations contained in the text of each shape, eg C1 is listed in a shape, the hyperlink takes you to the definition tab cell where C1 is defined as control point one. The hyperlinks work when the xlsm workbook is in my home location where i saved the file but they do not work if I save the file to another location (they try to open my original workbook). Is it as easy as changing the hyperlink addresses to thisworkbook.filename and dropping the path to make this work? do i need to create a macro that will automatically look up the old hyperlink address and replace it with the new address of current file location for every shape in the workbook containing a hyperlink. The path could change in the future, so want it to be relative and not fixed - for example if I save the file to share point and another user saves a copy to their home directory, I still want the links to work for them in either location. Some hyperlinks go to "sheet1" some to "sheet2" for example, but sheets 1 & 2 are both located in the same workbook. Please help!
ActiveWorkbook.Name solves the problem, replace ThisWorkbook.FullName with it and hyperlink works in other directories.