trying to do a match by relavence using excel - vba

I have this really poorly formatted data that I have to work with every week. Here's a sample:
Code: To: Total: Description:
-------------------------------------------------------------
FD987 00001 5 Food/Snack/M&M
FD987 00001 5 Food/Snack/Pretzels
NA654 00001 5 Non-Alc/Soda/Sprite
NA654 00002 2 Non-Alc/Soda/Sprite
NA987 00002 2 Non-Alc/Soda/Lemonde
I want the data to end up sorted by the "To" code, with the various data summed for subtotals in each category and subtotals for the whole "To" entry. Like this:
To: Total: Description:
---------------------------------------------------
00001 10 Food Subtotal
00001 5 Non-Alc Subtotal
00001 15 Grand total
00002 0 Food Subtotal
00002 4 Non-Alc Subtotal
00002 4 Grand total
I have written code that gets "00001" and "00002". But I don't know what to do from there. Should I loop through using a combination of if and for?
example:
pseudo code:
for all i in UniqueToCodes
if (cell.value = i) then
Descriptions = {"Food", "Non-Alc"}
tempsum = 0
for all j in Descriptions
q = total for item j belonging to Descriptions belonging to UniqueToCodes
tempSum = tempSum + q
next j
end if
next i
or would looping backwards be more efficient?
for example as this:
pseudo-code:
for i = lastOccurenceOf(ToCode) to firstOccurenceOf(ToCode) -1
Descriptions = {"Food", "Non-Alc"}
for all j in Descriptions
q = total for item j belonging to Descriptions belonging to UniqueToCodes
tempSum = tempSum + q
next j
next i
(I know that the "To" codes appear always as blocks and never with interjections by other codes i.e.
always as this:
00001
00001
00002
never as this:
00001
00001
00003
00001)
also how i store all these temp sums? i.e. tempSum of "Food" and tempSum of "Non-Alc" for "To" code "00001"? I know dictionary objects map only one property to a key. But I need to match the subtotal to the Description and the Grand Total to the "To" Code. What would you say is the best approach to this problem? Any help would be greatly appreciated! Thanks!

Like the people said in the comments, a Pivot Table would probably be the easiest way to sum up your data. However, in order to subtotal the way you want, you need to break apart the Description column.
In Excel 2007 you can use Text to Columns as follows:
Select your Description column.
Click Data -> Text to Columns.
On the Convert Text to Columns Wizard make sure Original data type is set to Delimited and click Next.
Select Delimiters to Other with a / and then click Finish.
Rename the new columns whatever you want.
If Text to Columns isn't available, just create a formula to pull the first food type out of the Description. For example, using the first picture above, put this formula in cell E2. =LEFT(D2,FIND("/",D2)-1)
Now you can create a Pivot Table.
Make sure the active cell is within the table of data and click Insert -> PivotTable. When the Create PivotTable dialog box displays, click OK.
Drag To and Type to Row Lables area and Total to Values area.
Done. There is your basic pivot table. You can change the basic format thru Pivot Table Options and Field Settings.

Related

VBA Excel: How to add items for duplicates data in range excel with vba

I want to use VBA to insert items in ITEM column with data source is in Number column like below, count number of items and add number from 1 to number counts. can you help me?
Number Item
24811395 1
24811396 1
24811401 1
24811401 2
24811402 1
24811408 1
24811408 2
24811408 3
24811408 4
Use the formula in B2 as in image below and drag throughout the range,
=COUNTIF($A$2:$A2,A2)

Fetching data from row N+M, N+2M,... when N changes

My Google spreadsheet has webpage data imported with IMPORTDATA() on Sheet1. The data of interest are 20 pcs of 12-row deep posts starting at row N (e.g. row 123). Unfortunately, N changes each week or so.
Today I just use 20 hardcoded formulas that copy the data of interest to Sheet2 (e.g. =Sheet1!A123), but each time N changes I need to re-code all these 20 copying formulas on Sheet 2.
I'd prefer a method using e.g. Indexing or Offset formulas, where I would only need to change one "starting index" value when N changes in the webpage. Help would be much appreciated!
EDIT : Hi, thanks for your interest, sorry for delay. So this is the data structure of my Google spreadsheet. The source data can be considered a product list or similar.
Sheet1:
A1 =IMPORTDATA (url)
A2 to A122
A123 product 1 name
A124 product 1 price
A125 to A132... other product 1 data
A133 product 2 name
A134 product 2 price
A135 to A142... other product 2 data
A143 product 3 name....
... and so on up to product 20.
On Sheet2 I then want the following result:
A1 product 1 name
A2 product 1 price
A3 product 2 name
A4 product 2 price.
... and so on.
The problem is that the "uninteresting preamble data" in the beginning changes in length (number of rows) every now and then.

Excel VBA selecting data from a sorted Table

I am running into a problem in VBA in excel.
I am trying to create a participant registration program in excel for a sports tournament. One can add data like the weight, age and name of a participant. And then based on that, The participants are divided into poules named with letters A, B... until Z. I have a table which can sort these poules by letters and then for example only display the participants which are in poule A such as below
Example
Now i want to count the numbers with VBA macros of participants in the poule which is displayed by the sorted table. For example when i sort on poule A it has to return 3 and when i sort on B, it has to return 2.
Determined by the number of participants in a poule the program will print a different poule scheme, depending on the number of participants. (i have this part in VBA)
The problem is when i have a sorted table like below
Example 2
It counts all the participants, and not just the ones in B or any other sorted letter.
I have this:
Sub Count()
Dim nRows As Integer
nRows = Range(Range("A18"), Range("A18").End(xlDown)).Rows.Count
MsgBox (nRows)
End Sub
This works well if you sort A, but when you sort any other letter, it counts All the table until that letter. Eg. when you sort B, it displays 5 (Number of A + B).
I have looked on the internet for a really long time to find a solution but without succes.
I hope my question is clear and that somebody can help me.
I am assuming that you are using worksheet functions. Use Subtotal when working with filtered data.
These parameters evaluate all cells visible or hidden
1 AVERAGE
2 COUNT
3 COUNTA
4 MAX
5 MIN
6 PRODUCT
7 STDEV
8 STDEVP
9 SUM
10 VAR
11 VARP
These parameters evaluate only visible cells
101 AVERAGE
102 COUNT
103 COUNTA
104 MAX
105 MIN
106 PRODUCT
107 STDEV
108 STDEVP
109 SUM
110 VAR
111 VARP
The code does work now, except that it only counts the first letters it encounters.
So when the first column for the poules is for example A A A E A A B B E.
And i sort to A and use the count function, it only returns a value of 3 and not of 5 (because there are 5 A's)
When I sort the table to A, it looks like this (column number, poule value):
14 A
15 A
16 A
18 A
19 A
And it returns just a count of 3, have you maybe got any fixes for that problem as well?
Pictures:
sorted tabel to E
Table
Range.SpecialCells will return a range of only visible cells.
Dim rSource As Range
Dim rVisibleCells
Set rSource = Range(Range("A2"), Range("A2").End(xlDown))
Set rVisibleCells = rSource.SpecialCells(xlCellTypeVisible)
MsgBox rVisibleCells.Rows.Count

Getting quantity from a unique Item on Excel

I have a spredsheet on Excel and I must get the number of items from a row, somethink like this:
Item1 | Qty1 | Item2 | Qty2 | Item3 | Qty3 | ... | ItemZ | QtyZ |
Into a second sheet, I must populate with these data, but with just two Columns (Items and Qty).
There is any way to do that? I'd rather a solution that uses DGET or VLOOKUP (it seems faster), althought any feasable way to that would make me very happy :D
I've tried use a loop function, using Find + move to the left + copy and paste the qty. The problem was that this tooked like forever (plus my Excel crashed a few times).
So folks, could you help me?
If I understood correctly what you're after, then here's something basic to get you started (it's in VBA, not using Excel functions, but you'll hopefully get the idea):
Public Sub CopyDown()
Dim i As Long
Dim j As Long
j = 1
For i = 1 To Sheet1.Cells(1, Sheet1.Columns.Count).End(xlToLeft).Column Step 2
Sheet2.Cells(j, 1) = Sheet1.Cells(1, i)
Sheet2.Cells(j, 2) = Sheet1.Cells(1, i + 1)
j = j + 1
Next i
End Sub
It's not terribly robust as there's no error checking, but it should work for the case you've described. It assumes Items are arranged across row 1 in odd columns (1, 3, etc.) and Quantities are in even columns (2, 4, etc.) in "Sheet1". Then they are copied to ascending rows (from 1 onwards) in Columns A (for Items) and B (for Quantities) in "Sheet2". Let me know if you need more explanation.

Extract substring of list based on another list

Using two lists, one consisting of names with added information in various forms (see below for example - list 1) and one consisting of the clear formatted names, i.e. with no added information (list 2)
List 1
--------
Netto City | Value
Imerco City | value
Bilka Suburb | value
Bauhaus, City | Value
City FDB Superb | Value
List 2
------
Netto
Imerco
Bilka
Bauhaus
FDB Super
What I am trying to do is create a filter, so that no matter what the first column of my source data(list 1) looks like, i will be able to sum the values based on (list 2).
Something similar to this: Excel - extracting data based on another list
I tried using vlookup, but that does not search for substrings, then i tried using
=IF(COUNTIF(A$4:A$9;"*"&D5&"*")>0;
INDIRECT(ADDRESS(MATCH("*"&D5&"*";A$4:A$9;0);4));"not found")
But that appears to do the opposite, search list 1 for a single cell value from list 2.
I can't quite get my head around if this works just as well, I havent been able to get it to work anyway, thus my search for the other way. Search List 2, for each item from List 1.
But, ultimately, what I am trying to accomplish is to create a list from the source data, which I can use to categorize each item in list 1 from, based on list 3
List 3
Bilka | Cat1
Imerco | Cat2
FDB Super | Cat1
etc.
For that to work, i need a clean list of the source data, without all the extra information which comes with it.
I use the following sumif
=SUMIFS($F$3:$F$703;$B$3:$B$703;
"="&$H4;$D$3:$D$703;">="&I$2;$D$3:$D$703;"<="&I$3)
to sum all sums belonging to a particular item in List 3 (where i've manually created List 3), between to dates.
The purpose of this is to create a sheet that contains all expenditures to a particular store or category of ones own choosing, for instance the ones listed in List 1, are primarily food stores.
Edit - Clarification.
What I am proposing to do is a multistage process.
Stage 1:
Insert original source data (done)
Stage 2:
Filter source data for unique values (done)
Stage 3:
Create list of approve names for each item in source data
- Ie, Bilka Suburb into Bilka, Netto City into Netto
Here 'Netto' and 'Bilka' are approved names which is manually created to allow for grouping in stage 4. I am looking to automatize this step.
Stage 4:
Group each item from the list of Stage 3, based on name and date-interval, weekly monthly whatever (done) if i could only get Stage 3 to work, as it works on my manually corrected data.
Stage 5:
Select appropriate category, and type for each item in resulting list from Stage 3:
Bilka, is a food place, so it would get the category 'food', same as netto, where Bauhaus would get the category 'Building Supplies', each of these items would get the type 'expense' where say wage would get the type 'income' (done)
the solution to stage 5, is just a vlookup, based on the category into a table that lists each category with a type, so that is simple enough.
Final Solution: Requires that the list to iterate over is in column G, and outputs the list of approved names in column H. There is the error of if not being able to know the difference between an item such as "Super" and "SU", I don't know how to fix that. If anyone has any suggestions on that I am all ears.
Sub LoopCells()
Sheets("RawData").Select
Sheets("RawData").Activate
LRApproved = Cells(Rows.Count, "H").End(xlUp).Row
LRsource = Cells(Rows.Count, "G").End(xlUp).Row
For Each approvedcell In Worksheets("RawData").Range("H2:H" & LRApproved).Cells 'Approved stores entered by users
For Each sourcecell In Worksheets("RawData").Range("G2:G" & LRsource).Cells 'items found from bank statement export
If InStr(UCase(sourcecell.Value), UCase(approvedcell.Value)) <> 0 Then
sourcecell.Offset(0, 2).Value = approvedcell.Value
End If
Next sourcecell
Next approvedcell
End Sub
Thanks for all the help.
Edit: Added final solution and VBA tag.
This works for me:
=SUM(B$3:B$7*NOT(ISERROR(SEARCH(A11,A$3:A$7))))
This assumes that your example list 1 is in range A3:B7 and your list 2 in A11:B15. Paste the above formula in cell B11 and press CtrlShift-Enter to enter it as an array formula. Then you can drag-copy it all the way down to B15.
Explanation: SEARCH for e.g. "Netto" in the cells of List 1. For cells that do not contain that string, SEARCH returns an error. So we're looking for cells that do not return an error. We now have an array of booleans indicating this. Multiply it element-by-element by the array of values. In this multiplication, TRUE is interpreted as 1 and FALSE as zero, so you're screening out the values that don't correspond to "Netto".
Here's a secreenshot of my setup:
Perhaps I've misunderstood but can't you use SUMIF?
=SUMIF(A$4:A$9;"*"&D5&"*";B$4:B$9)
instead of going with VBA, you can extract this with simple small formula. =Index(List2!A2:A10,Match(1,Countif(List1A2,""&List2!A2:A10&""),0)) (Press Ctrl+Shift+Enter). Assume you want to extract the list 2 in to list 1.