【问题标题】:Form/code is not transfering to excel wb/ws, and when/if it does, its slow表单/代码没有传输到 excel wb/ws,当/如果它传输,它的速度很慢
【发布时间】:2019-04-08 17:45:31
【问题描述】:

我已经编写了一个代码来将表单数据传输到电子表格。它工作正常,但前几天晚上我开始对其进行更改,却没有意识到这是我的核心代码并且没有备份。 (很晚了)我不记得我改变了什么。现在它不会 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 = ""

结束子

【问题讨论】:

  • Else 之后应该发生什么事情吗?
  • 没有。如果没有它,它就不允许我编译。另外,代码中有一个“End Sub”,我只是忘了放在这里。

标签: excel vba transfer


【解决方案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 或对您有意义的任何内容。

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

【讨论】:

  • 谢谢!在我问这个问题之前,你支持了我正在做的事情。我只是错过了您在回答中指出的几件事!感谢您启动这个旧大脑!非常感谢!
  • 很高兴我能帮上忙! :)
猜你喜欢
  • 2012-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
相关资源
最近更新 更多