回到第一排

时间:2016-01-11 03:13:19

标签: excel vba excel-vba

我目前在Excel中的列是:

A栏:代理人的姓名
B栏:可用性
C列:指针

代码基本上会检查B列是否可用而C列是否有指针。如果两者都为True,那么如果代理可用,它将检查下一行。如果没有,则移至下一行。如果可用,则将指针移动到该行的C列。

我目前在找到代码到达最后一行后如何返回数据的第一行时遇到问题。基本上整个事情就像一个循环,但我不知道如何以这种方式编码。以下是我的工作代码。

Sub Pointer()
    Dim lgLastRow As Long 'specify the last data row
    lgLastRow = Range("A1048576").End(xlUp).Row     
    Dim lgLastPnt As Long 
    Dim lgCurrentRow As Long 'specify the currently examined row
    Dim nxtRow As Long
    Dim nxtAvl As Long
    Dim agAly As String
    Dim status As String
    Dim pnt As String

    For lgCurrentRow = 2 To lgLastRow
        agAly = Cells(lgCurrentRow, "A")
        status = Cells(lgCurrentRow, "B")
        pnt = Cells(lgCurrentRow, "C")

        If status = "Available" And pnt = "*" Then
            Debug.Print agAly
            Cells(lgCurrentRow, "C").Select
            Selection.Cut

            nxtRow = lgCurrentRow + 1

                For nxtAvl = nxtRow To lgLastRow
                    If Cells(nxtAvl, "B") = "Available" Then
                    Cells(nxtAvl, "C").Select
                    ActiveSheet.Paste
                    nxtAvl = nxtAvl + 1
                    End If
                Next
        End If

    Next

End Sub

1 个答案:

答案 0 :(得分:0)

        Sub Pointer()
            Dim lgLastRow As Long 'specify the last data row
            lgLastRow = Range("A1048576").End(xlUp).Row
            Dim lgLastPnt As Long
            Dim lgCurrentRow As Long 'specify the currently examined row
            Dim nxtRow As Long
            Dim nxtAvl As Long
            Dim agAly As String
            Dim status As String
            Dim pnt As String

            For lgCurrentRow = 2 To lgLastRow
                agAly = Cells(lgCurrentRow, "A")
                status = Cells(lgCurrentRow, "B")
                pnt = Cells(lgCurrentRow, "C")

                If status = "Available" And pnt = "*" Then
                    Debug.Print agAly
                    Cells(lgCurrentRow, "C").Select
                    Selection.Cut

                    nxtRow = lgCurrentRow + 1

                        For nxtAvl = nxtRow To lgLastRow
                            If Cells(nxtAvl, "B") = "Available" Then
                            Cells(nxtAvl, "C").Select
                            ActiveSheet.Paste
                            nxtAvl = nxtAvl + 1
                            End If
                        Next
                End If

            Next
          Range("A1").Select
        End Sub