简体   繁体   English

动态将公式添加到Excel单元格

[英]Adding a formula dynamically to an excel cell

Is there a way (VB?) to add a formula dynamically to an excel cell ? 有没有办法(VB?)向Excel单元格动态添加公式

I have a cell with conditions true and false, and they change based on a checkbox. 我有一个条件为true和false的单元格,它们会基于复选框进行更改。

And I have another cell, which has a formula in it. 我还有另一个单元格,里面有一个公式。 This formula should be used if the checkbox is unchecked . 如果未选中此复选框,则应使用此公式 If the checkbox is checked, then user should be able insert value manually (without any formula prompting there).So the formula should not be there. 如果选中该复选框,则用户应该可以手动插入值 (不带任何公式提示),因此公式不应在那里。

I was thinking of a solution where I would add the formula to the cell if checkbox is unchecked, and then if the checkbox is checked, then I would clear the cell. 我在考虑一种解决方案,如果未选中复选框,则将公式添加到单元格中,然后如果选中了复选框,则将清除该单元格。

How could this be done? 怎么办呢? I'm not very familiar with excel coding and VBA. 我对excel编码和VBA不太熟悉。

Ok you need a trigger on TRUE/FALSE cell to execut the next VBA code, right click on sheet name and click "View Code" and enter this code: 确定,您需要在TRUE / FALSE单元格上触发以执行下一个VBA代码,右键单击工作表名称,然后单击“查看代码”,然后输入以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A5:A5")) Is Nothing Then 'define adress of your True/Flase cell
 If Target.Cells.Value = False then
 Range("B5").formula = "=enter your formula" 'define adress for cell with formula aswell
 else 
 Range("B5").value = ""
 end if
end if
end sub

well you could use: 您可以使用:

if userform1.checkbox.checked = false then
range("A1").formula = "=myformula"
else
range("A1").value = ""
end if

you need to insert the code into the userform checkbox click or change event both should have same effect, just double click on the checkbox in userform and it will take you to the click event or replace the click with "change", hope that's what you meant to achieve, cheers PS. 您需要将代码插入到userform复选框中click或change事件都应具有相同的效果,只需双击userform中的复选框,它将带您进入click事件或将click替换为“ change”,希望这就是您所需要的。意味着要实现,为PS欢呼。 thanks for suggestions @99moorem 感谢您的建议@ 99moorem

If you have a classical Excel CheckBox, you can add a Linked Cell which will be True or False. 如果您有经典的Excel CheckBox,则可以添加一个为True或False 的链接单元格

In the following code, your Linked Cell is in A1 , the cell with the formula to use in B1 and the cell that need to be empty or filled by formula is in C1 . 在以下代码中,您的链接单元格A1具有要B1 使用的公式单元格,并且需要为空或用公式填充单元格C1

You'll need to specify the Sheet_Name (can be differents) and place that code directly into the Sheet "module" in VBA (press Alt+F11 and double-click on the sheet (in the left panel) with the Linked Cell, then just paste and edit regarding your specifications) 您需要指定Sheet_Name (可以不同),然后将代码直接放入VBA中的Sheet“模块”中(按Alt+F11并双击带有链接单元格的表(在左侧面板中),然后只需粘贴并编辑有关您的规范)

Private Sub Worksheet_Change(ByVal Target As Range)
Dim LinkedCell As Range, _
    FormulaCell As Range, _
    ChangingCell As Range

Set LinkedCell = Sheets("Sheet_Name").Range("A1")
Set FormulaCell = Sheets("Sheet_Name").Range("B1")
Set ChangingCell = Sheets("Sheet_Name").Range("C1")


If Application.Intersect(Target, LinkedCell) Is Nothing Then
    'not in linked cell
Else
    'in linked cell
    If LinkedCell.Value2 <> True Then
        'Unchecked
        ChangingCell.FormulaLocal = ChangingCell.FormulaLocal
    Else
        'Checked
        ChangingCell.FormulaLocal = vbNullString
    End If
End If

Set LinkedCell = Nothing
Set FormulaCell = Nothing
Set ChangingCell = Nothing

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM