一旦我尝试拨打MyRange.Validation.Add Type:= 3
,就会出现问题。我一直收到错误1004.我已经搜索了互联网,似乎无法找到解决方案。
我的猜测是我的格式正确但在我的代码中的错误位置才能正常工作?我真的是新的两个VBA(上周五),但它似乎并不困难(我知道C ++)。我只是希望有更多经验的人可以指出错误。
非常感谢任何帮助。如果我对我的问题的描述含糊不清,请告诉我您需要的其他信息,我会非常乐意发布。
Motor Sub:
Sub Motor(TotalLoad As Integer)
Dim Phase As Range, HP As Range, Voltage As Range, LoadType As Range, RatedkW As Range
Dim i As Integer 'Initializes the variables used.
Dim HPList(5) As String
For i = 4 To TotalLoad 'For loop that loops through till the value of TotalLoad
Set LoadType = Range(Cells(i, 3), Cells(i, 3)) 'Sets the range for the "Load Type" Columns.
Set Phase = Range(Cells(i, 6), Cells(i, 6)) 'Sets the range for the "Phase" Columns.
Set HP = Range(Cells(i, 7), Cells(i, 7)) 'Sets the range for the "HP" Columns.
Set Voltage = Range(Cells(i, 8), Cells(i, 8)) 'Sets the range for the "Voltage" Columns.
Set RatedkW = Range(Cells(i, 9), Cells(i, 9)) 'Sets the range for the "Rated kW" Columns.
If LoadType.Value = "Motor" Then 'If statement checking if "Motor" has be selected in the cell "Load Type".
Call UnlockCell(Phase)
If Phase.Value = "Single" Then
Call UnlockCell(HP)
Call UnlockCell(Voltage)
Call UnlockCell(RatedkW)
HP.Validation.Delete
HP.Validation.Add Type:=3
HP.Validation.Add Formula1:="list 1, list 2, list 3, list 4"
'other codes-----------
End If 'End of all the if statements.
Next i 'Increments the value of i within the for loop.
End Sub
答案 0 :(得分:2)
最后在我的代码段后面删除段后想出来......错误来自我的工作簿受到保护。我想你不能在受保护的工作簿中的vba中创建一个列表?是的,我确实设置了它,以便它允许宏更改而不是用户保护更改用户。
我想发布我的解决方案,以防其他人遇到同样的问题。
再次感谢Paul的帮助!!
这是修复我的问题的代码。关键是unprotect,运行代码,再次保护。
Unprotect ("PASSWORD")
With HP.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="""List 1"", ""List 2"""
End With
Protect Password:="PASSWORD", UserInterFaceOnly:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True