Programmatically identify a particular formula (VLOOKUP) in a cell

Joined
Jul 29, 2010
Messages
1
Reaction score
0
I'm writing a small windows form app in C# that searches through an excel spreadsheet for a particular type of formula (VLOOKUP). Currently it loops thru the sheets and the the cells and searches for a string but I don't know how to identify if a cell contains a particular formula.

Below is the method that I'm using. Input parameters are the worksheet and the string to search for. It works for strings but how do I find a particular formula!?

PLEASE HELP!

<code>

private bool ScanWorksheetForValue(Excel.Worksheet xlWorksheet, string sFindMe)
{


txtDisplay.Text = txtDisplay.Text + "... processing worksheet: " + xlWorksheet.Name.ToString() + "\n";
Excel.Range rgFound;
string sFirstFoundAddress;

// Find's parameters are "sticky". If you don't specify them
// they'll default to the last used values - including parameters
// set via Excel's user interface
rgFound = xlWorksheet.Cells.Find(sFindMe,
xlWorksheet.Cells[1, 1],
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart,
Type.Missing,
Excel.XlSearchDirection.xlNext,
false,
Type.Missing,
Type.Missing);

// If Find doesn't find anything, rgFound will be null
if(rgFound != null)
{
// Save the address of the first found item -
// it will be used in a loop terminating condition.
sFirstFoundAddress = rgFound.get_Address(
true, true, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);

txtDisplay.Text = txtDisplay.Text + "......Found match on sheet " + xlWorksheet.Name.ToString() + " at cell " + sFirstFoundAddress + "\n";

// Continue finding subsequent items using FindNext
rgFound = xlWorksheet.Cells.FindNext(rgFound);
string sAddress = rgFound.get_Address(
true, true, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);

// Start a loop that calls FindNext until
// the first found cell is found again
while (!sAddress.Equals(sFirstFoundAddress))
{
//MessageBox.Show("Found match on sheet " + xlWorksheet + " at cell " + sAddress);
txtDisplay.Text = txtDisplay.Text + "......Found match on sheet " + xlWorksheet.Name.ToString() + " at cell " + sAddress + "\n";
rgFound = xlWorksheet.Cells.FindNext(rgFound);
sAddress = rgFound.get_Address(
true, true, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);
}
return true;
}
//MessageBox.Show("The worksheet has been searched.");

return false;
}
</code>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top