如何根据条件语句使用特定变量?

时间:2016-08-04 21:24:41

标签: vba excel-vba excel-2010 excel

我有一个显示UserForm的宏,它会根据用户决定要生成的报告创建对帐报告。 (即,如果用户选择信用卡的对帐,则会生成信用卡报告。如果他们选择检查& 信用卡,将为信用卡支票创建报告 - 在同一主报告摘要中)

在初始表单(或取消选择)中选择任一报告的CheckBoxes后,将切换该帐户的Boolean变量。用户继续选择报告后,将为每个选择生成报告。 (第一张表是摘要,每个连续的表包含源文件和每个报表的平衡计算 - 上限是摘要表)

请参阅第一张图片。

...

报告完成后,如果正在调和的两个帐户(银行对帐单与我们的会计记录)之间存在差异,则会显示另一个显示差异的UserForm,以两个单独的形式显示ListBoxes;一个ListBox包含记录中的额外值,另一个ListBox包含银行对帐单中的额外值。

请参阅第二个&第三张图片。

...

报告后UserForm每个Buttons有2 ListBox个;一个Button允许用户从列表中删除所选的[ListBox]项目,如果它与另一个ListBox中的项目/多个项目进行平衡。如果不匹配,则必须在我们的记录中反映出来,第二个Button允许他们在日常对帐报告中反映这样做。

对于宏,我为每个可能的报告创建了格式为“BWVariable”,“ChVariable”,“eChVariable”或“CCVariable”的变量,以仅影响该报告的项目。但是,通过我的测试,我只使用了BWVariables,因为它最容易测试。

现在已经完成了,如果BWReport为true,我希望Button_Click()事件的结果使用BWVariables,如果CCReport为true,我希望它使用CCVariables。有没有办法根据条件语句的输出使用特定变量?

...

以下是受特别影响的代码:

Private Sub RemovedfromGPButton_Click()
    Dim BWItem As Double

    For lItem = BWListBox.ListCount - 1 To 0 Step -1
        If BWListBox.Selected(lItem) Then
            BWItem = BWListBox.List(lItem, 0)
            BWListBox.RemoveItem lItem
            If BWAddedGPSum2 Is Nothing Then
                Set BWAddedGPSum2 = Range(BWGPSum.Offset(1, -3), BWGPSum.Offset(1, 1))
                BWAddedGPSum2.Insert Shift:=xlDown
                Set BWAddedGPSum2 = Range(BWGPSum.Offset(1, -3), BWGPSum.Offset(1, 1))
                BWAddedGPSum2.Interior.ColorIndex = 0
                BWAddedGPSum2.Insert Shift:=xlDown
                Set BWAddedGPSum2 = Range(BWGPSum.Offset(1, -3), BWGPSum.Offset(1, 1))
                BWAddedGPSum2.Interior.ColorIndex = 0
                Set BWAddedGPTitle2 = Range(BWGPSum.Offset(1, -2), BWGPSum.Offset(1, -1))
                With BWAddedGPTitle2
                    .MergeCells = True
                    .HorizontalAlignment = xlRight
                    .VerticalAlignment = xlCenter
                End With
                    BWAddedGPTitle2.Value = "Added to Deposit:"
                Set BWAddedGPSum2 = BWGPSum.Offset(1, 0)
                    If BWAddedGPSum Is Nothing Then
                        BWAddedGPSum2.Borders(xlEdgeBottom).LineStyle = xlContinuous
                    End If
                If BWGPSum.Offset(-1, 0).Text = "" Then
                    Set BWGPSum = BWVariance.Offset(-2, 0)
                        Range(BWGPSum.Offset(0, -1), BWGPSum.Offset(0, -2)).MergeCells = True
                        BWGPSum.Offset(0, -1).HorizontalAlignment = xlRight
                        BWGPSum.Offset(0, -2).Value = "Total:"
                        BWGPSum.Interior.ColorIndex = 6
                End If
            End If
            BWAddedGPSum2.Value = Format((BWAddedGPSum2.Value + BWItem), "$#,##0.00")
            BWGPSum.Value = Format(Application.WorksheetFunction.Sum(BWGPSum.Offset(-1, 0).End(xlUp), _
                                   BWGPSum.Offset(-1, 0)), "$#,##0.00")
            Call DetermineVariance
            If Me.BWListBox.MultiSelect = fmMultiSelectSingle Then
                Exit For
            End If

        End If
    Next
End Sub

...

第一张图片:

This is the First Image:

...

第二张图片:

This is the Second Image:

...

第三张图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

无法查询/引用VBA中的变量名称部分

所以我建议你:

  • 使用User Defined Type(我们称之为"报告"),其字段名称与您的变量名称​​常量部分(ListBox)匹配, ItemAddedGPSumAddedGPSum2,...)

  • 声明类型"报告"的Public变量(比如我们称之为REP)在你的任何模块中

    它将在userforms

  • 之间使用/共享
  • 声明userform范围正确命名的变量BWCheChCC)类型"报告"在ReportGenerator userform

这样您的RemovedfromGPButton_Click子代码将进行少量修改:

  • 将其封装在With REPEnd With块内

  • BW替换为.

更详细一点:

主要

此处遵循您的主要必要摘录:

Option Explicit

Type Report '<-- define the User Defined Type 'Report'
    ListBox As MSForms.ListBox
    Item As String
    AddedGPSum As Range
    AddedGPSum2 As Range
    AddedGPTitle2 As Range
    GPSum As Range
    Variance As Range
    '
    ' and whatever else you may need
    '
End Type

Public REP As Report '<--| declare a Public variable of type 'Report'


Sub main() 'this is the sub that will exploit 'ReportGenerator' Userform

    '
    ' your code preceeding 'ReportGenerator' Userform exploitation
    '

    With ReportGenerator '<--| now you load the 'ReportGenerator' Userform...
        ' code to initialize/set userform controls (you may want to take it into its 'UserForm_Initialize' sub)

        .Show '<--| ... and show it

        '
        ' possible code to exploit 'ReportGenerator' Userform outcome
        '
    End With

    '
    ' your code after 'ReportGenerator' Userform exploitation
    '


End Sub

ReportGenerator Userform

此处遵循您的ReportGenerator用户表单代码必备摘录:

Option Explicit

Dim BW As Report, Ch As Report, eCh As Report, CC As Report '<--| declare Userform scoped variables. They'll be available throughout the whole 'ReportGenerator' userform subs and functions


Private Sub OKButton_Click() '<--| change "OKButton" with your actual 'ReportGenerator' userform button name with "OK" caption

    With Me '<--| refer to 'ReportGenerator' userform
        Select Case True '<--| check for the first checked checkbox...
            Case .CheckBox1
                REP = BW '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
            Case .CheckBox2
                REP = Ch '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
            Case .CheckBox3
                REP = eCh '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
            Case .CheckBox4
                REP = CC '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
        End Select
        .Hide '<--| hide the 'ReportGenerator' userform before showing "Unmatched_Summary" one

        Unmatched_Summary.Show ' <--| load and show "Unmatched_Summary" userform
        Unload Unmatched_Summary  ' <--| unload "Unmatched_Summary" userform

        .Show ' <--| show 'ReportGenerator' userform again
    End With


End Sub


Private Sub UserForm_Initialize() '<--| this runs at Userform loading time
    With Me '<--| refer to 'ReportGenerator' userform
        Set BW.ListBox = .ListBox1 '<--| set BW listbox to appropriate variable of type "Report"
        Set Ch.ListBox = .ListBox2 '<--| set Ch its listbox to appropriate variable of type "Report"
        Set eCh.ListBox = .ListBox3 '<--| set eCH listbox to appropriate variable of type "Report"
        Set CC.ListBox = .ListBox4 '<--| set CC listbox to appropriate variable of type "Report"
    End With
End Sub

无法比较的摘要用户表单

此处遵循您的UnmatchedSummary用户表单代码必需摘录

Option Explicit

Private Sub RemovedfromGPButton_Click()
    Dim lItem As Long

    With REP '<--| refer the Public variable of tipe "Report" that has been previuosly set to the proper one in 'ReportGenerator' just before 'Unmatched_Summary.Show' statement
        For lItem = .ListBox.ListCount - 1 To 0 Step -1
            If .ListBox.Selected(lItem) Then
                .Item = .ListBox.List(lItem, 0)
                .ListBox.RemoveItem lItem
                If .AddedGPSum2 Is Nothing Then
                    Set .AddedGPSum2 = Range(.GPSum.Offset(1, -3), .GPSum.Offset(1, 1))
                    .AddedGPSum2.Insert Shift:=xlDown
                    Set .AddedGPSum2 = Range(.GPSum.Offset(1, -3), .GPSum.Offset(1, 1))
                    .AddedGPSum2.Interior.ColorIndex = 0
                    .AddedGPSum2.Insert Shift:=xlDown
                    Set .AddedGPSum2 = Range(.GPSum.Offset(1, -3), .GPSum.Offset(1, 1))
                    .AddedGPSum2.Interior.ColorIndex = 0
                    Set .AddedGPTitle2 = Range(.GPSum.Offset(1, -2), .GPSum.Offset(1, -1))
                    With .AddedGPTitle2
                        .MergeCells = True
                        .HorizontalAlignment = xlRight
                        .VerticalAlignment = xlCenter
                    End With
                        .AddedGPTitle2.value = "Added to Deposit:"
                    Set .AddedGPSum2 = .GPSum.Offset(1, 0)
                        If .AddedGPSum Is Nothing Then
                            .AddedGPSum2.Borders(xlEdgeBottom).LineStyle = xlContinuous
                        End If
                    If .GPSum.Offset(-1, 0).Text = "" Then
                        Set .GPSum = .Variance.Offset(-2, 0)
                            Range(.GPSum.Offset(0, -1), .GPSum.Offset(0, -2)).MergeCells = True
                            .GPSum.Offset(0, -1).HorizontalAlignment = xlRight
                            .GPSum.Offset(0, -2).value = "Total:"
                            .GPSum.Interior.ColorIndex = 6
                    End If
                End If
                .AddedGPSum2.value = Format((.AddedGPSum2.value + .Item), "$#,##0.00")
                .GPSum.value = Format(Application.WorksheetFunction.Sum(.GPSum.Offset(-1, 0).End(xlUp), _
                                       .GPSum.Offset(-1, 0)), "$#,##0.00")
    '            Call DetermineVariance
                If .ListBox.MultiSelect = fmMultiSelectSingle Then Exit For

            End If
        Next lItem
    End With

End Sub

并且,正如您所看到的,与原始RemovedfromGPButton_Click()

相比,只需进行少量修改

编辑以检查所有复选框

检查并执行所有复选框,以下是如何更改ReportGenerator Userform代码的相关摘录:

With Me '<--| refer to 'ReportGenerator' userform
    .Hide '<--| hide the 'ReportGenerator' userform before showing "Unmatched_Summary" one
    If .CheckBox1 Then
        REP = BW '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
        Unmatched_Summary.Show ' <--| load and show "Unmatched_Summary" userform
    End If
    If .CheckBox2 Then
        REP = Ch '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
        Unmatched_Summary.Show ' <--| load and show "Unmatched_Summary" userform
    End If
    If .CheckBox3 Then
        REP = eCh '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
        Unmatched_Summary.Show ' <--| load and show "Unmatched_Summary" userform
    End If
    If .CheckBox4 Then
        REP = CC '<--|... and assign the proper variable of type "Report" to the public variable REP of type "Report"
        Unmatched_Summary.Show ' <--| load and show "Unmatched_Summary" userform
    End Select

    Unload Unmatched_Summary  ' <--| unload "Unmatched_Summary" userform

    .Show ' <--| show 'ReportGenerator' userform again
End With
相关问题