使用combox选择更新数据集时出现1004错误

时间:2015-01-27 11:15:41

标签: excel vba excel-vba userform

我有一个用户表单,用户可以使用命令按钮滚动数据,其中一些显示在文本框中。 我有一个2个组合框,一个依赖于另一个,它使用case语句。我想使用这些组合框选择来更新原始数据,但我一直收到错误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

1 个答案:

答案 0 :(得分:0)

尝试用这个替换这两行:

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