简体   繁体   中英

Write value of formula into cell with VBA

If i want value from a Excel formula intoa cell and only value not " The formula"

I have try with this code but it gives me not only the value.

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

you could use this:

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

You can use Evaluate :

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

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