我正在With
命令中选择一个范围并使用该选择,众所周知,.Selection
命令会减慢进程的速度。编写此代码的更好方法是什么,以便更快地运行它?
这是我的代码:
Sheets(Currentsheetname).Range("A" & SelRowNumber + 1 & ":A" & lastrow).Select
With .Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="Remove"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Warning"
.InputMessage = ""
.ErrorMessage = "Please select a value from the list available in the selected cell."
.ShowInput = True
.ShowError = True
End With
答案 0 :(得分:2)
以下是移除Select
和Selection
后的情况:
With Sheets(Currentsheetname).Range("A" & SelRowNumber + 1 & ":A" & lastrow).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="Remove"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Warning"
.InputMessage = ""
.ErrorMessage = "Please select a value from the list available in the selected cell."
.ShowInput = True
.ShowError = True
End With
答案 1 :(得分:2)
这是删除选择并加快处理的方法:
With Sheets(Currentsheetname).Range("A" & SelRowNumber + 1 & ":A" & lastrow).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="Remove"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "Warning"
.InputMessage = ""
.ErrorMessage = "Please select a value from the list available in the selected cell."
.ShowInput = True
.ShowError = True
End With
始终尽量避免在VBA中进行选择和激活。