活动工作簿在没有提示的情况下恢复到“thisworkbook”

时间:2018-02-14 21:25:35

标签: excel vba excel-vba

Workbooks("The One Sheet.xlsx").Activate
MsgBox ActiveWorkbook.Name  'Returns "The One Sheet.xlsx"
Worksheets("One Sheet").Activate

MsgBox ActiveWorkbook.Name 'Returns "The One Sheet.xlsx"
RwCnt = Application.WorksheetFunction.CountA(Range("A:A"))
MsgBox ActiveWorkbook.Name 'Returns the sheet that contains the code

有谁知道什么可以调用另一个工作簿?

1 个答案:

答案 0 :(得分:0)

不确定是什么调用其他WB聚焦。但最佳做法是明确定义对工作簿的引用

Sub SheetCode()
    Dim wbOne As Workbook
    Dim RwCnt As Long

    Set wbOne = Workbooks("The One Sheet")
    wbOne.Activate
    RwCnt = wbOne.ActiveSheet.Range("A" & wbOne.ActiveSheet.Rows.Count).End(xlUp).Row

    MsgBox "The last row is " & RwCnt
End Sub

希望有帮助,Caleeco