Warm tip: This article is reproduced from serverfault.com, please click

Converting formulas in certain column for specific worksheet

发布于 2020-11-28 11:26:03

I'm trying to have my macro insert a formula then convert its calculations to values only for column A.

it seems to be working on Sheet3 but its completely ignoring sheet2

    Private Sub FillRows2()
  '
  Sheets("Sheet2").Range("A14:A" & Sheets("Sheet2").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
  Sheets("Sheet3").Range("A8:A" & Sheets("Sheet3").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
    
    
    Dim sh  As Worksheet

    For Each sh In ThisWorkbook.Sheets
        If sh.name = "Sheet2" Or sh.name = "Sheet3" Then
                With Range(Range("A1"), Range("J" & Rows.Count).End(xlUp))
                    .Value2 = .Value2
                End With
            End If
    Next sh
    
  
  Sheets("Sheet2").Range("B14:B" & Sheets("Sheet2").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
  Sheets("Sheet3").Range("B8:B" & Sheets("Sheet3").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
  '
  Sheets("Sheet2").Range("G14:G" & Sheets("Sheet2").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
  Sheets("Sheet3").Range("G8:G" & Sheets("Sheet3").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
  
End Sub
Questioner
araau11
Viewed
0
kevin9999 2020-11-29 12:42:32

If you tweak your code slightly, you can achieve the result you seem to be after. The following has been tested and works for me using a simple formula to test the conversion to values.

Option Explicit
Sub FillRows2()

Sheets("Sheet2").Range("A14:A" & Sheets("Sheet2").Range("J" & Rows.Count).End(xlUp).Row).Formula = "=5*5"
Sheets("Sheet3").Range("A8:A" & Sheets("Sheet3").Range("J" & Rows.Count).End(xlUp).Row).Formula = "=5*5"
  
Dim sh As Worksheet

For Each sh In Sheets(Array("Sheet2", "Sheet3"))
    With sh.Range("A1:A" & sh.Range("A" & Rows.Count).End(xlUp).Row)
        .Value = .Value
    End With
Next sh

Sheets("Sheet2").Range("B14:B" & Sheets("Sheet2").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
Sheets("Sheet3").Range("B8:B" & Sheets("Sheet3").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"

Sheets("Sheet2").Range("G14:G" & Sheets("Sheet2").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
Sheets("Sheet3").Range("G8:G" & Sheets("Sheet3").Range("J" & Rows.Count).End(xlUp).Row).Formula = "Formula here"
  
End Sub