【问题标题】:Excel Drop down list of formulas or vba functions that will do the sameExcel 将执行相同操作的公式或 vba 函数的下拉列表
【发布时间】:2019-07-02 05:31:08
【问题描述】:

我可以有一个下拉列表,我可以在其中选择要使用的公式吗 我有一系列公式,即=indirect(address(row(),4)))*deposit,其中存款是一个命名范围,我希望能够选择使用哪个公式,基本上公式中唯一会改变的部分是命名范围

我不能只在数据验证中输入一个公式。 如果可以将公式复制到自定义 vba 函数中,我也可以,但不确定如何执行此操作

基本上,我需要能够从一个相对于当前单元格的单元格中获取一个美元值,并将其乘以指定范围之一中的百分比

【问题讨论】:

  • 您能否在下拉列表中使用带有命名范围名称的额外列,例如Z1 中的命名范围下拉列表,AA1 中的公式,然后写 =$D1 * INDIRECT($Z1) ?

标签: excel vba


【解决方案1】:

前段时间出于不同目的写了这篇文章,修改并添加了一些 cmets。

放置在 ThisWorkbook 中并通过在除第 4 列 (D:D) 以外的任何单元格中键入(不带引号)“_”来触发宏,然后按 Enter。

此外,您的命名范围应以“_”(例如“deposit_”)结尾,这是由于第二种情况的条件,并且是在编辑描述性文本单元格时避免触发的必要条件。

键入“-”并按 Enter 会删除验证和值。

Public Str As String

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If ActiveSheet.Index = 1 Then 'If you want to limit the reach of this
    With Target
        If .Count = 1 Then 'Limits to single cell to avoid errors
            If .Column <> 4 Then 'So that it can't refer to itself unless you try in one of them named ranges
                Select Case True
                    Case (.Value Like "_") 'Triggers if cell value equals "_"
                        For Each nm In ThisWorkbook.Names 'Adds all names to a string
                            Str = Str & "," & nm.Name
                        Next nm
                        .Validation.Delete 'Removes previous validation
                        .Validation.Add Type:=xlValidateList, Formula1:=Replace(Str, ",", "", 1, 1) 'Adds the list of names to validation
                        .Validation.ShowError = False 'Makes it possible edit without removing validation
                        .Select: SendKeys ("%{UP}") 'Triggers the dropdown list

                    Case (.Value Like "*_") 'Triggers if cell value ends with "_"
                        .FormulaR1C1 = "=RC4*" & .Value 'Applies formula

                    Case (.Value Like "-") 'Triggers if cell value equals "-"
                        .Validation.Delete 'Removes validation from cell
                        .Value = "" 'Removes value

                End Select
                Str = "" 'Empties out string
            End If
        End If
    End With
End If
End Sub

【讨论】:

  • 我还没有机会剖析你的代码,等我看过了再告诉你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-31
  • 1970-01-01
相关资源
最近更新 更多