Stream Lining长代码

时间:2016-10-06 18:17:09

标签: excel vba excel-vba macros

我有以下代码通过多个命令按钮运行。只是想知道是否有任何流线方法。每个按钮在具有某些特征的流中工作。我相信有办法可以减少多余的垃圾。

Private Sub CommandButton1_Click()
ActiveSheet.Unprotect "bir@2016"
 Range("A17").Select
    ActiveCell.FormulaR1C1 = "Research"
    Sheets("Questionnaire").Select
    Sheets("Questionnaire").Range("A1").Select
    Sheets("Analyst Score").Select
    Sheets("Questionnaire").Select
ActiveSheet.Protect "bir@2016"
End Sub

Private Sub CommandButton10_Click()
ActiveWorkbook.Unprotect "bir@2016"
Sheets("Investigation Comments Input").Visible = False
Sheets("Analyst Score").Visible = False
Sheets("Questionnaire").Select
ActiveWorkbook.Protect "bir@2016"
End Sub

Private Sub CommandButton11_Click()
ActiveWorkbook.Unprotect "bir@2016"
Sheets("Report Template").Visible = True
Sheets("Report Template").Select
Sheets("Report Template").Range("B4").Select
ActiveSheet.Protect "bir@2016"
End Sub

Private Sub Commandbutton2_Click()
ActiveSheet.Unprotect "bir@2016"
Range("A17").Select
    ActiveCell.FormulaR1C1 = "Quality Check"
    Sheets("Questionnaire").Select
    Sheets("Questionnaire").Range("A1").Select
    Sheets("Analyst Score").Select
    Sheets("Questionnaire").Select

Sheets("Questionnaire").Range("W1").Select
    ActiveCell.Value = Time
Sheets("Questionnaire").Range("A1").Select

ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton3_Click()
ActiveSheet.Unprotect "bir@2016"
Range("A17,B17,B1,C1,B3:B5,B7,H19:I127,O19:O127,K19:K127,L19:L127").Select
    Range("H19").Activate
    Selection.ClearContents
Sheets("Questionnaire").Select
Sheets("Questionnaire").Range("W1:X1,Z1:AE1").Select
ActiveSheet.Unprotect "bir@2016"
Selection.ClearContents
Sheets("Analyst Score").Select
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton4_Click()
ActiveSheet.Unprotect "bir@2016"
ActiveSheet.Unprotect "bir@2016"
Range("I19:I127").Select
    Range("I19").Activate
    Selection.ClearContents
    Range("N7").Select
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton5_Click()
ActiveSheet.Unprotect "bir@2016"
ActiveWindow.ScrollRow = 9
Range("A19").Select
ActiveWindow.FreezePanes = True
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton6_Click()
ActiveSheet.Unprotect "bir@2016"
ActiveWindow.FreezePanes = False
    ActiveWindow.SmallScroll Down:=-33
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton7_Click()
ActiveSheet.Unprotect "bir@2016"
ActiveSheet.Unprotect "bir@2016"
Range("H19:H127").Select
    Range("H19").Activate
    Selection.ClearContents
    Range("N7").Select
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton8_Click()
ActiveSheet.Unprotect "bir@2016"
Rows("17:127").Select
    Selection.EntireRow.Hidden = True
    Range("H5").Select
Range("Z:Z,AA:AA,AB:AB").Select
    Range("AB9").Activate
    Selection.EntireColumn.Hidden = True
    Range("M14").Select
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub CommandButton9_Click()
ActiveSheet.Unprotect "bir@2016"
Rows("17:127").Select
    Selection.EntireRow.Hidden = False
    Range("H5").Select
    Range("Z:Z,AA:AA,AB:AB").Select
    Range("AB9").Activate
    Selection.EntireColumn.Hidden = False
    Range("M14").Select
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub DEWS_Click()
ActiveSheet.Unprotect "bir@2016"
 Range("A17").Select
    ActiveCell.FormulaR1C1 = "Dews"
    Sheets("Questionnaire").Select
Sheets("Questionnaire").Range("W1").Select
    ActiveSheet.Unprotect "bir@2016"
    ActiveCell.Value = Time
    Range("W2").Value = Date
Sheets("Questionnaire").Range("A1").Select
ActiveSheet.Protect "bir@2016"
End Sub
Private Sub Worksheet_Calculate()
If Range("E5").Value < 1 Then Me.Shapes("CommandButton2").Visible = False
If Range("E5").Value > 1 Then Me.Shapes("CommandButton2").Visible = True
End Sub

1 个答案:

答案 0 :(得分:0)

有很多方法可以缩短您的代码:

1)从Kyle的评论开始,减少你的选择陈述。

2)如果您希望直观地整理您的代码,请更好地利用空白区域。

3)在commandbutton4,commandbutton7中,您取消保护同一张纸两次。

除了上述内容之外,如果您需要这些按钮中的每一个,都没有太多工作要做。你确定你不能组合按钮吗?