使用组合框选择更新数据集时出现 1004 错误

1004 error using combox selections to update a dataset

我有一个用户窗体,用户可以使用命令按钮滚动浏览数据,其中一些显示在文本框中。 我有 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

尝试将这两行替换为:

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