【问题标题】:Write value of formula into cell with VBA用VBA将公式的值写入单元格
【发布时间】:2018-04-23 16:22:56
【问题描述】:

如果我想将 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

【问题讨论】:

标签: vba excel


【解决方案1】:

你可以用这个:

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

【讨论】:

  • 删除了我的帖子,因为它本质上是相同的代码。
  • 如果我希望值是文本,某些公式给出的结果超过 15 个数字?
  • @KjeldOstersen,请更详细地解释您关于文本值15位数字的问题
  • @KjeldOstersen,有什么反馈吗?
【解决方案2】:

你可以使用Evaluate:

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

【讨论】:

    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 2018-12-19
    • 2015-05-08
    • 2022-11-22
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多