表单/代码不会转移到excel wb / ws,如果/如果它转移,它的速度很慢

问题描述 投票:0回答:1

我已经整理了一个代码来将表单数据传输到电子表格。它工作正常,但另一天晚上我开始对它进行更改,却没有意识到这是我的核心代码而且没有备份。 (已经很晚了)我不记得我改变了什么。现在它不会在100%的时间内传输表单数据,当它发生时,它的速度很慢。我的意思是...像35秒一样更新缓慢。任何人都可以提供任何有关如何改进和/或修复的提示或帮助吗?任何帮助,将不胜感激。

 Sub CommandButton1_Click()
If ComboBox2.Value = "" Or ComboBox3.Value = "" Or ComboBox6.Value = ""      Or ComboBox7.Value = "" Or ComboBox8.Value = "" Or ComboBox9.Value = "" Then
    MsgBox ("The form is not complete")
    Exit Sub
Else
End If

 Workbooks("Language Line April AM - 2019.xlsm").Worksheets("2019 LL DB").Activate

 Dim lastrow As Long, count As Long

 lastrow = Sheet7.Cells(Rows.count, 1).End(xlUp).Row
 lastrow = lastrow + 1

 Cells(lastrow, 1) = ComboBox6.Value 'Tester
 Cells(lastrow, 2) = TextBox4.Value 'Date
 Cells(lastrow, 3) = TextBox5.Value 'Meridiem
 Cells(lastrow, 4) = TextBox6.Value 'LOB
 Cells(lastrow, 5) = TextBox7.Value 'Language
 Cells(lastrow, 6) = ComboBox4.Value 'Phone Options
 Cells(lastrow, 7) = ComboBox6.Value 'Representative
 Cells(lastrow, 8) = TextBox4.Value 'Supervisor
 Cells(lastrow, 9) = TextBox5.Value 'Manager
 Cells(lastrow, 10) = TextBox6.Value 'Director
 Cells(lastrow, 11) = TextBox7.Value 'Location
 Cells(lastrow, 12) = ComboBox7.Value 'Whisper
 Cells(lastrow, 13) = ComboBox8.Value 'UAD Indicator
 Cells(lastrow, 14) = TextBox8.Value 'Results for system
 Cells(lastrow, 15) = ComboBox9.Value 'Free Translator Offer
 Cells(lastrow, 16) = TextBox9.Value 'Results for Rep offer
 Cells(lastrow, 17) = TextBox12.Value 'Comments

 ActiveWorkbook.Save

 MsgBox "Entry has been logged!"

    ComboBox2.Value = ""
    ComboBox3.Clear
    ComboBox4.Clear
    ComboBox4.Value = ""
    ComboBox5.Clear
    ComboBox6.Value = ""
    ComboBox7.Value = ""
    ComboBox8.Value = ""
    ComboBox9.Clear

    TextBox12.Value = ""

    CheckBox1.Value = False

    TextBox4.Value = ""
    TextBox5.Value = ""
    TextBox6.Value = ""
    TextBox7.Value = ""
    TextBox8.Value = ""
    TextBox9.Value = ""
    TextBox13.Value = ""

结束子

excel vba transfer
1个回答
1
投票

使用exit不是一个好习惯。您可以使用类似的东西:

Sub CommandButton1_Click()
If ComboBox2.Value = "" Or ComboBox3.Value = "" Or ComboBox6.Value = ""      Or ComboBox7.Value = "" Or ComboBox8.Value = "" Or ComboBox9.Value = "" Then
    MsgBox ("The form is not complete")
Else
'your code here

End If
end Sub

使用.Activate方法并留下excel来确定哪些cellsworksheets或工作簿被引用也不是一个好习惯。

试试这个:

Dim sht1 As Worksheet
Dim book as Workbook
Set book=Workbooks("Language Line April AM - 2019.xlsm")
Set sht1 = book.Worksheets("2019 LL DB")

然后您可以像这样引用单元格:

sht1.cells(lastrow,1).value=""

这同样适用于sheet7。它和2019 LL DB一样吗?然后应该相应地引用它。

不引用控件所属的用户表单也不是一个好习惯。试试这个:

userform.ComboBox2.Value="" 'where userform is your userform's name

或者,如果你想避免重复userform.xxx,你可以试试这个:

With userform 'your userform's name
    .combobox1.Value = ""
    .textbox1.Value = ""
End With

最后,尝试为变量和对象使用有意义的名称。例如,combobox6可以重命名为testerComboBox或任何对你有意义的东西。

使用良好实践将帮助您更好地跟踪代码,并且您会发现更容易进行故障排除和调试。

© www.soinside.com 2019 - 2024. All rights reserved.