为什么我的脚本会在此True / False上停下来以匹配两个值?

时间:2019-05-17 19:11:02

标签: excel vba

我构建了一个应用程序,其中会弹出一个对话框,询问工作表2中两列中的两个值。如果它们都匹配,则会弹出一个msg框,然后弹出其他实例。当我输入工作代码时,它与该工作代码匹配,但是该代码可能具有多个成本中心。我的代码在发生匹配的第一行停止,但没有遍历所有行以查看是否可以找到成本中心。它返回一条“已找到但不符合此成本中心的条件”消息,但是它并没有完成循环以真正能够分辨。

我需要在此处进行哪些更改以确保它不会过早触发该味精?

这是说rFound正在找到工作代码,但是对于每个合格的成本中心而言,该工作代码有4行,跨越第211-14行。它在第211行停止,然后不尝试浏览与rFound匹配的其他3行。 代码:

Option Explicit

Sub tgr()

Dim rFound As Range
Dim lJobCode As String
Dim lFLSA As String
Dim lCC As String
Dim sFirst As String
Dim sResults As String
Dim sh As Worksheet
Dim matched As Boolean
Dim Y As Integer, Z As Integer

lJobCode = Application.InputBox("Please provide a job code", "Job Code", Type:=2)
lCC = Application.InputBox("Please enter in a cost-center", "CC", Type:=2)

If lJobCode = "False" Or lCC = "False" Then Exit Sub 'Pressed cancel

Set sh = Sheets("Sheet1")

With ThisWorkbook.Worksheets("Sheet2").Columns("A")

    Set rFound = .Find(lJobCode, .Cells(.Cells.Count), xlValues, xlWhole)

    If Not rFound Is Nothing Then

        If ThisWorkbook.Worksheets("Sheet2").Cells(rFound.Row, 3).Value = lCC Then

            matched = True

            If rFound.Offset(, 4).Value = "Exempt" And Y = 0 Then
                MsgBox "Exempt roles may be eligible for schedule pay allowance."
                Y = 1

            Exit Sub 'if criteria is met, display above msgbox and then exit sub after user clicks ok or cancel

            End If

            If rFound.Offset(, 5).Value = "Eligible - Employee Level" And Z = 0 Then
                MsgBox "This job is only eligible at the employee level."
                Z = 1

            Exit Sub

            End If

            MsgBox "Job Code (" & lJobCode & ") is eligible for this cost-center."

        End If

        If Not matched Then MsgBox "Job Code (" & lJobCode & ") found, but not eligible for this cost-center."

    Else
        MsgBox "Job Code (" & lJobCode & ") not eligible."
    End If

End With

End Sub

1 个答案:

答案 0 :(得分:1)

您的代码中有很多未使用的变量(也许是从您实际尝试做的事情简化吗?),所以我删除了所有多余的东西。根据您的评论和提供的原始代码,我相信您实际上是在这样做:

public class MyViewModel: INotifyPropertyChanged
{

    private readonly TaskScheduler _uiScheduler;

    public MyViewModel()
    {
        _uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    }

    public Item SelectedItem
    {
        get { return m_selectedItem; }
        set
        {
            m_selectedItem = value;
            InitializeAsyncAndFirePropertyChanged()
                .ContinueWith(t =>
                {
                    if (t.Exception != null)
                    {
                        throw t.Exception;
                    }

                    OnPropertyChanged();
                }, _uiScheduler);
        }
    }

    public async Task InitializeAsyncAndFirePropertyChanged(ObservableCollection<RFEnvironment> possibleRfEnvironments)
    {
        //should check this method for exceptions and propagate them to the UI via databinding
        OtherDataBoundProperty = await GetSomeStringFromWebAsync();
    }

    public string OtherDataBoundProperty
    {
        get { return m_otherDataBoundProperty; }
        set
        {
            m_otherDataBoundProperty = value;
            OnPropertyChanged();
        }
    }

    .... other code to support INotifyPropertyChanged
}