简体   繁体   中英

Loop Copy Cell that contains Formula Paste in Same Cell as Value

Coming here because i am struggling a bit with VBA looping.

Requesting some assistance for code to handle the below situation:

I have multiple columns (have named ranges for each) that have formulas in a data table. I want to reduce the file size size since keeping all of the formulas exponentially increases the file size as more records are entered into the data structure. I have approximately 135 columns that are formulated.

Step 1 - I need to copy cells in a multiple columns to be copied and pasted as a value in the same cell. Except for the first row of data which that would always contain the formulas.

Step 2 - Macro to Reinsert the formulas to update the records from the first row.

Thanks in advance!

Luckily loops are not needed. This is for column B only. This routine converts all the cells except B1 from formulas to values:

Sub column2values()
    Dim B1 As Range, B2N As Range, N As Long

    Set B1 = Range("B1")
    N = Cells(Rows.Count, "B").End(xlUp).Row
    Set B2N = Range("B2:B" & N)

    With B2N
        .Value = .Value
    End With
End Sub

and this sub takes the formula still remaining in B1 and copies it downward:

Sub restoreFormulas()
    Dim B1 As Range, B2N As Range, N As Long

    Set B1 = Range("B1")
    N = Cells(Rows.Count, "B").End(xlUp).Row
    Set B2N = Range("B2:B" & N)

    B1.Copy B2N
End Sub

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