简体   繁体   English

循环复制单元格,其中包含公式粘贴在与值相同的单元格中

[英]Loop Copy Cell that contains Formula Paste in Same Cell as Value

Coming here because i am struggling a bit with VBA looping.来到这里是因为我在 VBA 循环中有点挣扎。

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.我有大约 135 列已制定。

Step 1 - I need to copy cells in a multiple columns to be copied and pasted as a value in the same cell.第 1 步 - 我需要复制多个列中的单元格,以将其复制并粘贴为同一单元格中的值。 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.第 2 步 - 宏重新插入公式以更新第一行的记录。

Thanks in advance!提前致谢!

Luckily loops are not needed.幸运的是不需要循环。 This is for column B only.这仅适用于B列。 This routine converts all the cells except B1 from formulas to values:此例程将除B1之外的所有单元格从公式转换为值:

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 采用仍然保留在B1中的公式并将其向下复制:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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