Reading from a series of Excel Workbooks - vba

Lets say that I would like to read something from one Excel workbook, into another. This could be done typing
='C:\Documents\[MyOtherExcelFile]Sheet1'!A1
But suppose that I dont have a single file, but rather 100 (or 1.000) other Excel Workbooks, each from which I need a few values.
Assuming that they are named systematically like this:
C:\Doecuments\ExcelSeriesFile_0001.xlsx
C:\Doecuments\ExcelSeriesFile_0002.xlsx
C:\Doecuments\ExcelSeriesFile_0003.xlsx
(...)
C:\Doecuments\ExcelSeriesFile_1000.xlsx
Is there any way that I can automaticcaly create the name of the file, in the read command, getting the four digit number from a cell value in my original sheet?
Preferably I would like to do this without VBA, but if this is needed (or if it simply makes it easier/more effective) then VBA is okay.
Thanks in advance!

Related

Formatting Data in excel sheet with blue prism

I'm trying to run a duplicate check In which varying data is pulled from a website and compared to a master list, the master list being stored in Excel. The information from the website is read from a table in which has line breaks. These breaks are translated over to the data collection they are initially stored in. Some of the data from the website us eventually written to the master list in Excel. So when I read the master list back into Blue Prism to run a duplicate check, the rows that have line breaks are written into a collection as multiple rows (ex. I should have on 7 rows in my collections but am getting 42). Since the rows are not EXACTLY the same between the 2 collections, when it runs the automation does not recognize the duplicates.
The easiest way to solve this would be if I could make the collection rows have no line breaks as soon as the data is read. I've attempted to use the calculation stage to do so with no luck. I'm not sure if it is actually possible to do this, but would appreciate any direction.
Record an Excel macro to do the data sorting/cleaning in Excel (possibly Text To Columns, etc..) and then include the running of the macro as part of your Blue Prism process by using an action stage and the MS Excel VBO - Run Macro. Get the process to create an Excel instance (and create a handle data item from that stage), then use Open Workbook (whatever workbook you store your Macro in) and then use the MS Excel VBO - Run Macro (use the same handle created earlier and type in the name of the "macro").
It sounds like what is happening is that the MS Excel VBO is grabbing the data from the Excel Worksheet wholesale.
This is to say that it's accessing your Worksheet table, copying the cell values BUT not the cell formatting data, and then dumping the values into a BP collection.
Since it did not bring along any of the original cell formatting data to reference when it went to populate the collection it's just breaking up the values based on crturn/line breaks. Thus, your collection is organized based on that, and not on the original Worksheet cell.
So, with that said, on to a solution!
Solution 1
Brute force the organization of the incoming Excel cell data to the collection by looping over the Excel Worksheet cell-by-cell.
Run a loop, and in that loop have BP go into the Excel Worksheet and grab the first populated cell it comes across. Run a formatting/cleanup Calculation stage over the data. Dump the cell value into a single collection field.
Repeat.
This is...inelegant, expensive at best, and not at all recommended for any medium to large dataset. But it's definitely the best way to do string manipulation and value comparisons before it hits your collection. Since it sounds like your using a Master template then you as-well know what the expected format of your data should be.
This method will enable you implement Trim(), Concat(), or Split() in a Calculation stage to better organize your incoming data before you dump it into a collection.
This is also basically what I think you're already trying to do, but cell-by-cell instead of Worksheet row-by-row or table-by-table.
Solution 2
Clean up the table data you grab from the website before you dump it into the Excel Worksheet.
This is basically Solution 1, but in reverse. Simply format/cleanup your data before it hits you Excel Worksheet.
I'm not sure this is any better than Solution 1, but, you know, it's something...
Solution 3
Format the cell data IN the MS Excel Worksheet itself.
Basically rearrange the cells and cell data in the Excel Worksheet into a more predictable format by using the Split, Trim, Merge, or other actions included in the MS Excel VBO. You can also do this using the Data - OLEDB utility object, but that requires some pretty solid understanding of SQL syntax.
This would look like this using the MS Excel VBO:
Grab the Excel Worksheet data wholesale and dump into a collection
Count the rows/fields of the collection
Is that number consistent with the desired/expected format of your data?
If not, have the bot go back into the Excel Worksheet and reformat the cells by removing any carriage returns/line breaks/whatever else
Repeat.
However, I'm always reluctant to reformat any original source, as it's then hard to figure out what wrong and where it went wrong when you've changed the original structure of your data. So it's best to always make a copy of the Worksheet before you make any manipulation.
Unfortunately I don't have access to my BP environment at the moment or I'd provide you with the act object actions you'd need to do any of this, my bad. Once I do I'll update this answer.

How to automatically convert text-based numbers to numbers in several files?

I have 1000 files in Excel format (Excel 2010) and each file contains 7 sheets with data.
This is an example for Excel sheet.
Is there a way to automatically convert the numbers that are stored as text to numbers, without affecting the actual text data? (maybe by VBA macros ? but I am a beginner in VBA code)
I can give you an algorithm, but I don't have time to write the entire code. I would write it in a seperate workbook, for repeatability. You can then either hardcode the 7 file names, or you can make an input for file name (the latter is a bit more flexible, if you need to use it for more files later).
Open a file
Loop all sheets
Loop all cells
If IsNumeric(Cell.Value) > Change format
You should be able to Google your way to the separate parts. Once you have some actual code, you can ask for more help on StackOverflow.

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

Output all cells in a named range in Excel?

I have a named range in Excel (called JourneyReference) that contains data to do with common journeys employees make (travel time, distance, etc). I would like to output all of the cells from this named range on several sheets.
The reason I am looking at doing it this way is because the named range will be updated regularly, and thus I'd like the changes in it to propagate through to all the sheets.
Is this possible in Excel 2007? Or, if there is perhaps a better way of doing this, please let me know. Thanks.

Copying/Mapping data between excel spread sheets, Vb.net

I need to copy data using Vb.net if possible from one excel spreadworbook to another and place the data into the correct columns in the existing excel spreadsheet. The column titles of the spreadsheets match up, I have several templates I need to place data into and the order of the columns is different in each template so I need a way of searching for a column header in the template and then copying the data into that column.
Would the best way of achieving this using ADO?
For example move the data from this Workbook1 with columns "Test1", "Test2" and data
Test1 Test2
1 2
12 23
123 234
Into workbook 2 which will have the same column names but could be in a different order:
Test0 Test1 Test1.1 Test2
I need to do this automatically as I have alot of data to copy and 30-40 workbook templates to copy the data into, the templates columns are in different orders and can not be moved around.
There are different ways to interface with Excel using .NET. If you are just looking to do it with one version of Excel, then VSTO might be your easiest solution, otherwise use something else. I like to use EXCEL-DNA.
You can also use ADO to get the data out, but to put it in another one, I would think you would need one of the ways listed above to do it (since you would need to reference the excel object). If you are using Excel 2007 and above you can also directly access the XML files and manipulate them that way (minus xlb, of course).
You can also create a library from you execution file and copy it locally. See here.
As for headers, just use a Dictionary(Of String, Integer) or List(Of String) to figure out what the index of the file(s) is.