简体   繁体   English

Excel VBA:如何将公式转换为最后使用的列的值?

[英]Excel VBA: How to turn formulas into values for the last used column?

I am trying to turn all my formulas into values for the last used column in my sheet. 我试图将所有公式转换为工作表中最后使用的列的值。 (Last column changes monthly) This is my code so far: (最后一栏每月更改)这是到目前为止的代码:

Sub turnvalues2()
Set rng = Range(4, Columns.Count).End(xlToLeft).Column - 1
rng.Value = rng.Value
End Sub

Range(4, Columns.Count).End(xlToLeft).Column - 1 returns a number not a range. Range(4, Columns.Count).End(xlToLeft).Column - 1返回一个数字而不是范围。 You want the Columns() at that number. 您想要该数字的Columns()。

Sub turnvalues2()
Set rng = Columns(Cells(4, Columns.Count).End(xlToLeft).Column - 1)
rng.Value = rng.Value
End Sub

Also the - 1 moves it to the second to last column used, You may want to remove that to get the last column used. 同样, - 1将其移至使用的倒数第二列,您可能希望删除该列以获取使用的最后一列。

Alternate version using Range.Find to get last populated column: 使用Range.Find的替代版本以获取最后填充的列:

Sub tgr()

    Dim ws As Worksheet
    Dim rFound As Range

    Set ws = ActiveWorkbook.ActiveSheet
    Set rFound = ws.Cells.Find("*", ws.Range("A1"), xlFormulas, , xlByColumns, xlPrevious)

    If Not rFound Is Nothing Then rFound.EntireColumn.Value = rFound.EntireColumn.Value

End Sub

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

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