You are here:

Excel/Excel VBA to conditionally select formula

Advertisement


Question
Tom

I need to test each formula in a spreadsheet.  If the formula contains a reference like "Text!A" (followed by the remainder of the cell reference - there will be hundreds) then go to next cell and test.  If cell contains formula that does not make reference to "Text!A", then copy, pastespecial, value for that cell.  Process next cell.

The reference to text could be as simple as "=Text!A45" or more complex like "=IF(AC126=Up,Text!$A$55,IF(AC126=Same,Text!$A$56,Text!$A$57))"
Many thanks


Answer
Brian,

so for every cell that has a formula that does not have a reference to text, then replace that formula with the value returned

select the cells to process and run the macro

sub replaceformula()
dim cell as range
for each cell in selection
if cell.hasformula then
 if instr(1,cell.formula,"text!",vbtextcompare) = 0 then
      cell.formula = cell.value
 end if
end if
Next
End sub

test this on a copy of your data to insure it is doing what you want.

--
Regards,
Tom Ogilvy
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

All Answers

Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Worked with the program for many years - provided assistance on MS Excel Newsgroups since 1997. Have received the Microsoft MVP award annually since 1999. I don't answer questions on using Excel in a browser Since I have no way to test this. Prefer not to answer charting questions. I consider myself to be particularly knowledgeable about using VBA internal to Excel but have no problems with formulas and pivot tables either.

Experience

Have Used Excel for 15 - 20 years. Answered in excess of 70,000 Excel related questions in MS Excel newsgroups. Unless obvious, please specify whether you want a worksheet function or macro/VBA solution.

Education/Credentials
BS General Engineering (concentration in Industrial Engineering) MS Operations Research Systems Analysis

©2009 About.com, a part of The New York Times Company. All rights reserved.