使用清除按钮在单独的工作簿中清除单元格,复选框和组合框

时间:2017-10-04 11:23:18

标签: excel vba excel-vba

我正在使用大约10个单独的Excel工作簿和一个跟踪工作簿。我在所有10个工作簿上都有清晰的按钮,但是想要在跟踪工具簿上清除所有按钮,这样我的员工就不必打开每个工作簿来清除其内容。每个工作簿中的VBA代码如下所示:

Private Sub CommandButton1_Click()
    Sheet1.ComboBox172.Text = "-"
    Sheet1.ComboBox174.Text = "-"
    Sheet1.ComboBox175.Text = "-"

    Sheet1.CheckBox1.Value = False
    Sheet1.CheckBox2.Value = False
    Sheet1.CheckBox3.Value = False

    Sheet1.TextBox1.Text = ""
    Sheet1.TextBox2.Text = ""
    Sheet1.TextBox3.Text = ""
    Sheet1.TextBox4.Text = ""
    Sheet1.TextBox5.Text = ""
    Sheet1.TextBox6.Text = ""
    Sheet1.TextBox7.Text = ""
    Sheet1.TextBox9.Text = ""
    Sheet1.TextBox10.Text = ""
    Sheet1.TextBox11.Text = ""
    Sheet1.TextBox12.Text = ""

    Sheet1.Range("D9:D21").Value = "-"
    Sheet1.Range("D25:D36").Value = "-"
    Sheet1.Range("D42:D52").Value = "-"
    Sheet1.Range("D56:D60").Value = "-"
    Sheet1.Range("D65:D67").Value = "-"
    Sheet1.Range("D71:D76").Value = "-"
    Sheet1.Range("D80:D83").Value = "-"
    Sheet1.Range("D87:D91").Value = "-"
    Sheet1.Range("D95:D100").Value = "-"
    Sheet1.Range("D104:D106").Value = "-"
    Sheet1.Range("L9:L20").Value = "-"
    Sheet1.Range("L25:L35").Value = "-"
    Sheet1.Range("L42:L52").Value = "-"
    Sheet1.Range("L56:L61").Value = "-"
    Sheet1.Range("L65:L67").Value = "-"
    Sheet1.Range("L71:L75").Value = "-"
    Sheet1.Range("L80:L82").Value = "-"
    Sheet1.Range("L87:L90").Value = "-"
    Sheet1.Range("L95:L100").Value = "-"

    Dim Shp As Shape

    For Each Shp In Sheet1.Shapes
        If Not (Shp.Type = msoOLEControlObject Or Shp.Type = msoFormControl Or Shp.Type = msoPicture) Then Shp.Delete
    Next Shp

    For Each Shp In Sheet2.Shapes
        If Not (Shp.Type = msoOLEControlObject Or Shp.Type = msoFormControl Or Shp.Type = msoPicture) Then Shp.Delete
    Next Shp

    Sheet2.ComboBox2.Text = "-"
    Sheet2.ComboBox3.Text = "-"
    Sheet2.ComboBox4.Text = "-"

    Sheet2.CheckBox1.Value = False
    Sheet2.CheckBox2.Value = False
    Sheet2.CheckBox3.Value = False
    Sheet2.CheckBox4.Value = False
    Sheet2.CheckBox5.Value = False
    Sheet2.CheckBox8.Value = False
    Sheet2.CheckBox9.Value = False
    Sheet2.CheckBox10.Value = False
    Sheet2.CheckBox11.Value = False

    Sheet2.Range("F9:F9").Value = 0
    Sheet2.Range("F11:F11").Value = 0
    Sheet2.Range("F15:F15").Value = 0
    Sheet2.Range("F17:F17").Value = 0
    Sheet2.Range("F21:F21").Value = 0
    Sheet2.Range("F23:F23").Value = 0
    Sheet2.Range("F27:F27").Value = 0
    Sheet2.Range("F29:F29").Value = 0
    Sheet2.Range("F35:F35").Value = 0
    Sheet2.Range("F37:F37").Value = 0
    Sheet2.Range("F39:F39").Value = 0
    Sheet2.Range("F45:F45").Value = 0
    Sheet2.Range("F47:F47").Value = 0
    Sheet2.Range("F53:F53").Value = 0
    Sheet2.Range("F55:F55").Value = 0
    Sheet2.Range("K35:K35").Value = 0
    Sheet2.Range("J9:M9").ClearContents
    Sheet2.Range("J15:M15").ClearContents
    Sheet2.Range("J21:M21").ClearContents
    Sheet2.Range("J27:M27").ClearContents

    Sheet3.ComboBox1.Text = "-"
    Sheet3.ComboBox2.Text = "-"
    Sheet3.ComboBox3.Text = "-"
    Sheet3.ComboBox4.Text = "-"
    Sheet3.ComboBox6.Text = "-"

    Sheet3.TextBox1.Text = ""
    Sheet3.TextBox2.Text = ""
    Sheet3.TextBox3.Text = ""
    Sheet3.TextBox5.Text = ""
    Sheet3.TextBox6.Text = ""

    Sheet3.CheckBox1.Value = False
    Sheet3.CheckBox2.Value = False
    Sheet3.CheckBox3.Value = False
    Sheet3.CheckBox4.Value = False
    Sheet3.CheckBox5.Value = False
    Sheet3.CheckBox6.Value = False
    Sheet3.CheckBox7.Value = False
    Sheet3.CheckBox8.Value = False
    Sheet3.CheckBox9.Value = False
    Sheet3.CheckBox10.Value = False
    Sheet3.CheckBox11.Value = False
    Sheet3.CheckBox12.Value = False
    Sheet3.CheckBox13.Value = False
    Sheet3.CheckBox14.Value = False
    Sheet3.CheckBox15.Value = False
    Sheet3.CheckBox16.Value = False
    Sheet3.CheckBox17.Value = False
    Sheet3.CheckBox18.Value = False
    Sheet3.CheckBox19.Value = False
    Sheet3.CheckBox20.Value = False
    Sheet3.CheckBox21.Value = False
    Sheet3.CheckBox22.Value = False
    Sheet3.CheckBox23.Value = False
    Sheet3.CheckBox24.Value = False
    Sheet3.CheckBox25.Value = False
    Sheet3.CheckBox26.Value = False
    Sheet3.CheckBox27.Value = False
    Sheet3.CheckBox28.Value = False
    Sheet3.CheckBox29.Value = False
    Sheet3.CheckBox30.Value = False
    Sheet3.CheckBox31.Value = False
    Sheet3.CheckBox32.Value = False
    Sheet3.CheckBox33.Value = False
    Sheet3.CheckBox34.Value = False
    Sheet3.CheckBox35.Value = False
    Sheet3.CheckBox36.Value = False
    Sheet3.CheckBox37.Value = False
    Sheet3.CheckBox38.Value = False
    Sheet3.CheckBox39.Value = False
    Sheet3.CheckBox41.Value = False
    Sheet3.CheckBox43.Value = False

    Sheet3.Range("C9:C9").Value = ""
    Sheet3.Range("C11:C11").Value = ""
    Sheet3.Range("H9:H9").Value = ""
    Sheet3.Range("H11:H11").Value = ""
    Sheet3.Range("L9:L9").Value = ""
    Sheet3.Range("L11:L11").Value = ""
    Sheet3.Range("L17:N17").Value = 0
    Sheet3.Range("L18:N18").Value = 0
    Sheet3.Range("L19:N19").Value = 0
    Sheet3.Range("L20:N20").Value = 0
End Sub

我在跟踪表上的清除按钮引用任何其他工作簿时遇到问题。

1 个答案:

答案 0 :(得分:0)

您可以做的是创建一个带有工作簿参数的sub,以清除传递的工作簿。类似的东西:

Sub ClearWorkBook(wb As Workbook)
    Dim Shp As Shape

    wb.Sheets("Sheet1").ComboBox172.Text = "-"
    wb.Sheets("Sheet1").ComboBox174.Text = "-"
    wb.Sheets("Sheet1").ComboBox175.Text = "-"

    wb.Sheets("Sheet1").CheckBox1.Value = False
    wb.Sheets("Sheet1").CheckBox2.Value = False
    wb.Sheets("Sheet1").CheckBox3.Value = False

    wb.Sheets("Sheet1").TextBox1.Text = ""
    wb.Sheets("Sheet1").TextBox2.Text = ""
    wb.Sheets("Sheet1").TextBox3.Text = ""
    wb.Sheets("Sheet1").TextBox4.Text = ""
    wb.Sheets("Sheet1").TextBox5.Text = ""
    wb.Sheets("Sheet1").TextBox6.Text = ""
    wb.Sheets("Sheet1").TextBox7.Text = ""
    wb.Sheets("Sheet1").TextBox9.Text = ""
    wb.Sheets("Sheet1").TextBox10.Text = ""
    wb.Sheets("Sheet1").TextBox11.Text = ""
    wb.Sheets("Sheet1").TextBox12.Text = ""

    wb.Sheets("Sheet1").Range("D9:D21,D25:D36,D42:D52,D56:D60,D65:D67,D71:D76,D80:D83,D87:D91,D95:D100,D104:D106,L9:L20,L25:L35,L42:L52,L56:L61,L65:L67,L71:L75,L80:L82,L87:L90,L95:L100").Value = "-"

    For Each Shp In wb.Sheets("Sheet1").Shapes
        If Not (Shp.Type = msoOLEControlObject Or Shp.Type = msoFormControl Or Shp.Type = msoPicture) Then Shp.Delete
    Next Shp

    For Each Shp In wb.Sheets("Sheet2").Shapes
        If Not (Shp.Type = msoOLEControlObject Or Shp.Type = msoFormControl Or Shp.Type = msoPicture) Then Shp.Delete
    Next Shp

    wb.Sheets("Sheet2").ComboBox2.Text = "-"
    wb.Sheets("Sheet2").ComboBox3.Text = "-"
    wb.Sheets("Sheet2").ComboBox4.Text = "-"

    wb.Sheets("Sheet2").CheckBox1.Value = False
    wb.Sheets("Sheet2").CheckBox2.Value = False
    wb.Sheets("Sheet2").CheckBox3.Value = False
    wb.Sheets("Sheet2").CheckBox4.Value = False
    wb.Sheets("Sheet2").CheckBox5.Value = False
    wb.Sheets("Sheet2").CheckBox8.Value = False
    wb.Sheets("Sheet2").CheckBox9.Value = False
    wb.Sheets("Sheet2").CheckBox10.Value = False
    wb.Sheets("Sheet2").CheckBox11.Value = False

    wb.Sheets("Sheet2").Range("F9,F11,F15,F17,F21,F23,F27,F29,F35,F37,F39,F45,F47,F53,F55,K35").Value = 0
    wb.Sheets("Sheet2").Range("J9:M9,J15:M15,J21:M21,J27:M27").ClearContents

    wb.Sheets("Sheet3").ComboBox1.Text = "-"
    wb.Sheets("Sheet3").ComboBox2.Text = "-"
    wb.Sheets("Sheet3").ComboBox3.Text = "-"
    wb.Sheets("Sheet3").ComboBox4.Text = "-"
    wb.Sheets("Sheet3").ComboBox6.Text = "-"

    wb.Sheets("Sheet3").TextBox1.Text = ""
    wb.Sheets("Sheet3").TextBox2.Text = ""
    wb.Sheets("Sheet3").TextBox3.Text = ""
    wb.Sheets("Sheet3").TextBox5.Text = ""
    wb.Sheets("Sheet3").TextBox6.Text = ""

    wb.Sheets("Sheet3").CheckBox1.Value = False
    wb.Sheets("Sheet3").CheckBox2.Value = False
    wb.Sheets("Sheet3").CheckBox3.Value = False
    wb.Sheets("Sheet3").CheckBox4.Value = False
    wb.Sheets("Sheet3").CheckBox5.Value = False
    wb.Sheets("Sheet3").CheckBox6.Value = False
    wb.Sheets("Sheet3").CheckBox7.Value = False
    wb.Sheets("Sheet3").CheckBox8.Value = False
    wb.Sheets("Sheet3").CheckBox9.Value = False
    wb.Sheets("Sheet3").CheckBox10.Value = False
    wb.Sheets("Sheet3").CheckBox11.Value = False
    wb.Sheets("Sheet3").CheckBox12.Value = False
    wb.Sheets("Sheet3").CheckBox13.Value = False
    wb.Sheets("Sheet3").CheckBox14.Value = False
    wb.Sheets("Sheet3").CheckBox15.Value = False
    wb.Sheets("Sheet3").CheckBox16.Value = False
    wb.Sheets("Sheet3").CheckBox17.Value = False
    wb.Sheets("Sheet3").CheckBox18.Value = False
    wb.Sheets("Sheet3").CheckBox19.Value = False
    wb.Sheets("Sheet3").CheckBox20.Value = False
    wb.Sheets("Sheet3").CheckBox21.Value = False
    wb.Sheets("Sheet3").CheckBox22.Value = False
    wb.Sheets("Sheet3").CheckBox23.Value = False
    wb.Sheets("Sheet3").CheckBox24.Value = False
    wb.Sheets("Sheet3").CheckBox25.Value = False
    wb.Sheets("Sheet3").CheckBox26.Value = False
    wb.Sheets("Sheet3").CheckBox27.Value = False
    wb.Sheets("Sheet3").CheckBox28.Value = False
    wb.Sheets("Sheet3").CheckBox29.Value = False
    wb.Sheets("Sheet3").CheckBox30.Value = False
    wb.Sheets("Sheet3").CheckBox31.Value = False
    wb.Sheets("Sheet3").CheckBox32.Value = False
    wb.Sheets("Sheet3").CheckBox33.Value = False
    wb.Sheets("Sheet3").CheckBox34.Value = False
    wb.Sheets("Sheet3").CheckBox35.Value = False
    wb.Sheets("Sheet3").CheckBox36.Value = False
    wb.Sheets("Sheet3").CheckBox37.Value = False
    wb.Sheets("Sheet3").CheckBox38.Value = False
    wb.Sheets("Sheet3").CheckBox39.Value = False
    wb.Sheets("Sheet3").CheckBox41.Value = False
    wb.Sheets("Sheet3").CheckBox43.Value = False

    wb.Sheets("Sheet3").Range("C9,C11,H9,H11,L(,L11").Value = ""
    wb.Sheets("Sheet3").Range("L17:N17,L18:N18,L19:N19,L20:N20").Value = 0

End Sub

此工作簿将存储在跟踪工作簿中。然后在该跟踪工作簿中,您将拥有类似的内容:

Sub ClearWorkBooks()
    Dim wb As Workbook
    For Each wb In Workbooks
        If wb.Name <> "Tracking" Then ClearWorkBook wb
    Next wb
End Sub

请注意,我通过使用非连续范围将您的许多语句折叠为一个来简化代码。可以进一步精简,尽管它与您的问题有些相关。您可能必须在ClearWorkBooks()中对循环进行操作,以便它只在您要清除的工作簿上运行。