VBA遍历文件夹内工作薄并指向指定工作表

我在遍历工作表时,当打开一个excel文件时,如果当前活动工作表不是sheet1,怎么样才激活sheet1。
Sub UnionWorksheets()
Dim i, m As Integer

Dim lj As String
Dim dirname As String
Dim nm As String
Dim Sht As Worksheet
Dim Str As String
lj = ActiveWorkbook.Path '查找工作簿
nm = ActiveWorkbook.Name
dirname = Dir(lj & "\*.xls")
m = 0
Do While dirname <> ""
If dirname <> nm Then
Workbooks.Open Filename:=lj & "\" & dirname '打开一个工作簿
Range("B10").Select
Sheet1.Activate '激活sheet1,但是无效
End If
dirname = Dir
Loop
end sub

Sheet1.Activate '激活sheet1,但是无效

你这个语句是激活的代码所有在工作簿的Sheet1,要激活新工作簿的Sheet1应该这样写:

ActiveWorkbook.Sheets(1).Activate
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-05-17
亲,试一下这个:Sheets(1).Activate追问

换了你的语句后,程序指向我的汇总表的sheet1了

相似回答