EXCEL VBA USERFORM - 如果选择了某些组合框,则使值为负值

时间:2016-10-25 13:18:04

标签: excel vba excel-vba combobox

我正在创建一个带有userform的简单电子表格,以允许管理员帮助将交易输入到预算中。我遇到问题的唯一方法是找出一种方法,如果名为:entrytype的Combobox中的值为“Revenue”,则自动生成一个值(自动)

任何帮助都会受到赞赏,甚至可以在其他地方看一下类似的例子......

Private Sub CommandButton1_Click()
Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Entry")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 1).value = Me.entrytype.value
        .Cells(lRow, 2).value = Me.expense1.value
        .Cells(lRow, 3).value = Me.expense2.value
        .Cells(lRow, 4).value = Me.eventcategory.value
        .Cells(lRow, 5).value = Me.state.value
        .Cells(lRow, 6).value = Me.dateVariable.value
        .Cells(lRow, 7).value = Me.invoice.value
        .Cells(lRow, 8).value = Me.description.value
        .Cells(lRow, 9).value = Me.memo.value
        .Cells(lRow, 11).value = Me.TextBox1.value
    End With
    'Clear input controls.
    Me.entrytype.value = ""
    Me.expense1.value = ""
    Me.expense2.value = ""
    Me.eventcategory.value = ""
    Me.state.value = ""
    Me.dateVariable.value = ""
    Me.invoice.value = ""
    Me.description.value = ""
    Me.memo.value = ""
    Me.TextBox1.value = ""
End Sub

Private Sub expense1_Change()
expense2.Clear
Dim index As Integer
index = expense1.ListIndex
Select Case index
    Case Is = 0
        With expense2
            .AddItem "N/A"
        End With
    Case Is = 1
        With expense2
            .AddItem "Campus Tour Program"
            .AddItem "Events Off-Campus"
            .AddItem "Events On-Campus"
            .AddItem "Alumni Association"
        End With
    Case Is = 2
        With expense2
            .AddItem "Advertising"
            .AddItem "Promotional Giveaways"
            .AddItem "Publications-Marketing"
        End With
    Case Is = 3
        With expense2
            .AddItem "Recruitment Travel"
            .AddItem "Search Names"
            .AddItem "Travel"
        End With
    Case Is = 4
        With expense2
            .AddItem "Oper. Maint. & Upgrade Agreements"
            .AddItem "Furnishings & Renovations"
            .AddItem "Office Supplies"
            .AddItem "Postage"
            .AddItem "Telephone & Cable"
            .AddItem "Equipment Purchase & Repair"
            .AddItem "Training & Prof Develop"
        End With
    Case Is = 5
        With expense2
            .AddItem "Miscellaneous"
        End With
If expense1 <> -1 Then
    expense2 = expense1
    expense2 = -1
    End If

End Select

End Sub

Private Sub Image1_Click()
dateVariable = CalendarForm.GetDate( _
        SelectedDate:=Date, _
        FirstDayOfWeek:=Monday, _
        DateFontSize:=12, _
        OkayButton:=True, _
        ShowWeekNumbers:=True, _
        BackgroundColor:=RGB(243, 249, 251), _
        HeaderColor:=RGB(147, 205, 2221), _
        HeaderFontColor:=RGB(255, 255, 255), _
        SubHeaderColor:=RGB(223, 240, 245), _
        SubHeaderFontColor:=RGB(31, 78, 120), _
        DateColor:=RGB(243, 249, 251), _
        DateFontColor:=RGB(31, 78, 120), _
        TrailingMonthFontColor:=RGB(155, 194, 230), _
        DateHoverColor:=RGB(223, 240, 245), _
        DateSelectedColor:=RGB(202, 223, 242), _
        SaturdayFontColor:=RGB(0, 176, 240), _
        SundayFontColor:=RGB(0, 176, 240), _
        TodayFontColor:=RGB(0, 176, 80))
    If dateVariable <> 0 Then dateVariable = dateVariable

End Sub

Dim TextBox1oldValue As String

Private Sub TextBox1_AfterUpdate()
    If IsNumeric(TextBox1) Then
        TextBox1 = Format(TextBox1, "$#,##0.00")
    Else
        TextBox1 = TextBox1oldValue
    End If
End Sub

Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If IsNumeric(TextBox1) Then
        TextBox1oldValue = Format(TextBox1, "$#,##0.00")
    End If
End Sub

Private Sub UserForm_Initialize()
    TextBox1oldValue = "$0.00"
    TextBox1 = "$0.00"

With entrytype
    .AddItem "Revenue"
    .AddItem "Expense"
End With

With expense1
    .AddItem "Application Fee Expense"
    .AddItem "Campus Tour Events"
    .AddItem "Marketing Communication"
    .AddItem "Recruitment Outreach"
    .AddItem "General Operating"
    .AddItem "Miscellaneous"
End With

With eventcategory
    .AddItem "Application Workshop"
    .AddItem "Up Close"
    .AddItem "Scholars Day"
    .AddItem "Senior Day"
    .AddItem "Junior Day"
    .AddItem "Financial Aid Night"
    .AddItem "Academic Talent Event"
    .AddItem "PC Conference"
    .AddItem "Orange Friday"
    .AddItem "Gear UP"
End With

With state
    .AddItem "Oklahoma"
    .AddItem "Arkansas"
    .AddItem "Arizona"
    .AddItem "California"
    .AddItem "Colorado"
    .AddItem "Florida"
    .AddItem "Illinois"
    .AddItem "Kansas"
    .AddItem "Missouri"
    .AddItem "Nebraska"
    .AddItem "New Mexico"
    .AddItem "Texas"
End With

End Sub

2 个答案:

答案 0 :(得分:1)

只需为ComboBox添加一个Change事件处理程序并进行简单的测试:

Private Sub entrytype_Change()
    If entrytype.Value = "Revenue" Then
        yourvaluehere = -yourvaluehere 
    End If
End Sub

答案 1 :(得分:1)

有多种方法可以进行输入验证,但是,这样的方法可能很有用:

If Me.ComboBox1.Value = "Revenue" And Left(TextBox1.Text, 1) <> "-" Then
 TextBox1.Text = "-" & TextBox1.Text

ComboBox1是包含“Revenue”下拉列表的对象,TextBox1是您输入的任何对象(如果选择了Revenue,您希望更改为减去的所有值字段)。

您也可以稍后再做一些事情,然而,这会在提交前在表格上明显显示。