【问题标题】:1004 error using combox selections to update a dataset使用组合框选择更新数据集时出现 1004 错误
【发布时间】:2015-01-27 23:23:15
【问题描述】:

我有一个用户窗体,用户可以使用它使用命令按钮滚动数据,其中一些显示在文本框中。 我有 2 个组合框,一个依赖于另一个,用例语句。我想使用这些组合框选择来更新原始数据,但我不断收到错误 1004。

我已经设置了CommandButton3_Click(),我希望用户在使用组合框做出选择后按下它。理想情况下,我还希望组合框中的选择覆盖文本框 1 和 3 中显示的值。不过,这很好。

我遇到问题的代码如下,Me.ComboxCat2NameMe.ComboxCat3Namecomboxbox 选择:

Worksheets("CurrentMonth").Range(Cells(Currentrow, 31)) = Me.ComboBoxCat2Name
Worksheets("CurrentMonth").Range(Cells(Currentrow, 33)) = Me.ComboBoxCat3Name

我的用户表单的完整代码如下:

Option Explicit


Dim Lastrow As Long
Dim Currentrow As Long


Private Sub ComboBoxCat2Name_Change()

'Clears 2nd ComboBox each time the 1st is picked so no previous selection   residual is left
Me.ComboBoxCat3Name = ""
'Contingent ComboBoxes that pull values from lots of named ranges as below
Select Case Me.ComboBoxCat2Name
    Case "B2C"
        Me.ComboBoxCat3Name.RowSource = "B2C"
    Case "B2B"
        Me.ComboBoxCat3Name.RowSource = "B2B"
    Case "Games Dev"
        Me.ComboBoxCat3Name.RowSource = "GamesDev"
    Case "SimGam"
        Me.ComboBoxCat3Name.RowSource = "SimGam"
    Case "RGS"
        Me.ComboBoxCat3Name.RowSource = "RGS"
    Case "RMG"
        Me.ComboBoxCat3Name.RowSource = "RMG"
    Case "IT"
        Me.ComboBoxCat3Name.RowSource = "IT"
    Case "Head Office"
        Me.ComboBoxCat3Name.RowSource = "HeadOffice"
    Case "System Sales"
        Me.ComboBoxCat3Name.RowSource = "SystemSales"

End Select

End Sub


Private Sub CommandButton3_Click()

Worksheets("CurrentMonth").Range(Cells(Currentrow, 31)) = Me.ComboBoxCat2Name
Worksheets("CurrentMonth").Range(Cells(Currentrow, 33)) = Me.ComboBoxCat3Name

End Sub

Private Sub UserForm_Initialize()

Worksheets("CurrentMonth").Select
Currentrow = 2
TextBox1.Text = Cells(Currentrow, 31).Text
TextBox2.Text = Cells(Currentrow, 30).Text
TextBox3.Text = Cells(Currentrow, 33).Text
TextBox4.Text = Cells(Currentrow, 32).Text
TextBox5.Text = Cells(Currentrow, 9).Text
TextBox6.Text = Cells(Currentrow, 67).Text
TextBox11.Text = Cells(Currentrow, 28).Text
TextBox12.Text = Cells(Currentrow, 68).Text
TextBox13.Text = Cells(Currentrow, 29).Text

Me.ComboBoxCat2Name = ""
Me.ComboBoxCat3Name = ""

End Sub

Private Sub CommandNext_Click()

Lastrow = Worksheets("CurrentMonth").Range("A" & Rows.Count).End(xlUp).Row
Currentrow = Currentrow + 1
If Currentrow = Lastrow + 1 Then
MsgBox ("You have reached the last record in this set of data")
Currentrow = Lastrow
End If
TextBox1.Text = Cells(Currentrow, 31).Text
TextBox2.Text = Cells(Currentrow, 30).Text
TextBox3.Text = Cells(Currentrow, 33).Text
TextBox4.Text = Cells(Currentrow, 32).Text
TextBox5.Text = Cells(Currentrow, 9).Text
TextBox6.Text = Cells(Currentrow, 67).Text
TextBox11.Text = Cells(Currentrow, 28).Text
TextBox12.Text = Cells(Currentrow, 68).Text
TextBox13.Text = Cells(Currentrow, 29).Text

End Sub

Private Sub CommandPrevious_Click()

Currentrow = Currentrow - 1
If Currentrow > 1 Then
TextBox1.Text = Cells(Currentrow, 31).Text
TextBox2.Text = Cells(Currentrow, 30).Text
TextBox3.Text = Cells(Currentrow, 33).Text
TextBox4.Text = Cells(Currentrow, 32).Text
TextBox5.Text = Cells(Currentrow, 9).Text
TextBox6.Text = Cells(Currentrow, 67).Text
TextBox11.Text = Cells(Currentrow, 28).Text
TextBox12.Text = Cells(Currentrow, 68).Text
TextBox13.Text = Cells(Currentrow, 29).Text
ElseIf Currentrow = 1 Then
MsgBox ("This is the first row")
Currentrow = Currentrow + 1
End If

End Sub

【问题讨论】:

  • 您可能应该进行一些验证以确保用户确实选择了一个值...
  • 谢谢 - 我的下一份工作:-)

标签: vba excel userform


【解决方案1】:

尝试将这两行替换为:

Worksheets("CurrentMonth").Cells(Currentrow, 31).Value = Me.ComboBoxCat2Name.Value
Worksheets("CurrentMonth").Cells(Currentrow, 33).Value = Me.ComboBoxCat3Name.Value

【讨论】:

  • 非常感谢 - 你永远不会知道我尝试了多少事情。当你知道怎么做时很简单:-)
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 2012-06-29
  • 1970-01-01
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多