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

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

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