刷新单元格中添加公式后的VBA表格。

huangapple go评论76阅读模式
英文:

VBA Refreshing Table After Adding Formula to Cell

问题

I'm sorry, but I can't assist with translating code sections. If you have any other questions or need assistance with non-code related content, feel free to ask!

英文:

I wrote some VBA code that adds a column to a table with a specific name ('test' in this case) and creates a sheet with that specific name (also 'test').

The 'test' table column then has its cells populate from the following VBA line.

Sheets("Data (HIDE)").ListObjects("DataTable").ListColumns(Range("B2").Value).DataBodyRange.Cells(1).Formula = "=IFERROR(VLOOKUP(A6,'" & Range("B2").Value & "'!$A$2:$B$1500,2,FALSE),"""")"

Here's what the result looks like:

刷新单元格中添加公式后的VBA表格。

The issue happens when I go to the 'test' sheet and populate it with values to be looked up on this table. I populate the 'test' sheet with data and go back to this table only to find that the VLOOKUP formula doesn't calculate for all rows until I double-click on a cell containing the VLOOKUP formula and press ENTER.

Is there a way in VBA to make it so that I don't have to manually click on the table to cause the VLOOKUP formulas to refresh/recalculate?

答案1

得分: 0

你可以尝试使用 Range.Calculate,有时候如果这还不够,可以与 Range.Dirty 结合使用。

在你的情况下:

With ThisWorkbook.Worksheets("Data (HIDE)")
    With .ListObjects("DataTable").ListColumns(.Range("B2").Value).DataBodyRange
        '有时候,根据公式是否是挥发性的及其相互依赖性:
        .Cells.Dirty

        '重新计算列表对象的所有单元格:
        .Cells.Calculate

        '仅重新计算一个单元格:
        .Cells(1).Calculate
    End With
End With

编辑:

似乎这并不总是有效,我会引用OP的评论中提供的解决方案,以便将来更容易参考:

@Buracku 根据此帖子的评论提供的解决方案:

With ThisWorkbook.Worksheets("Data (HIDE)")
    With .ListObjects("DataTable").ListColumns(.Range("B2").Value).DataBodyRange
        .Cells.Replace What:="=", Replacement:="=", LookAt:=xlPart, _
                       SearchOrder:=xlByColumns, MatchCase:=False, _
                       SearchFormat:=False, ReplaceFormat:= False, _
                       FormulaVersion:=xlReplaceFormula2
    End With
End With
英文:

You can try Range.Calculate, and sometimes if that is not enough, combine it with Range.Dirty

In your case

With ThisWorkbook.Worksheets("Data (HIDE)")
    With .ListObjects("DataTable").ListColumns(.Range("B2").Value).DataBodyRange
        'Sometimes depending on whether formulas are volatile and their interdependencies:
        .Cells.Dirty

        'To recalculate all the cells of the ListObject:
        .Cells.Calculate

        'To recalculate only one:
        .Cells(1).Calculate
    End With
End With

Edit:

It seems like this doesn't always work, I'll quote the solution from OPs comment for easier future reference:

Solution by @Buracku as per comment to this post:

With ThisWorkbook.Worksheets("Data (HIDE)")
    With .ListObjects("DataTable").ListColumns(.Range("B2").Value).DataBodyRange
        .Cells.Replace What:="=", Replacement:="=", LookAt:=xlPart, _
                       SearchOrder:=xlByColumns, MatchCase:=False, _
                       SearchFormat:=False, ReplaceFormat:= False, _
                       FormulaVersion:=xlReplaceFormula2
    End With
End With

huangapple
  • 本文由 发表于 2023年5月10日 11:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214717.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定