VBA - 使用和选择命令太慢。如何更好地优化它以更快地运行?

时间:2017-09-29 17:20:56

标签: excel vba excel-vba

我正在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

2 个答案:

答案 0 :(得分:2)

以下是移除SelectSelection后的情况:

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中进行选择和激活。