Excel VBA - Find first instance of value after another value - vba

The example is created to help illustrate my problem.
I have a table of values the following table in Excel:
A B C D
Total Inventory £55,000 Customer 3
Work In Progress £20,000
Completed Goods £35,000
Total Inventory £60,000 Customer 4
Work In Progress £30,000
Completed Goods £30,000
Total Inventory £20,000 Customer 2
Work In Progress £5,000
Completed Goods £15,000
Total Inventory £2000 Customer 1
Work In Progress £1500
Completed Goods £500
When the table is generated the customers in column D (and the corresponding information in the other columns) appear in a random order. I'd like to extract the "Total Inventory" figure for Customer 4. As such I am wooking for a way to search for "Customer 4" in Column D and then extract the first "Total Inventory" figure which occurs in Coloumn A after that.
I know how to do a VLookup using VBA and extract the figure from Column B but I don't known how to make sure that I extract the correct "Total Inventory" figure when the order of the customers changes each time the table is generated.
Any suggestions would be appreciated!
Thanks

Sub Macro1()
Dim cust As String
cust = "Customer 3" ' it can be any other varible I just used this to check the data
formu = "index(b:b,match(""" & cust & "Total Inventory"",c:c&a:a,0),1)" ' this one will run to lookups and make sure that correct line with 2 lookup values
Result = Evaluate(formu)
MsgBox (Result) ' again I entered this just to see that this is working
End Sub
Text inline. Tested and validated.

Related

SQL Where for multiple condition

I am trying to filter a data in SQL 2008 using where to create purchase order report. I want to say if Item Price < $2 and we sold 1 sale at least within a month then show me all items their quantity on had are less than 12 AND if Item Price < $10 and we sold 1 sale at least within three months then show me all items their quantity on had are less than 4 AND if Item Price < $50 and we sold 1 sale at least within six months then show me all items their quantity on had are less than 2 AND if Item Price < $100 and we sold 1 sale at least within nine months then show me all items their quantity on had are less than 1
Is any way I can do that. Please advise with syntax. The price filed is PREC_1, Quantity filed is QTY_ON_HND, Sale Date is SALD_DAT and sold quantity is QTY_SOLD
I will appreciate your help
Here is the query I made
`SELECT
'-- Select from IM_IN'
enter code hereM.ITEM_NO,
enter code hereM.DESCR,
enter code hereN.QTY_ON_HND,
enter code hereCASE
enter code hereWHEN N.QTY_ON_HND<0
enter code hereTHEN 0
enter code hereELSE N.QTY_ON_HND
enter code hereEND AS QTY_HND,
enter code hereN.LST_COST,
enter code hereN.LST_RECV_DAT,
enter code here--Select from IM_ITEM
enter code hereM.CATEG_COD,
enter code hereM.ATTR_COD_1,
enter code hereM.ITEM_VEND_NO,
enter code hereM.ALT_1_UNIT,
enter code hereM.ALT_1_NUMER,
enter code hereM.PRC_1,
enter code hereM.LST_COST,
enter code here--Select from PS_TKT_HIST_LIN
enter code heresum (S.QTY_SOLD) AS QTY_SOLD,
enter code herecount (S.BUS_DAT) AS SALS_DATS
enter code hereFROM
enter code heredbo.IM_INV N
enter code hereINNER JOIN dbo.IM_ITEM M
enter code hereON
enter code hereN.ITEM_NO = M.ITEM_NO
enter code hereINNER JOIN
enter code heredbo.PS_TKT_HIST_LIN S
enter code hereON
enter code hereN.ITEM_NO = S.ITEM_NO
enter code hereWHERE S.BUS_DAT>getdate()-270 AND N.QTY_ON_HND <4 AND M.PRC_1 <2
enter code hereGroup by
enter code hereM.ITEM_NO,
enter code hereM.DESCR,
enter code hereM.ITEM_VEND_NO,
enter code hereM.CATEG_COD,
enter code hereM.ATTR_COD_1,
enter code hereN.QTY_ON_HND,
enter code hereN.LST_COST,
enter code hereN.LST_RECV_DAT,
enter code hereN.LST_SAL_DAT,
enter code hereM.ALT_1_UNIT,
enter code hereM.ALT_1_NUMER,
enter code hereM.PRC_1,
enter code hereM.LST_COST,
enter code hereOrder by M.ITEM_NO`
You are going to need to combine 2 types of filtering.
"Item Price < $2" -> This needs a WHERE Clause
EG
WHERE [item price] < 2
" we sold 1 sale at least within a month"
This is an Aggregate type filter so you need to use HAVING
HAVING COUNT(*) > 1
Plus a Grouping clause
GROUP BY DATEPART(mm, bus_dt)
As you have a lot of criteria - I'd suggest you break it down and start at the top. Then work your way down once you have the first set of filters working ...
It might be beneficial to try and tabulate all the different criteria into a simple EXCEL table.
EG
Add a row for every OR and a column for every AND condition.
Bon Chance!

Selection Formula where value is in a DataTable

My system has a database table for orders (Sales Lines) and a table that stores customer addresses (Customer_Addresses). Each customer may have up to 10 addresses.
I need to create Crystal Reports to get all orders between a specified date range, where the customer is in a selected country. Such as between 01/01/2016 - 31/12/2016 in the UK.
I have considered selecting all address codes where Country = selected_country from Customer_Addresses, then storing the results in a DataTable. Then I'd use a selection formula of {Sales Lines.Order_Date} >= myDate AND {Sales Lines.Order_Date} <= myOtherDate AND {Sales Lines.Address} = (One of the codes within the DataTable)
What code would I need after {Sales Lines.Address} = to search for records where the address code in Sales Lines.Address is also stored in the DataTable I created when selecting from Customer_Addresses?
Crystal Reports has a simple functionality for this kind of tasks:
Just join from Sales Lines to Customer_Adresses.
Now you can set your date filter and your country filter directly.
You will have duplicates in your data if more than one customer address for the same order matches with the country code. But this is no problem. Crystal Reports has a option to enforce distinct data. Just activate this option in the database menu (it will use a DISTINCT in SQL)...

get previous total from filtered amount in crystal report

I have created the following report in Crystal Report using VB.Net 2012. The OPENING BALANCE & NET TOTAL rows are not in my report but my question is there.
TR_ID | TR_DATE |DETAILS | EXPENSE | INCOME | BALANCE
OPENING BALANCE 0.00
1005 08/24/2015 CASH RTN 0.00 10,000.00 10,000.00
1013 08/25/2015 PURCHASE 3,500.00 0.00 6,500.00
1014 08/25/2015 DEPOSIT 5,000.00 0.00 1,500.00
1013 08/27/2015 SALES 0.00 4,000.00 5,500.00
NET TOTAL 5,500.00
I filtered the specific records within the date range of 08/25/2015 and 08/26/2015 & it works well. My question is how I get the previous balance (10000) of the specific date (08/25/2015) in "OPENING BALANCE" row and the total balance in "NET TOTAL" row?
As per the given datas above, the report shows the 4 records well excluding the rows of OPENING BALANCE & NET TOTAL. I want to add the OPENING BALANCE row to show the opening balance of every vendors' accounts when I open the report. And if I view the records of a particular customer within a date range, then OPENING BALANCE row should show the previous balance until the start date of the given date criteria. Ex: If I select records from 08/25/2015 to 08/26/2015 as per above datas, the report will show the two particular records (2nd & 3rd). But I want to show the previous balance 10,000 or the total amount which is above to the star date (08/25/2015) should be shown in the OPENING BALANCE row. And the sub total amount of this vendor's
should be shown in the last NET TOTAL row. So, pls help me, how the formulas will come & how to do.
Thank you
I expect your great help
Thank you
It´s not too much clear to me, but you may try to get the values directly from the report using a variable to do it.
With the report opened, you may entry in the CrystalReport option and select "Selection Expert - Saved Data".
You must create a formula using the "Formula Field" at left panel, and build it using the top 3 windows:
The first top window shows the fields of the table your report handles;
The middle top window show some built-in formulas
SO, you must
Go to formula field and create a new one;
Select the field at the top-left window you want to get (the Openning)
Drag-drop it into the main window (below the 3 top selections) and save it as a new variable
Add this variable into your report. This way, you may get this field for calculations in the same report.
Is it you need to do?
Sorry if I cannot understand exactly what you need.
Hugs!
UPDATE
You may have TOTAL creating a formula of SUM within report.
Click on the field EXPENSE (which appear in 3 rows) and select in menu: CRYSTAL REPORTS - INSERT - SUMMARY. It will create a field that must be positioned at the bottom (in the new Group will be created).
Do the same with INCOME field.
Create a FORMULA FIELD at the bottom to calculate the difference between both SUMARIES. You can name each of these summary fields (like IncSum and ExpSum) and put in that third field: IncSum-ExpSum. You may also put them as invisible.
Your final balance should be:
OldBalance (the parameter I point next lines)
+
Movement (the field that reflects IncSum-ExpSum)
To get the last/old Balance, you may have to get it from code as pass it as a PARAMETER. To do this, create a parameter in te report: see in the FIELD EXPLORER you have a choice of PARAMETER FIELD. AFter create it, use this code:
Dim MyCrystalReportt As New ReportDocument()
MyCrystalReportt.Load(Application.StartupPath & "\ReportSample.rpt\")
MyCrystalReportt.SetDatabaseLogon(UserID, Password, DataSource, InitialCatalog)
Dim myLogonInfo As New TableLogOnInfo()
Dim myTable As Table
For Each myTable In MyCrystalReportt.Database.Tables
myLogonInfo = myTable.LogOnInfo
myLogonInfo.ConnectionInfo.ServerName = DataSource
myLogonInfo.ConnectionInfo.DatabaseName = InitialCatalog
myLogonInfo.ConnectionInfo.UserID = UserID
myLogonInfo.ConnectionInfo.Password = Password
myTable.ApplyLogOnInfo(myLogonInfo)
Next
MyCrystalReportt.SetParameterValue("Data1", LastBalance.Value.ToString)
ReportsForm.CrystalReportViewer1.Reportsource = MyCrystalReport
ReportsForm.show
Notes:
You may have a form (ReportsForm) with a Crystal Control within (CrystalReportViewer1).
Datasource is the Server Name or IP
InitialCatalog is the Database (not the table!)
The ParameterValue MUST be a string - is numeric is required, convert it to number inside the Report. Use a temp_field to get it (see formulas of Crystal and you will find some to make this task).
I hope it can help you.
Good luck.

SQL and VBScript

I want to show the quantity and Lbs purchased per customer on a web page without showing duplicate customers. The output value of total lbs is derived from retrieving the total lb in that type of package i.e. 6 for a 6 lbs per package, then in VBScript multiplying it by the quantity in that row.
<% Customer = ("CustomerName")
Qty = ("Qty")
ItemLbs = ("Lbs") * Qty
%>
It works fine without SUMing the customer names but when the duplicate customer names are SUMed the VB equation outputs incorrect information.
I would Like to corect this either via VBScript or SQL
Use GROUP BY and have SQL do it for you:
SELECT CustomerName,sum(qty*lbs) AS TotalWeight FROM yourtable GROUP BY CustomerName

Making Excel report into SSRS with Pivot Tables

I am trying to create a SSRS report from an ad hoc script I have. I can get results but the user of the report requests certain pivots that I can not seem to correctly display the information.
Below is an export of the report from sql with the pivots added. I want each row to be grouped by the GRADE first, OD second, and ID third. The pivot is adjusted for the STOCK NUMBERs. Each row with matching grade,OD, and ID will have numerous stock numbers.
http://i.imgur.com/Bx3yMuX.png
This is the closest I have have came to displaying the information in SSRS.
http://i.imgur.com/hpajKmB.png
Is there anyway I can have the Stock numbers run across a single row instead of a row for each stock number?
I would create a Column Group based on Stock number. That will generate a column for each Stock number.
It was a little more tricky than I thought. Since I did not want the Stock Number to actually be a column header but to just list on each column corresponding to my group it was not an actual pivot and was why it was not working properly.
Basically to get it to work I had to use the LookUpSet+Join function in SSRS to list the stock numbers out. I was not required to list them out on different columns. Thank-you for your replies.
Here is the code I used in the column
=Join(LookupSet(Fields!grade.Value & Fields!od.Value & Fields!wall.Value & Fields!id.Value, Fields!grade.Value & Fields!od.Value & Fields!wall.Value & Fields!id.Value, RTrim(Fields!stock_number.Value), "FOC"), ", ")