压缩重复的Excel用户窗体VBA代码

时间:2018-07-10 16:55:50

标签: excel vba excel-vba

我的宏太大了,因此我试图压缩代码,因为很多代码都是重复的。

这里是30个不同复选框重复30次的主要代码

// get the number of words of each length
df.groupBy(expr("length(word)")).count()

更改的唯一项目是CheckBox3和TextBox11,我试图大胆地确定它是否有效。

上述值仅改变+1。

我已经尝试了多个Dim命令,但是下一个却无法正常工作。

任何帮助都感激不尽。

1 个答案:

答案 0 :(得分:0)

将所有内容放入函数中,并将适当的值传递给它:

If CheckBox3.Value = True Then
    call myFunction (CheckBox3.Caption, textbox11.value)
End if

然后再次调用其他复选框和文本框的函数):

If CheckBox4.Value = True Then
    call myFunction (CheckBox4.Caption, textbox12.value)
End if

功能:

myFunction (myCheckboxCaption as string, myTextBoxValue as string)
    eRow = Sheet3.Cells(Rows.Count, 6).End(xlUp).Offset(1, 0).Row
    Cells(eRow, 7).Value = ComboBox1.Value
        If CheckBox29.Value = True Then
            Cells(eRow, 8).Value = Controls("CheckBox" & 29).Caption
                If CheckBox1.Value = True Then
                    Cells(eRow, 14).Value = "Yes"
                Else
                    Cells(eRow, 14).Value = "No"
                End If
            Cells(eRow, 15).Value = ComboBox6.Value
            Cells(eRow, 16).Value = ComboBox7.Value
                If CheckBox2.Value = True Then
                    Cells(eRow, 17).Value = "Yes"
                Else
                    Cells(eRow, 17).Value = "No"
                End If
            Cells(eRow, 18).Value = ComboBox8.Value
            Cells(eRow, 19).Value = TextBox5.Value
            Cells(eRow, 20).Value = TextBox6.Value
            Cells(eRow, 21).Value = TextBox7.Value
            Cells(eRow, 22).Value = TextBox8.Value
            Cells(eRow, 26).Value = TextBox9.Value
            Cells(eRow, 27).Value = TextBox10.Value
        Else
            Cells(eRow, 8).Value = "Sales Call"
            Cells(eRow, 15).Value = "-"
            Cells(eRow, 16).Value = "-"
            Cells(eRow, 18).Value = "-"
            Cells(eRow, 19).Value = "-"
            Cells(eRow, 20).Value = "-"
            Cells(eRow, 21).Value = "-"
            Cells(eRow, 22).Value = "-"
            Cells(eRow, 26).Value = "-"
            Cells(eRow, 27).Value = "-"
            Cells(eRow, 14).Value = "No"
            Cells(eRow, 17).Value = "No"
        End If
    Cells(eRow, 9).Value = ComboBox3.Value
    Cells(eRow, 10).Value = TextBox2.Value
    Cells(eRow, 11).Value = TextBox3.Value
    Cells(eRow, 12).Value = TextBox4.Value
    Cells(eRow, 13).Value = ComboBox4.Value
    Cells(eRow, 23).Value = ComboBox5.Value
    Cells(eRow, 24).Value = myCheckboxCaption
    Cells(eRow, 25).Value = myTextBoxValue
    Cells(eRow, 6).Value = TextBox1.Value
End function