为什么findnext方法在我的excel VBA代码中不起作用?

时间:2019-02-04 15:24:59

标签: excel vba

Findnext方法在过程“ Sub Loadschedule()”中不起作用 首先查找方法找到了我想要的范围,然后findnext方法未找到“设置教师查找=教师姓名.FindNext(teacherfind)”,尽管“教师姓名”范围仍然相同

但是,如果我删除了“ Sub Loadschedule()”中包含的过程“ color_available_Sch”,它将可以正常工作。

Sub Loadschedule()
Dim WS_sch As Worksheet 'Sheet schedule display
Dim WS_T As Worksheet 'Sheet Teacher list
Dim WS_S As Worksheet 'Sheet Student list
Dim Av_Day(1 To 7) As Integer 'Available day, mon to sun
Dim Av_time_hrs_STRT As Integer
Dim Av_time_hrs_END As Integer
Dim Av_time_min_STRT As Integer
Dim Av_time_min_END As Integer
Dim teacher_select As String 'teacher selected in schedule display
Dim teachername As Range 'column teacher name in the teacher list
Dim teacherfind As Range 'cell corresponding to the selected teacher name
Dim i As Integer 'for loop
Dim firstaddress
Dim checkaddress
Set WS_sch = Sheets("Schedule")
Set WS_T = Sheets("Teacher")
Set WS_S = Sheets("Student")

teacher_select = WS_sch.Range("Teacher_Selected")
Set teachername = WS_T.Range("B:B")
Set teacherfind = teachername.Find(teacher_select, LookIn:=xlValues)

If Not teacherfind Is Nothing Then
firstaddress = teacherfind.address
Do


Av_time_hrs_STRT = teacherfind.Offset(0, 2)
Av_time_min_STRT = teacherfind.Offset(0, 3)
Av_time_hrs_END = teacherfind.Offset(0, 4)
Av_time_min_END = teacherfind.Offset(0, 5)
For i = 1 To 7
    If teacherfind.Offset(0, i + 5) = "" Then
    Av_Day(i) = 0
    Else
    Av_Day(i) = 1
    End If
Next i

color_available_Sch Av_Day, Av_time_hrs_STRT, Av_time_hrs_END, Av_time_min_STRT, Av_time_min_END, WS_sch

checkaddress = teachername.address


Set teacherfind = teachername.FindNext(teacherfind)
checkaddress = teacherfind.address
Loop While Not teacherfind Is Nothing And teacherfind.address <> firstaddress
End If


End Sub
Sub color_available_Sch(ByRef Av_Day() As Integer, ByVal Av_time_hrs_STRT As Integer, _
ByVal Av_time_hrs_END As Integer, ByVal Av_time_min_STRT As Integer, ByVal Av_time_min_END As Integer, ByVal WS_sch As Worksheet)
Dim day_sch As Range
Dim day_find As Range
Dim i As Integer
Dim firstaddress
Dim rng_color_STRT As Range
Dim rng_color_END As Range

Set day_sch = WS_sch.Range("S:S")
For i = 1 To 7
If Av_Day(i) = 1 Then
Set day_find = day_sch.Find((i + 1 Mod 7), LookIn:=xlValues)
    If Not day_find Is Nothing Then
    firstaddress = day_find.address
    Do
        If Av_time_hrs_STRT <= 15 Then
        Set rng_color_STRT = day_find.Offset(0, 2 * (Av_time_hrs_STRT + Av_time_min_STRT / 60) - 32)
        Set rng_color_END = rng_color_STRT.Offset( _
        0, 2 * (Av_time_hrs_END + Av_time_min_END / 60 - Av_time_hrs_STRT + Av_time_min_STRT / 60))

        ElseIf Av_time_hrs_STRT > 15 Then
        Set rng_color_STRT = day_find.Offset(1, 2 * (Av_time_hrs_STRT + Av_time_min_STRT / 60) - 32)
        Set rng_color_END = rng_color_STRT.Offset( _
        1, 2 * (Av_time_hrs_END + Av_time_min_END / 60 - Av_time_hrs_STRT + Av_time_min_STRT / 60))
        End If


    With Range(rng_color_STRT, rng_color_END).Interior


        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
        .PatternTintAndShade = 0

    End With
    Set day_find = day_sch.FindNext(day_find)
    checkaddress = day_find.address
    Loop While Not day_find Is Nothing And day_find.address <> firstaddress
    End If
End If
Next i

End Sub

在“ teacherName”范围内,有一些相同的名称。我使用了findnext方法,以便可以对所有相同的名称执行相同的操作(显示子过程color_available_Sch)。

但是,它在Sub loadchedule过程及其子过程中不起作用。

0 个答案:

没有答案