Excel用户表单组合框一旦提交输出到excel

时间:2015-02-16 10:51:53

标签: excel vba excel-vba combobox userform

我对整个VBA都比较陌生,所以任何帮助都会非常感谢... 我在我的表单上获取我的Combobox(一旦点击提交按钮)将问题输入到特定列中的excel电子表格时会出现问题,每次输入新记录时该列都会向下移动。

请参阅下面的代码,如果您需要任何进一步的信息,请随便提出:)非常感谢提前Paula

Option Explicit
Private Sub cmdAdd_Click()
    Dim irow As Long
    Dim EorP As String
    Dim ComboStaus As ComboBox

    Dim ws As Worksheet
    Set ws = Worksheets("BS Personal Data")

    'find first empty row in spreadsheet
    irow = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
                         SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    EorP = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
                         SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    Combo = ws.Cells.Find(What:="*", SearchOrder:=xlRows, _
                          SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    'copy the data to the spreadsheet

    With ws
        .Cells(irow, 1).Value = Me.txtmanager.Value
        .Cells(irow, 2).Value = Me.txtdivision.Value
        .Cells(irow, 3).Value = Me.txtlocation.Value
        .Cells(irow, 4).Value = Me.txtsystemname.Value
        .Cells(irow, 9).Value = Me.Txtpurpose.Value
        .Cells(irow, 10).Value = Me.txtaccess.Value
        .Cells(irow, 11).Value = Me.txtdatecompleted.Value
    End With

    'clear the data
    Me.txtmanager.Value = ""
    Me.txtdivision.Value = ""
    Me.txtlocation.Value = ""
    Me.txtsystemname.Value = ""
    Me.Txtpurpose.Value = ""
    Me.txtaccess.Value = ""
    Me.txtdatecompleted.Value = ""

    'set option button to input data based on type of information to column 5
    With ws
        If OptElectronic Then
            .Cells(EorP, 5).Value = "Electronic"
        Else
            .Cells(EorP, 5).Value = "PaperBased"
        End If
    End With

    'set option button to input data based on personal data into column 7
    With ws
        If PersonalYes Then
            .Cells(EorP, 7).Value = "Yes"
        Else
            .Cells(EorP, 7).Value = "No"
        End If
    End With

    'set option button to input data based on privacy notes into column 8
    With ws
        If PrivacyYes Then
            .Cells(EorP, 8).Value = "Yes"
        Else
            .Cells(EorP, 8).Value = "No"
        End If
    End With

End Sub

'combo button setup
Private Sub UserForm_Activate()
    ComboStatus.Clear
    With ComboStatus  'this loads the combo
        .AddItem ""
        .AddItem "Live"
        .AddItem "Archived"
        .AddItem "zzz"
    End With

End Sub

'close button on the form
Private Sub cmdClose_Click()
    Unload Me
End Sub

0 个答案:

没有答案