21
Answers

Search/Filter for data in excel

Matthew Pawelke

Matthew Pawelke

14y
2.5k
1

i have a number of sheets with sales information on different products. each sale has an orderNumber assigned to it, usually one orderNumber will cover numerous sales (ex. 3 balls, 2 shoes, and 5 hats all at differ prices). i want to print an orderReceipt, so have the user search for an order number 12345 and then loop through each sheet (ea. sheet is a product), check if orderNumber column has 12345 in any of its cells, if it does, store all other data in that row for that orderNumber, (i.e. cost, quantity sold, etc.) and store this in a string array.
 
current code:
 
// declare variables and arrays here
for (int i = 1; i < sheetNum; i++)

   xlWorkSheet = (Excel.
Worksheet)xlSheets.get_Item(i); 
   chartRange = xlWorkSheet.UsedRange; 
   string counterColumn = Convert.ToString((chartRange.Cells[counterVar, 2] as Excel.Range).Value2); 

   // read all rows it data 
   while (counterColumn != null
   { 
      if (counterColumn == orderNumber) 
      { 
         date[i] =
Convert.ToString((chartRange.Cells[counterVar, 3] as Excel.Range).Value2); 
         component[i] =
Convert.ToString((chartRange.Cells[1, 1] as Excel.Range).Value2); 
         cost[i] =
Convert.ToString((chartRange.Cells[counterVar, 7] as Excel.Range).Value2); 
         quantityPurchased[i] =
Convert.ToString((chartRange.Cells[counterVar, 5] as Excel.Range).Value2); 
         quantitySold[i] =
Convert.ToString((chartRange.Cells[counterVar, 6] as Excel.Range).Value2); 
         valuePurchased[i] =
Convert.ToString((chartRange.Cells[counterVar, 9] as Excel.Range).Value2); 
         valueSold[i] =
Convert.ToString((chartRange.Cells[counterVar, 10] as Excel.Range).Value2); 
         

      receiptRow[i] = date[i] + "\t \t" + component[i] + "\t $" + cost[i] + "\t" + quantityPurchased[i] + "\t $" + valuePurchased[i] + "\t" + quantitySold[i] + "\t $" + valueSold[i]; 
      } 
      else 
      { 
         counterVar++; 
         counterColumn =
Convert.ToString((chartRange.Cells[counterVar, 2] as Excel.Range).Value2); 
      } 
   }
}

Answers (21)