Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 使用“清除”按钮清除单独工作簿中的单元格、复选框和组合框_Vba_Excel - Fatal编程技术网

Vba 使用“清除”按钮清除单独工作簿中的单元格、复选框和组合框

Vba 使用“清除”按钮清除单独工作簿中的单元格、复选框和组合框,vba,excel,Vba,Excel,我正在使用大约10个单独的excel工作簿和一个跟踪工作簿。我在所有10个工作簿上都有清除按钮,但希望在跟踪工作簿上有一个清除所有按钮,这样我的员工就不必打开每个工作簿来清除其内容。每个工作簿中的VBA代码如下所示: Private Sub CommandButton1_Click() Sheet1.ComboBox172.Text = "-" Sheet1.ComboBox174.Text = "-" Sheet1.ComboBox175.Text = "-"

我正在使用大约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

我对引用任何其他工作簿的跟踪表上的“清除”按钮有问题。

您可以使用工作簿参数创建一个子项,以清除传递的工作簿。比如:

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
此工作簿将存储在跟踪工作簿中。然后,在该跟踪工作簿中,您将有如下内容:

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
子ClearWorkbook()
将wb设置为工作簿
对于工作簿中的每个wb
如果wb.Name为“跟踪”,则ClearWorkBook wb
下一个wb
端接头

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

您可以使用工作簿参数创建一个子项,以清除传递的工作簿。比如:

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
此工作簿将存储在跟踪工作簿中。然后,在该跟踪工作簿中,您将有如下内容:

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
子ClearWorkbook()
将wb设置为工作簿
对于工作簿中的每个wb
如果wb.Name为“跟踪”,则ClearWorkBook wb
下一个wb
端接头

注意,我通过使用非连续范围将许多语句折叠成一个语句,从而简化了代码。进一步的精简是可能的,尽管这与你的问题有些相切。您可能需要在
ClearWorkBooks()中处理循环
这样它就只在您想要清除的工作簿上运行。

为什么不使用循环来减少重复的代码?对于将代码插入Excel来说还是相当新的…大多数代码都是我在网上找到的东西为什么不使用循环来减少重复的代码?对于将代码插入Excel来说还是相当新的…大多数代码都是我在网上找到的东西在线找到