简体   繁体   中英

Excel to replace formulas with values

I have an Excel workbook (1) with around 9 sheets that is pulling in and manipulating data from a second workbook (2).

After pulling in the data from workbook (2) I need to be able to replace the formulas in workbook (1) to the resulting values that the formulas have produced, from here I will then save the workbook (1) with the results.

Is there a macro that can do this for me?

On your new workbook some basic code such as:

Sub Value()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
    ws.UsedRange.Value = ws.UsedRange.Value
Next
End Sub

While the OP is dated, I want to make note of a non-loop method that is useful. In certain scenarios, loops can really slow down the code execution. To replace formulas in a cell --without a loop-- try:

With Sheets("example").Range("A1:C1000")
   .value = .value
End With

You can revise the reference as necessary, but the execution is seamless, fast, and as a bonus - prevents range highlighting that cannot be cleared if you pursued the .copy + .pastespecial xlPasteValues approach.

What seems to work for me is to use concatenate()

So, for example, the formula I have referencing a cell from another sheet is:

=arrayformula(iferror(index('To Be Processed'!X:X,small(if($A$1='To Be 
Processed'!$Y2,row('To Be Processed'!X:X)),row((2:2))),"")))

and if I change to the formula to:

=concatenate(arrayformula(iferror(index('To Be
Processed'!X:X,small(if($A$1='To Be Processed'!$Y2,row('To Be
Processed'!X:X)),row((2:2))),""))))

and it puts in the text value from the reference cell into my second sheet.

Which may or may not be helpful depending on how you populate your sheets--I'm not very good with VBA, though, which means I do more things manually :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM