{"id":20132,"date":"2013-09-23T11:03:51","date_gmt":"2013-09-23T16:33:51","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/how-to-extract-excel-formulas\/"},"modified":"2023-08-16T23:07:09","modified_gmt":"2023-08-17T04:37:09","slug":"how-extract-excel-formulas","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/how-extract-excel-formulas\/","title":{"rendered":"How to Extract Excel Formulas"},"content":{"rendered":"<p class=\"\">I had a spreadsheet in Excel 2007. (I suspect the approach used here would also work in other versions of Excel, but I have not tried it.) I wanted to print out the formulas used in each cell. I did a couple of searches and wound up in a thread where they have advised me to create a module for the same. To add a new module to your excel file press Alt + F11. Following is the code that I used to get the list of all the formulas used in the excel file.<\/p>\n<p><code>Sub ListFormulas()<\/code><\/p>\n<p>&#8216; constants<br \/>\nConst ksDot = &#8220;.&#8221;<br \/>\nConst ksDoc = &#8220;_formula_doc&#8221;<br \/>\nConst ksXLSX = &#8220;xlsx&#8221;<\/p>\n<p>&#8216; declarations<br \/>\nDim I As Integer, J As Integer, K As Integer<br \/>\nDim L As Integer<br \/>\nDim iRows As Integer, iColumns As Integer<br \/>\nDim sName As String, sFileName As String<br \/>\nDim sNewName As String, sNewFileName As String<br \/>\nDim sCellName As String<\/p>\n<p>&#8216; start<br \/>\n&#8216; source workbook<br \/>\nWith ActiveWorkbook<br \/>\nsName = .Name<br \/>\nsFileName = .FullName<br \/>\nI = InStr(StrReverse(sName), ksDot)<br \/>\nsNewFileName = Left$(sName, _<br \/>\nLen(sName) &#8211; I &#8211; 1) &amp; ksDoc &amp;<br \/>\nksDot &amp; ksXLSX<br \/>\nI = Application.SheetsInNewWorkbook<br \/>\nApplication.SheetsInNewWorkbook = .Worksheets.Count<br \/>\nEnd With<\/p>\n<p>&#8216; delete<br \/>\nIf Dir(sNewFileName, vbNormal) &lt;&gt; &#8220;&#8221;<br \/>\nThen Kill sNewFileName<\/p>\n<p>&#8216; add<br \/>\nWorkbooks.Add<br \/>\nApplication.SheetsInNewWorkbook = I<br \/>\nWith ActiveWorkbook<br \/>\n.SaveAs sNewFileName<br \/>\nsNewName = .Name<br \/>\nEnd With<br \/>\nWorkbooks(sName).Activate<\/p>\n<p>&#8216; process<br \/>\nWith ActiveWorkbook<br \/>\nFor I = 1 To .Worksheets.Count<br \/>\n&#8216; format<br \/>\nWorkbooks(sNewName).Worksheets(I).Cells.NumberFormat<br \/>\n= &#8220;@&#8221;<br \/>\n&#8216; name<br \/>\nWorkbooks(sNewName).Worksheets(I).Name =<br \/>\n.Worksheets(I).Name<br \/>\n&#8216; formulas<br \/>\nWith .Worksheets(I)<br \/>\nWith .UsedRange<br \/>\niRows = .Rows.Count<br \/>\niColumns = .Columns.Count<br \/>\nEnd With<br \/>\nFor K = 1 To iRows<br \/>\nFor J = 1 To iColumns<br \/>\nIf .Cells(K, J).HasFormula Then<br \/>\nsCellName = .Cells(K, J).Address<br \/>\nElse<br \/>\nsCellName = &#8220;&#8221;<br \/>\nEnd If<br \/>\nWorkbooks(sNewName).Worksheets(I).Cells(K, J).Value =<br \/>\nsCellName + .Cells(K, J).Formula<\/p>\n<p>Next J<br \/>\nNext K<br \/>\nEnd With<br \/>\nNext I<br \/>\nEnd With<br \/>\n&#8216; end<\/p>\n<p>End Sub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a spreadsheet in Excel 2007. (I suspect the approach used here would also work in other versions of Excel, but I have not tried it.) I wanted to print out the formulas used in each cell. I did a couple of searches and wound up in a thread where they have advised me [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":20133,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-20132","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hire-developer"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20132"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20132\/revisions"}],"predecessor-version":[{"id":38610,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20132\/revisions\/38610"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20133"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}