简体   繁体   中英

Use formula to update cells, but as values

I've been working with a large set of data in Excel that I use macros to populate formulas in from two different source spreadsheets. The problem is that having so many formulas is causing the spreadsheet to slow to a crawl. Internet searches have led me to believe that I need to set my data as values. I've tested one method that caught my eye, but can't get it to work the way I need it to.

Range("D1:D10").Formula = "=SUM(A1:A10)"
Range("E1:E10").Formula = "=SUM($A$1:$A$10)"
Range("F1:F10").Formula = Evaluate("=SUM(A1:A10)")
Range("G1:G10").Formula = Evaluate("=SUM($A$1:$A$10)")
Range("H1:H10").Value = Evaluate("=SUM(A1:A10)")
Range("I1:I10").Value = Evaluate("=SUM($A$1:$A$10)")
Range("J1:J10").Value = "=SUM(A1:A10)"
Range("K1:K10").Value = "=SUM($A$1:$A$10)"

I just entered random numbers in A1 through A10. Normally, I'll use code like the one that would update my D and E range depending on how I need to use the formula. Using Evaluate() around my formula does what I want with regards to putting a value in each cell rather than the formula, but what it doesn't do is change the range I'm wanting to pull from like the D range formula will. If I use Evaluate(), it will only use the exact formula it contains, basically making it no different than an absolute reference. I need the flexibility to do either while putting in values.

I know a way I could achieve my goal is to simply copy my range with the updated formulas and then paste over them, but I was hoping to keep this a single step.

One other option I saw was Range("C1").Value = Range("A1").Value , but I'm not sure how to use this with a VLOOKUP or an INDEX & MATCH. I'm putting one set of reference data from two different sheets on a master sheet and then removing the duplicates. From there, I'm using formulas to find the different pieces of information on the other sheets. Some of the information might be one both sheets, but not at the same time, so I have to use IFERROR.

I guess I'm trying to find a versatile method to use. If you think you have a better option that doesn't answer my specific question, but resolves my ultimate issue of avoiding freezing with this large amount of data, I'm open to suggestions.

Put the relative addressed formulas into the range then revert to the values returned from the formulas.

with Range("D1:D10")
    .Formula = "=SUM(A1:A10)"
    .value = .value
end with

While I have no online whitepaper to link to at this moment to justify the following assertion, I would restrict the use of Evaluate. I've never found it to be an efficient method of coding and personally only use it as a last resort.

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