简体   繁体   中英

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. You want the Columns() at that number.

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.

Alternate version using Range.Find to get last populated column:

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

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