Display change in value of a cell in adjacent cell using excel VBA - vba

I have a excel sheet that display's price on certain items in a column by looking up amazon API using excel vba. The price of may change overtime. So I am trying to display the difference in prices each time i run my macro, in a cell adjacent to the cell that displays price.
But I am not sure how to achieve this. Can any body guide me on how to achieve this?

This is just a sample, it must be adapted to your schema and data layout. Say the prices are stored in column A from A1 to A100. Say you already have a macro called RefreshData() that updates column A. In B1 enter:
=C1-A1
and copy down. This macro store the current values in column C before refreshing the data:
Sub DoUpdate()
Range("A1:A100").Copy Range("C1")
Call RefreshData
End Sub
Column B will display the price difference.

Something like this?
Let's say your data are in a range A2:A10
Dim rng as Range
Set rng = Range("A2:A10")
rng.Offset(0,1).Value = rng.Value
Run this before you run your original macro to store the values in an adjacent column before the values change. You may need to make the range dynamic, depending on your needs.

Without seeing you code, I cannot give a detailed answer. However, I ran across a similar problem once, not using Amazon API though, but a sharepoint connection.
If the amazon api is somewhat similar to the sharepoint stuff, I guess it refreshes cells when you click "update", or run the update sub. In that case you will have to either create an array to store the old prices in vba (very slow process), and then write them to your table, or create a separate tab where you store the item-lastPrice combination.
I ended up storing not only current price but all prices and the date/time of the price, to be able to see change over time.
For the copying of data itself using VBA, either of the above methods should work. In my initial code I used vba loops :-p, but copying using excel functionality is much faster.

Related

Use One Cell to Add Values to a Named Range

We are trying to allow the end-user of this Google spreadsheet to be able to add values to a Named Range without accessing said range. (Sorting it will be a q for a later date)
In other words, let's say that we have a list of Suppliers that we purchase parts from, and that list resides on 'SheetLists' in cells A1 to A119.
To make that list easier for later use, we made it into a Named Range and named it "list1Supplier".
We then use that "Named Range" throughout the Workbook in drop-down lists. Each of the drop-down lists reference the "Named Range" and NOT cells A1 to A119.
We also realized that, should we begin using a new Supplier, the list could grow. So we changed the Named Range reference to cells A1:A, thus including the entire A column.
Now, as expected, one of the end users begins using a new Supplier. The LAST thing we would EVER want is for them to have to figure out how to add said Supplier to the end of that list MANUALLY... right!?!?
I've googled this for hours and have found NOTHING except ONE using VBA. And unless someone has a REALLY easy to follow How-To on VBA basics, I'd like to try this route first...
As always, ANY and ALL help is GREATLY appreciated!
DG
Why you just don't create a live form?
https://www.youtube.com/watch?v=wNMdjVxAEYY
Best

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.

Is there a way to activate Autofilter only on specific columns in VBA?

I have a macro that creates a separate workbook from a larger document. The number of columns vary on how much data is entered. I would like to add an autofilter to only the currently used columns but every time I add an autofilter I have to put in a range and the range varies. Is there a way to write in the code to only add the filter arrow on the applicable columns and not all of them that could possibly be shown? We are sending the document out to a customer and have to manually unfilter the blank boxes which we would prefer not to do.
Thanks!
Turn your data into an Excel Table using the Ctrl + T keyboard shortcut. These have the autofilter built in, and it automatically gets applied to any new columns in the Table, because Tables expand automatically to accommodate any new data.
Furthermore, because Tables are basically Named Ranges that Excel maintains on your behalf, it is very easy to identify the ranges concerned in VBA, as per my answer at VBA coding to identify and clear data in a specific table

How to make a value based reference in Excel

I have an excel sheet which fetches the values from an API and these values changes in real time, I mean the row position changes on daily basis.
Problem is when I reference a particular row to write a formula in another sheet and its value changes tomorrow I get the wrong value because it is a cell-based reference and not a value based.
How to handle such scenario?
Excel has several worksheet functions for that purpose. The most popular is VLOOKUP, where you know the column in which your value resides but not the row - precisely what you seem to explain. Google for "Excel VLOOKUP" or simply look in Excel's own Help.

VBA to transfer a figure from one sheet to another based on matching values other col

I am working on a spreadsheet which is being used to transfer a product from one location to another. Each day I will have a new list of products that needs sending to another location and I already have a "pre-populated" sheet that has suitable locations listed for where these products can be sent to.
I've already worked out the formula's to use which defines the location these products can be sent to (through index and match formula) but once this has been completed, I'd like to update the "pre-populated" sheet with the quantity I'm sending these locations so that limits can be deducted accordingly.
Essentially, I want to copy the figure from column G in ("Task") into column I in ("interstore transfer") where the two "REF" columns in either sheet match. The "New Limit" column will then automatically populate with the new limit based on the figure input into Column I. Once its finished working its way down the list in the sheet ("Task") then end.
I've had a rough attempt at this, but I'm coming stuck with defining the appropriate variables and how it should update.
Any ideas to better my approach would be appreciated.
A VBA solution with variables may not be your best approach for this. Variables declared within VBA code usually have a limited lifetime based on their scope, so when the code ends the values in the variable will be lost.
Another alternative may be to set aside another cell as a counter. Perhaps a good place for this would be next to the "New Limit" column?
Cell values are retained even when VBA code isn't running. Of course cell values also are saved when the workbook saves, so when you get a new list of products at the beginning of the day you can compare to or edit the previous day's work.
To get started with this, I'd recommend getting familiar with how to reference cells and ranges. And, there is some useful information here on Stack Overflow on how to reference well in Excel VBA.