我正在尝试动态地将validation
(使用DataValidation:List)添加到工作表中的范围。我记录所产生的下面的代码宏:Excel VBA - 使用单元格引用范围
With Worksheets("Clusters").Range("C2:C100").Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Managers"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
用于静态范围C2这是工作的罚款:C100,但该列可以不总是C.我有一个可变cMANFCM包含列数。我尝试编辑使用此代码:
With Worksheets("Clusters").Range(Cells(2,cMANFCM), Cells(100, cMANFCM)).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="=Managers"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
为什么不这项工作,如何解决呢?
尝试'随着工作表(“集群”)范围(“A2:A100”)。偏移(,cMANFCM -1)。验证' – 2013-02-27 22:26:59
或'Worksheets(“Clusters”)。Cells(2,cMANFCM).Resize(100,1)' – 2013-02-27 22:41:10
'Cells()'without quali fying工作表总是引用ActiveSheet,所以不要使用(例如)sht.Range(Cells(1,1),Cells(100,1))''你应该使用'sht.Range(sht.Cells(1, 1),sht.Cells(100,1))' – 2013-02-27 23:21:41