VBA - 从验证列表中选择值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26834634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:11:00  来源:igfitidea点击:

VBA - Select value from Validation List

excelvbavalidationexcel-vba

提问by PootyToot

I have some code in a spreadsheet which should change the value in a validaiton list on a worksheet called 'analysis'. The value in cell B1 on the analysis list should change to FTE if the value 'Costs' is already selected, else it should select the value 'costs'.

我在电子表格中有一些代码,它应该更改名为“分析”的工作表上验证列表中的值。如果已选择值“成本”,则分析列表中单元格 B1 中的值应更改为 FTE,否则应选择值“成本”。

My code below doesn't seem to be changing the value in the validation list, and when I try to record a macro to get an idea of how a macro recording would record the validation change, the recorder doesn't record the validation change. Does anybody have any idea how to select a value from a validation list via VBA?

我下面的代码似乎没有更改验证列表中的值,当我尝试录制宏以了解宏录制如何记录验证更改时,记录器不会记录验证更改。有人知道如何通过 VBA 从验证列表中选择一个值吗?

My current code is below

我当前的代码如下

Sub ChangeValue()

Worksheets("Analysis").Select
With Worksheets("Analysis")
   If Range("B1").Value = "Costs" Then
      Range("B1").Value = "FTE"
   Else
      Range("B1").Value = "Costs"
   End If
End With

End Sub()

回答by PootyToot

This was a total oversight on my behalf, the code was written as Range rather than .Range. Fix below

这完全是我的疏忽,代码被编写为 Range 而不是 .Range。下面修复

Sub ChangeValue()

Worksheets("Analysis").Select
With Worksheets("Analysis")
   If .Range("B1").Value = "Costs" Then
      .Range("B1").Value = "FTE"
   Else
      .Range("B1").Value = "Costs"
   End If
End With

End Sub()