代码之家  ›  专栏  ›  技术社区  ›  Al Grant

计算单元格列中的行数

  •  0
  • Al Grant  · 技术社区  · 7 年前

    我正在尝试循环查看每个员工的名册,并自动将他们安排到一个免费会议中。其中一部分是计算当前单元格(会话)列中的行数

    名册 enter image description here

    这个 会议 数据看起来像

    enter image description here

    Sub ScheduleSession()
    Dim Roster As Worksheet
    Dim Sessions As Worksheet
    Dim LastRow As Long
    Dim x As Long
    Dim row As Range
    Dim Session As Range
    
    With ActiveWorkbook.Worksheets("Roster")
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).row
    For x = LastRow To 8 Step -1
    
        If IsEmpty((.Range("B" & x))) Then
            ' Look for a session
            Debug.Print ("Looking For a Session for " & (.Range("A" & x)))
                    For Each Session In ActiveWorkbook.Worksheets("Sessions").Range("A1:E1").Cells
                        Debug.Print (Session)
                        ' Check session is not full (3 per session) else next session
                        SessionCount = Cells(Rows.Count, Session.Columns.Count).End(xlUp).row
                        If (SessionCount > 4) Then
                            Exit For
                        Else
                            ' Loop over cells in employee roster to see if they are working on given day and time.
                            ' If so roster them by recording session against employee (on ROSTER col B) and employee against session in appropriate col on SESSIONS
                        End If
                    Next
    
        Else
            ' Employee already scheduled
            Debug.Print ("This employee " & (.Range("A" & x)) & " already has a session")
        End If
    
    Next x
    End With
    

    我可以获得有关计算For Each Session循环行中的行的帮助吗?当前是运行时错误91-未设置块变量的对象变量。

    这个问题已经被回答了很多次,但我看到的其他答案并没有说明如何将它应用到类似于范围的会话中。

    如果有人觉得自己很慷慨,那么最好在每次会议上都能到员工名册上查看他们是否在工作,是否可以安排时间。

    使现代化 抱歉,会话是会话

    SessionCount = Cells(Rows.Count, Session.Columns.Count).End(xlUp).row
    

    2 回复  |  直到 7 年前
        1
  •  1
  •   Alex de Jong    7 年前

    我想你还没有设置变量 Sessions 然而之后的某个地方 Dim Sessions As Worksheet ,您需要添加 Set Sessions = ActiveWorkbook.Sheets("Sessions")

        2
  •  1
  •   Tim Williams    7 年前
    SessionCount = Cells(Rows.Count, Session.Columns.Count).End(xlUp).row
    

    在这里 Session 是一个单元格,所以你总是看第一列。。。

    SessionCount = Cells(Rows.Count, Session.Column).End(xlUp).row
    

    推荐文章