簡體   English   中英

用VBA將公式的值寫入單元格

[英]Write value of formula into cell with VBA

如果我想將Excel公式中的值放入單元格中,而僅不使用“公式”中的值

我嘗試使用此代碼,但它不僅給了我價值。

Dim ZmAntal As Long

Dim CountryRange As Range, C As Range

Dim Res As Variant 

ZmAntal = Worksheets("Maskinerum").Cells(8, 4).value 

Set CountryRange = Sheets("Zmbistad").Range(Cells(2, 1), Cells(ZmAntal, 1))

For Each C In CountryRange

    Res = "=CONCATENATE(RC[1],RC[14])"

    If Not IsError(Res) Then         

        C.Offset(0, 0).value = Res

    End If

Next C

End Sub

您可以使用此:

Dim ZmAntal As Long

ZmAntal = Worksheets("Maskinerum").Cells(8, 4).Value
With Sheets("Zmbistad").Range(Cells(2, 1), Cells(ZmAntal, 1)) ' reference relevant range
    .FormulaR1C1 = "=CONCATENATE(RC[1],RC[14])" ' write formula in referenced range
    .Value = .Value ' get rid of formulas and leave values only in referenced range
    .SpecialCells(xlCellTypeConstants, xlErrors).ClearContents ' clear any cell with an "error" value n referenced range
End With

您可以使用Evaluate

'...
With CountryRange
    .Value = Evaluate("=IF(ROW()," & _
                            "CONCATENATE(" & .Offset(0, 1).Address & "," & _
                                        "" & .Offset(0, 14).Address & "))")
End With
'...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM