vba转换值公式

时间:2018-07-13 09:26:44

标签: vba excel-vba excel-formula

我有这个例程:

Sub toevoegen()

  With Worksheets(SheetSchaduwblad)

    aantalrijen = .Range("A1", .Range("A1").End(xlDown)).Cells.Count

    .Range("S2:S" & aantalrijen) = "=IF(RC[-13]>0,""ja"",if(RC[-14]>0,""ja"",""nee""))"
    .Range("T2:T" & aantalrijen) = "=IF(RC[-11]>0,""ja"",if(RC[-12]>0,""ja"",""nee""))"
    .Range("U2:U" & aantalrijen) = "=IF(RC[-9]>0,""ja"",if(RC[-10]>0,""ja"",""nee""))"
    .Range("V2:V" & aantalrijen) = "=IF(RC[-7]>0,""ja"",if(RC[-8]>0,""ja"",""nee""))"
    .Range("W2:W" & aantalrijen) = "=IF(RC[-6]=""MERK"",""ja"",""nee"")"
    .Range("X2:x" & aantalrijen) = "=IF(RC[-6]=""UPC/EAN Code"",""ja"",""nee"")"

    End With
End Sub

我想知道是否以及如何使用VBA将公式的值直接更改为硬值。

我尝试添加此例程:

 .Range ("S2:X" & aantalrijen)
        .Value = .Value

这使它成为完整的例程:

Sub toevoegen()

  With Worksheets(SheetSchaduwblad)

    aantalrijen = .Range("A1", .Range("A1").End(xlDown)).Cells.Count

    .Range("S2:S" & aantalrijen) = "=IF(RC[-13]>0,""ja"",if(RC[-14]>0,""ja"",""nee""))"
    .Range("T2:T" & aantalrijen) = "=IF(RC[-11]>0,""ja"",if(RC[-12]>0,""ja"",""nee""))"
    .Range("U2:U" & aantalrijen) = "=IF(RC[-9]>0,""ja"",if(RC[-10]>0,""ja"",""nee""))"
    .Range("V2:V" & aantalrijen) = "=IF(RC[-7]>0,""ja"",if(RC[-8]>0,""ja"",""nee""))"
    .Range("W2:W" & aantalrijen) = "=IF(RC[-6]=""MERK"",""ja"",""nee"")"
    .Range("X2:x" & aantalrijen) = "=IF(RC[-6]=""UPC/EAN Code"",""ja"",""nee"")"

    .Range ("S2:X" & aantalrijen)
        .Value = .Value
     End With
End Sub

但是在执行时,它显示了一个错误警告:error 438: this property or method is not supported by this object

我在互联网上找到了这个例程:

Sub Range_Example_1()
    With Range("A5:D100")
        .Cells.Copy
        .Cells.PasteSpecial xlPasteValues
        .Cells(1).Select
    End With
    Application.CutCopyMode = False
End Sub

我想知道是否需要将此复制/粘贴作为值,或者是否可以使公式直接写值。

1 个答案:

答案 0 :(得分:1)

您从此行得到错误:

.Range ("S2:X" & aantalrijen)
    .Value = .Value

它在With语句内,因此.Value之后的=引用Worksheets(SheetSchaduwblad).Value。但是,工作表没有值,因此,如果要在单元格中不带公式的值,则需要再次引用该单元格。

.Range("S2:X" & aantalrijen).Value = .Range("S2:X" & aantalrijen).Value

应该做到这一点,您可以避免使用嵌套的Range两次使用With

With .Range("S2:X" & aantalrijen)
.Value = .Value
End With