如何在工作簿中正确激活工作表

时间:2015-05-07 23:15:27

标签: excel vba excel-vba excel-2010 worksheet

我正在尝试获取工作簿中给定工作表的最后一列和最后一行。下面是我用来获取这些值的代码。只要我点击工作表然后运行 Sub LastRowCol(),此代码就可以完美运行(因为我得到了正确的col和行数)。

Function LastUsedRow_Find() As Integer
    Dim rng As Range
    Set rng = ActiveSheet.Cells
    LastUsedRow_Find = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
End Function

Function LastUsedColumn_Find() As Integer
    Dim rng As Range
    Set rng = ActiveSheet.Cells
    LastUsedColumn_Find = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
End Function

Sub LastRowCol()
  MsgBox "last row -> " & LastUsedRow_Find()
  MsgBox "last col -> " & LastUsedColumn_Find()
End Sub

但是,我要说我有多张纸。我试图将工作表的索引传递给如下所示的方法。然后我尝试激活给定索引的工作表,因为我稍后使用 ActiveSheet 几行。代码运行没有例外但是我没有在msg框中获得正确数量的列和行。

Function LastUsedRow_Find(wksIndex As Integer) As Integer
    ActiveWorkbook.Sheets(wksIndex).Activate
    Dim rng As Range
    Set rng = ActiveSheet.Cells
    LastUsedRow_Find = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
End Function

Function LastUsedColumn_Find(wksIndex As Integer) As Integer
    ActiveWorkbook.Sheets(wksIndex).Activate
    Dim rng As Range
    Set rng = ActiveSheet.Cells
    LastUsedColumn_Find = rng.Find(What:="*", After:=rng.Cells(1), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
End Function

Sub LastRowCol()
    MsgBox "last row -> " & LastUsedRow_Find(1)
    MsgBox "last col -> " & LastUsedColumn_Find(1)
End Sub

有人可以让我知道我可能做错了什么或者我可能需要改变什么?感谢

2 个答案:

答案 0 :(得分:2)

为什么不通过Worksheet Object而不是传递索引。

Function LastUsedRow_Find(sh As Worksheet) As Variant
    Dim rng As Range
    Set rng = sh.Cells.Find(What:="*", _
                            After:=sh.Cells(1), _
                            Lookat:=xlPart, _
                            LookIn:=xlFormulas, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlPrevious, _
                            MatchCase:=False)
    If Not rng Is Nothing Then LastUsedRow_Find = rng.Row
End Function

与您的其他功能相同。
另外,我稍微修改了它以处理空白纸张 因此,如果函数可用,则上面的函数将返回最后一个行号,如果不可用,则返回空值。

然后像这样调用你的函数:

Sub LastRowCol()
    MsgBox "last row -> " & LastUsedRow_Find(Sheets("Sheet1"))
    MsgBox "last col -> " & LastUsedColumn_Find(Sheets("Sheet1"))
End Sub

答案 1 :(得分:1)

您的代码实际上工作正常,但这是一个修改后的版本,它将Function LastUsedRow_Find(wksIndex As Integer) As Integer Dim ws As Worksheet Set ws = ActiveWorkbook.Sheets(wksIndex) Dim rgFound As Range Set rgFound = ws.Cells.Find(What:="*", After:=ws.Cells(1), _ Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, MatchCase:=False) 'return first row if worksheet is blank If rgFound Is Nothing Then LastUsedRow_Find = 1 Else LastUsedRow_Find = rgFound.Row Debug.Print "Last row address: " & rgFound.Address End If End Function Function LastUsedColumn_Find(wksIndex As Integer) As Integer Dim ws As Worksheet Set ws = ActiveWorkbook.Sheets(wksIndex) Dim rgFound As Range Set rgFound = ws.Cells.Find(What:="*", After:=ws.Cells(1), _ Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, MatchCase:=False) 'return first column if worksheet is blank If rgFound Is Nothing Then LastUsedColumn_Find = 1 Else LastUsedColumn_Find = rgFound.Column Debug.Print "Last column address: " & rgFound.Address End If End Function Sub LastRowCol() MsgBox "last row -> " & LastUsedRow_Find(1) MsgBox "last col -> " & LastUsedColumn_Find(1) End Sub 最后一行/列的找到单元格的地址。我也没有激活这张纸,因为这是多余的。我也不会将整个工作簿单元格区域分配给变量,只分配找到的结果。

{{1}}