SOLVED

Separating CSV data into rows

%3CLINGO-SUB%20id%3D%22lingo-sub-3011783%22%20slang%3D%22en-US%22%3ESeparating%20CSV%20data%20into%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3011783%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20you%20wonderful%20people%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20create%20an%20expense%20calculator%20based%20on%20order%20data.%26nbsp%3B%20When%20I%20enter%20the%20Invoice%20%23%20in%20the%20box%20at%20top%2C%20data%20populates%20into%20row%207%20as%20csv's.%26nbsp%3B%20I%20need%20help%20with%20a%20VBA%20code%20that%20separates%20the%20csv%20values%20into%20rows%20so%20I%20can%20then%20calculate%20the%20expenses%20associated%20which%20each%20line%20item.%26nbsp%3B%20I%20have%20been%20working%20on%20a%20code%20but%20I%20can%20only%20get%20it%20to%20show%20the%20first%20value%20in%20the%20next%20row%2C%20not%20all%20the%20values%20in%20new%20rows.%26nbsp%3B%20I%20have%20screenshots%20of%20the%20calculator%20and%20also%20the%20code%20I%20have%20so%20far.%26nbsp%3B%20I%20would%20be%20happy%20to%20provide%20any%20additional%20information%20needed.%26nbsp%3B%20Thanks%20so%20much%20for%20your%20help!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3011783%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3012090%22%20slang%3D%22en-US%22%3ERe%3A%20Separating%20CSV%20data%20into%20rows%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3012090%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1221313%22%20target%3D%22_blank%22%3E%40rshenk3%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20I%20understand%20your%20needs%20correctly%2C%20and%20you%20have%20an%20Excel%20version%20with%20FILTERXML%20and%20XLOOKUP%20you%20can%20do%20it%20without%20macros.%3C%2FP%3E%3CP%3EThe%20main%20problem%20seems%20to%20be%20separating%20the%20csv%20data%2C%20and%20that%20can%20be%20done%20using%20FILTERXML.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DFILTERXML(%22%3CC%3E%3CR%3E%22%26amp%3BSUBSTITUTE(H7%3B%22%2C%22%3B%22%3C%2FR%3E%3CR%3E%22)%26amp%3B%22%3C%2FR%3E%3C%2FC%3E%22%3B%22%2F%2Fr%22)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ESubstitute%20the%20commas%20to%20make%20it%20parsable%20as%20XML%3B%3C%2FP%3E%3CP%3E%3CC%3E%3CR%3E1%3C%2FR%3E%3CR%3E2%3C%2FR%3E%3CR%3E3%3C%2FR%3E%3CR%3E4%3C%2FR%3E%3C%2FC%3E%3C%2FP%3E%3CP%3EThat%20formula%20in%20H10%20results%20in%20one%20%3CU%3Ec%3C%2FU%3Eolumn%20with%20multiple%20%3CU%3Er%3C%2FU%3Eows.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22bosinander_0-1638117989230.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F329873i411CC132D0EFD1E7%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22bosinander_0-1638117989230.png%22%20alt%3D%22bosinander_0-1638117989230.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EJ10%20shows%20the%20SUMPRODUCT%2C%20or%20simply%26nbsp%3B%3DSUM(H10%23*I10%23)%20assuming%20Excel%20365.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20attaced%20file%2C%20you%20also%20have%20an%20example%20of%20looking%20up%20the%20wanted%20invoice%20row.%20cell%20P10%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%3DLET(%0A%20%20%20qtyCell%3B%20%20%20%20%20XLOOKUP(H3%3BB%3AB%3BH%3AH)%3B%0A%20%20%20priceCell%3B%20%20%20XLOOKUP(H3%3BB%3AB%3BI%3AI)%3B%0A%20%20%20qty%3B%20%20%20%20%20%20%20%20%20FILTERXML(%22%3CC%3E%3CR%3E%22%26amp%3BSUBSTITUTE(qtyCell%3B%22%2C%22%3B%22%3C%2FR%3E%3CR%3E%22)%26amp%3B%22%3C%2FR%3E%3C%2FC%3E%22%3B%22%2F%2Fr%22)%3B%0A%20%20%20price%3B%20%20%20%20%20%20%20FILTERXML(%22%3CC%3E%3CR%3E%22%26amp%3BSUBSTITUTE(priceCell%3B%22%2C%22%3B%22%3C%2FR%3E%3CR%3E%22)%26amp%3B%22%3C%2FR%3E%3C%2FC%3E%22%3B%22%2F%2Fr%22)%3B%0A%20%20%20output%3B%20%20%20%20%20%20SUM(qty*price)%3B%0A%20%20%20output%0A)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hello you wonderful people,

 

I am trying to create an expense calculator based on order data.  When I enter the Invoice # in the box at top, data populates into row 7 as csv's.  I need help with a VBA code that separates the csv values into rows so I can then calculate the expenses associated which each line item.  I have been working on a code but I can only get it to show the first value in the next row, not all the values in new rows.  I have screenshots of the calculator and also the code I have so far.  I would be happy to provide any additional information needed.  Thanks so much for your help!

2 Replies
best response confirmed by rshenk3 (New Contributor)
Solution

Hi @rshenk3 

If I understand your needs correctly, and you have an Excel version with FILTERXML and XLOOKUP you can do it without macros.

The main problem seems to be separating the csv data, and that can be done using FILTERXML.

=FILTERXML("<c><r>"&SUBSTITUTE(H7;",";"</r><r>")&"</r></c>";"//r")

Substitute the commas to make it parsable as XML;

<c><r>1</r><r>2</r><r>3</r><r>4</r></c>

That formula in H10 results in one column with multiple rows.

bosinander_0-1638117989230.png

J10 shows the SUMPRODUCT, or simply =SUM(H10#*I10#) assuming Excel 365.

 

In the attaced file, you also have an example of looking up the wanted invoice row. cell P10

=LET(
   qtyCell;     XLOOKUP(H3;B:B;H:H);
   priceCell;   XLOOKUP(H3;B:B;I:I);
   qty;         FILTERXML("<c><r>"&SUBSTITUTE(qtyCell;",";"</r><r>")&"</r></c>";"//r");
   price;       FILTERXML("<c><r>"&SUBSTITUTE(priceCell;",";"</r><r>")&"</r></c>";"//r");
   output;      SUM(qty*price);
   output
)

 

Thank you so much for your help!