VBA: Run as query in a Sheet of the same Book - vba

I have a macro in file, and what I need to do is to run a query in it, as it was a SQL table, or find a similar code in VBA. Almost everything I found about it, was about running a query in Access database using VBA in an Excel file.
In my file I have a Sheet that I need to use as database, and a second Sheet that need to show some information when I run a macro. Imagine that I have a sheet like this:
|Number|Field1|
| 5 |AAAAAA|
| 4 |BBBBBB|
| 8 |CCCCCC|
| 3 |AAAAAA|
| 4 |BBBBBB|
| 2 |AAAAAA|
| 1 |CCCCCC|
This query should sum only the numbers where Field1 has a determined value, and should sum all the lines in the sheet. The number of lines vary depending the day.
Is it possible to do something like this?

I suggest the sumif forumla. (So, you don't need a macro for that):
=SUMIF(B2:B8;"BBBBBB";A2:A8)
This sums the numbers in A2 to A8 if the mathching rown in B2 to B8 contains "BBBBBB"

Related

How to Embed Excel Formula in SSRS Report [duplicate]

I have an ssrs report that sums a column for a total, like so:
=Sum(CDEC(Fields!Month01Balance.Value))
Now, when exporting to excel this does not export as a formula. I was told there is a way to do it using the ReportFields collection, however I cannot get that to work. Does anyone know of a way I can get a total column to export a formula to excel? I don't know how many rows there will be so I cannot manually reference each ReportField text box.
Thanks for any pointers here!
You must replace the VALUE of the cell with text representing the formula, but the formula must be preceded by a single apostrophe: e.g.,
A | B | C
2 | 4 | 6
Becomes:
A | B | C
2 | 4 | '=A1+B1
When you export the report to Excel, replace all the single apostrophes with [nothing]. The formulas should now run.
Suggestion: Create a new boolean report parameter called "Show_Formulas." Then, write an expression for field C: =IIF(Parameters!ShowFormulas.Value,"'=A" & RowNumber("myQuery") & "+B" & RowNumber("myQuery"),Fields!C.Value)
This way, the enduser can either see the values in the report, or re-run the report showing the formulas for export.

VLOOKUP | MATCH/INDEX, or VBA? Help pls! Search column for string, return value from that row

I will try to reword this bc I still have had no luck:
I have a workbook with 2 different tabs:
REPORT_TAB and
RAWDATA_TAB
My RAWDATA_TAB has standard database looking data in it like this:
A | B | C | D
ACCOUNT_KEY | AMOUNT | DATE | FEES
MGX4421 100 6/15/2018 26
MGLR200 75 6/15/2018 5
CXDSTR 150 6/15/2018 50
18000 45 6/15/2018 10
On my REPORT_TAB, I simply want to search column A above for specific accounts, and return the value in column B. For example:
For one cell I want to search for "CXDSTR" and return 150
For another cell, I want to specifically search for acct "MGX4421" and return 100
I thought I could accomplish that with something like:
=VLOOKUP("MGX4421", RAWDATA_TAB!A1:D10,2,0) which would return me 100, but it just says #N/A with the green triangle in the cell
Any help? Would this be possible with VLOOKUP, or easier with VBA? or MATCH/INDEX
Thanks in advance for the help!
I was able to find a solution for this. I couldn't get the VLOOKUP to do exactly what I was trying to do, so I used the INDEX-MATCH combo. Syntax looks like:
=INDEX(RAWDATA_TAB!A1:T1000,MATCH("MGX4421",RAWDATA_TAB!C:C,0),2)
This basically goes to the RAWDATA_TAB, searches for the row which contains the unique account number "MGX4421" and returns the column "2" which in the above example is the [AMOUNT].
I hope this helps anyone

Is it possible to write a VBA code that searches for the exact same data or the closest one in the past?

I have two excel wordbooks. One has parameter changes on certain dates and the other has dates on which the juice has been produced. Like this
Spreadsheet 1
date of parameter change %apples %oranges
30/09/2014 55 45
25/09/2014 50 50
20/09/2014 45 55
Spreadsheet 2
date of the created juice %people that liked it %apples %oranges
26/09/2014 88
22/09/2014 91
And I want to copy the % parameters of apples and oranges into the second workbook so that the result looks like
date of the created juice %people that liked it %apples %oranges
26/09/2014 88 50 50
22/09/2014 91 45 55
So basically if a juice is made on 26/09 for example then I want to have the parameters that were used for that juice. Obviously the parameters of 30/09 weren't used but the parameters of 25/09 were used because it's the day before the juice has been made.
Is it possible to create something like this with VBA? Could anyone show me how to do the date searching part at least? It would be so much appreciated!
You can use a VLOOKUP formula for what you would like to do rather than using VBA.
The syntax of a VLOOKUP is as follows:
VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])
To find an approximate match to the value that you are looking for, you can set the range_lookup parameter to TRUE
If you cannot change the sort order of the information in Spreadsheet 1 then a slightly more complex INDEX(MIN(INDEX())) function pair would do.
     
The standard formula in G2 is,
=IFERROR(INDEX(B$2:B$4, MIN(INDEX(ROW($1:$3)+($A$2:$A$4>$E2)*1E+99,,))),"")
This can be copied or filled both right and down. The IFERROR wrapper shows a blank cell when there are no dates that match your criteria (as in 19-Sep-2014 in the image supplied). The sample as produced on a single worksheet for demonstration's sake but the two tables could in in different worksheets or even different workbooks.
If date of parameter change is in Book2 Sheet1 A1 and date of the created juice is in A1 then in C1:
=VLOOKUP($A2,[Book2.xlsx]Sheet1!$A:$C,COLUMN()-1)
copied across and down may serve provided the date order in Book2 is reversed and both sheets are open at the same time (or the full path would be required).

How to automatically create filtered list from Excel dataset using VBA?

I got a problem with Excel 2007 where I cannot find a solution to. I think one has to use VBA to do it by I am pretty unexperienced with that.
I have a dataset in Excel (about 1k lines) with 10 columns. In an other tab/worksheet, I now want to display the data filtered by one column (country codes). It can be done dirty with autofiltering but it should be more like a report so it has to look good.
Let me make an example what I mean:
DATASET
Column | Column 2 | Column 3
Hans | GER | 16
Peter | US | 29
Maria | GER | 30
Darian | US | 15
Lisa | GER | 40
In an other Tab/Worksheet, I now want an automatically updated, formatted and filtered list based on this data set, e.g.:
REPORT
Germany
Hans | 16
Maria | 30
Lisa | 40
US
Peter | 29
Darian | 15
I hope you understand what I mean. If I add new lines of data in my dataset sheet, I want the report to be automatically updated (so I can't just but autofilter lists below each other because the number of rows which will be needed depends on the data points). I think this should be pretty easy to do using a VBA macro, but I have no clue how to do it... Help very much appreciated!
Thanks upfront!
*
Have you tried a PivotTable combined with a dynamic named range?
Given your data set above is set up in Sheet1 of your workbook, starting in A1 with column names. See the following solution.
1) Enter a dynamic named range for your dataset, like below.
This will ensure that any new data that is added to the end, or inserted into the middle of the data set gets captured and updated automatically with a pivot table refresh.
2) Base your pivot table on the Named Range.
3) Finally, set up your PivotTable like below.
You can then format your pivot table how you like.
A Pivot table can do the type of grouping you are looking for. Using the data you provided, this is what I get from a pivot table:
I suggest you take a look at pivot tables. they can be a bit complicated but once you figure out how they work they are very useful.

Excel formula to calculate diff b/w 1st value and last value in a column (Excel)?

I have the following columns : (Using Excel Formula)
A B
------------------------
1 | Date | Value |
------------------------
2 | 8/20/2011 | 92.8 |
3 | 8/21/2011 | 92.4 |
4 | 8/22/2011 | 91.8 |
5 | 8/23/2011 | 90.7 |
6 | 8/24/2011 | 91.1 |
7 | 8/25/2011 | |
8 | 8/26/2011 | |
9 | 8/27/2011 | |
------------------------
I want to calculate the difference between 1st Value (B2) and last Value (last populated row in column B)
Edited :
Using formula : =B2-B6 is not what's required. (I want diff in Cell C2)
I want when the user enters the value in B7 it automatically shows the difference between B2 and B7, when he populates B8 then it shows the diff between B2 and B8 and so on..
I don't want some direct method to do this in Excel and not by iterating all values to check the last value. Please suggest the best method to this in Excel.
This is more of a logic problem !
A Pure Excel solution is welcome, I can do that manually.
Edited:
Using Excel formula !
I hope I explained my problem properly..
If the headers are in A1 and B1, and your table is A2:B9 for instance, use the formula:
=$B$2-OFFSET($B$2,COUNTA($B$2:$B$9)-1,0,1,1)
To find the last number in a column you can use VLOOKUP on a single column where 1E+99 is the lookup_value and the optional range_lookup is left to the default. The data does not have to be sorted.
     
The formula in C2 is =VLOOKUP(1E+99,$B$2:$B$99,1)-B2. Similarly, you can locate the last non-blank alphabetic value with =VLOOKUP("zzzzz",$E$2:$E$99,1) (example in F2).
To find the last non-blank value where there are mixed alphabetic and numeric values, use INDEX and apply MAX to MATCH, e.g. =INDEX(E2:E99,MAX(IFERROR(MATCH(1E+99,E2:E99),0),IFERROR(MATCH("zzzz",E2:E99),0))). Any of these formulas could easily be adjusted to return a value from another column corresponding to the last value in the lookup column.
I understand you are not looking for a pure Excel solution but you can use it a different way >> you can add an Excel formula with vb/vba.
Something like =B7-$B$2 is the formula needed.
Thus, you can do something like:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Target.Offset(0, 1).Formula = "=" & Target.Address & "-$B$2"
End If
End Sub
Another less elegant way to do this with Excel formulas alone, would be to use a helper column, which is a telescoping series.
In column C2, enter 0.
In columns C3, use the formula
=IF(B3="",0,B2-B3)
Copy down the length of the table.
At the bottom, sum everything in the helper column.