VBA功能问题

时间:2010-12-16 08:26:59

标签: excel-vba vba excel

我正在开展一个项目,我需要根据15分钟的时间段,他们的语言技能和轮班来分析员工的可用性。我有一张看起来像这样的表:

--Time-------English------German-----French

--06:15--------0------------0-----------0
--06:30--------0------------0-----------0
--06:45--------0------------0-----------0

另一个看起来像这样:

--Name-----Language-----Info--------Shift--------

--Joe B----English-----BlaBla---05:45 - 12:30---
--Al C-----English-----BlaBla---07:45 - 15:30---
--Jill T---English-----BlaBla---07:45 - 15:30---

所以,我需要采用他们所说的语言,他们的转换(分为开始和结束时间),并根据该列标题中指定的语言(在上面给出的第一张表中)添加'1'如果他们说那种语言并且当时可用。

以下是我目前的代码,但我没有这样做:

Function CalculateAvailability(The_Time As String, The_Info As Range, Current_Lang As String)

'The_Time is the current 15-minute period
'The_Info is the range of fields in sheet 2 with our information (A3:D5 above)
'Current_Lang is the current language heading

Dim The_Lang As String 'the language of the staff member
Dim The_Shift_Start As String 'obvious
Dim The_Shift_End As String 'also obvious
Dim stGotIt As String 'redundant, not used currently
Dim stCell As String 'for capturing the cell contents
Dim Counter As Integer 'our counter

Counter = 0

For Each r In The_Info.Rows
    For Each c In r.Cells
        stCell = c.Value
        If InStr(1, stCell, ":", vbTextCompare) > 0 Then
            The_Shift_Start = GetShiftStart(stCell) 'this code works
            The_Shift_End = GetShiftEnd(stCell) 'this code works
        End If
        If InStr(1, stCell, Current_Lang, vbTextCompare) > 0 Then
            The_Lang = Current_Lang 'seems redundant but how else to do the check?
            Counter = 17 'test value - doesn't seem to reach here
        End If
    Next c
    If The_Lang = Current_Lang Then
        Counter = 5 'test value - doesn't seem to reach here either
    End If
Next r

CalculateAvailability = "Time:" + The_Time + " - Start:" + The_Shift_Start + " - End:" + The_Shift_End + " - Avail. " + Counter 'for output purposes

End Function

它似乎没有到达我的代码的重要部分,我将进行计算调用单独的(测试和工作)函数,该函数根据时间段和班次返回“1”或“0”开始和结束时间。 (那就是我指定Counter = 5的地方)我上面代码中的逻辑是不正确的?我意识到语言的测试似乎是多余的,可能是不正确的,但正如评论所说,我无法更明智地重做。我很好奇的一件事是,For循环立即从“Next”跳回到相关的“For Each”或者是否执行以下代码然后跳回来? (我知道这在编程上没有多大意义,但我对VB并不熟悉所以我不能肯定地说,尽管根据我的C / C#/ C ++知识看起来有多荒谬)

非常感谢所有人的帮助!

1 个答案:

答案 0 :(得分:1)

假设Current_Lang未作为空字符串发送且代码永远不会到达行Counter = 17,它也永远不会到达行Counter = 5,所以我认为你可以集中注意力为什么它没有达到Counter = 17

最可能的答案似乎是stCell不包含您希望它包含的内容,因此这就是为什么它在那里找不到Current_Lang的原因。尝试输出或Watch stCell中的内容,以便检查其无效的原因。