【问题标题】:How to add data inputed through combobox and add to to excel file如何添加通过组合框输入的数据并添加到excel文件
【发布时间】:2016-04-07 01:28:53
【问题描述】:

大家好,我是 VBA Excel 的新手,请多多包涵。这是我到目前为止的代码。这是一个具有三个输入的组合框 1.Part# 2. Month (Icol) 3. Amount。我希望我的组合框查看 A 列并查看该部分是否位于如果它不是我想将该部分添加到行的底部。一切正常,除非在 A 列上没有添加新部分。该部分被添加到行的底部,但值部分没有,这必须与用户选择的月份相对应。新部分,“当前月份 +1=N, 200。所以添加新部分并将 200 添加到 N 列。

私有子 cmdAdd_Click()

Dim iRow As Long
Dim lastRow As Long
Dim iCol As String
Dim c As Range
Dim ws As Worksheet
Dim value As Long
Dim NewPart As Boolean
Set ws = Worksheets("sheet2")

Set c = ws.Range("A7:A1048576").Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole)
If c Is Nothing Then
'find first empty row in database
    lastRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
    iRow = lastRow + 1
    NewPart = True
Else
'find row where the part is
    iRow = ws.Cells.Find(What:=Me.PartTextBox.value, SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row
    NewPart = False
End If
'check for a part number
If Trim(Me.PartTextBox.value) = "" Then
  Me.PartTextBox.SetFocus
  MsgBox "Please Enter A Part Number"
  Exit Sub
End If

If Trim(Me.MonthComboBox.value) = "" Then
  Me.MonthComboBox.SetFocus
  MsgBox "Please Enter A Month"
  Exit Sub
End If

If Trim(Me.AddTextBox.value) = "" Then
  Me.AddTextBox.SetFocus
  MsgBox "Please Enter A Value To Add Or Substract"
  Exit Sub
End If

'copy the data to the database
'use protect and unprotect lines,
'     with your password
'     if worksheet is protected

Select Case MonthComboBox.value

    Case "Current Month"

        iCol = "C"

    Case "Current Month +1"

        iCol = "N"

    Case "Current Month +2"

        iCol = "O"

    Case "Current Month +3"

        iCol = "P"

    Case "Current Month +4"

        iCol = "Q"



End Select
value = Cells(iRow, iCol).value

If NewPart = True Then
    ws.Cells(iRow, "A").value = Me.PartTextBox.value
    ws.Cells(iRow, iCol).value = value + CLng(Me.AddTextBox.value)


End If



With ws
'  .Unprotect Password:="password"

  .Cells(iRow, iCol).value = value + CLng(Me.AddTextBox.value)

'  .Protect Password:="password"
End With




'clear the data
Me.PartTextBox.value = ""
Me.MonthComboBox.value = ""
Me.AddTextBox.value = ""
Me.PartTextBox.SetFocus

End Sub

Private Sub cmdClose_Click()
Unload Me
End Sub


Private Sub UserForm_Initialize()

'Empty NameTextBox
 PartTextBox.value = ""

'Empty PhoneTextBox
 AddTextBox.value = ""

'Empty DinnerComboBox

'Fill DinnerComboBox
With MonthComboBox
     .AddItem "Current Month"
     .AddItem "Current Month +1"
     .AddItem "Current Month +2"
     .AddItem "Current Month +3"
     .AddItem "Current Month +4"

End With

End Sub

【问题讨论】:

    标签: vba excel combobox


    【解决方案1】:

    对于 line value = Cells(iRow, iCol).value,您需要有整数作为参考而不是字母。

    所以你需要在你的选择语句中使用数字:

    Select Case MonthComboBox.value
    
    Case "Current Month"
    
        iCol = 3
    
    Case "Current Month +1"
    
        iCol = 14
    
    Case "Current Month +2"
    
        iCol = 15
    
    Case "Current Month +3"
    
        iCol = 16
    
    Case "Current Month +4"
    
        iCol = 17
    
    
    End Select
    

    【讨论】:

    • 抱歉,不确定是否是问题所在。如果通过将 iCol 切换为数字,我的语句中出现错误,而且我认为不是这种情况,因为当在 A 列下找到该部件时,Select Case 确实有效。将新部件添加到行底部时,选择案例不起作用。
    • @INOPIAE 字符串值被Cells 对象接受。只需在即时窗口中输入?cells(1,"C").address
    • Select Case MonthComboBox.value Case "Current Month" iCol = Cells(3, "C").Address Case "Current Month +1" iCol = Cells(14, "N").Address Case "当月 +2" iCol = Cells(15, "O").Address case "Current Month +3" iCol = Cells(16, "P").Address case "Current Month +4" iCol = Cells(17, "Q").地址
    • 这就是我现在切换的方式,我仍然得到一个调试错误
    猜你喜欢
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2019-10-23
    • 2018-10-28
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多