vba打开excel文件遍历sheet的名字和指定单元格的值

今天项目上有个应用,获取指定Excel文件下的所有sheet的名称以及当前sheet中指定单元格的值,并把他们写到固定的sheet中去,看了下,文件比较多,而且每个文件sheet的个数比较多,也不一样,所以打算写个程序来帮我们实现任务,代码很简单,也写的比较死板。欢迎大家给出意见及你的代码:

Sub aaaa()
Dim sh1, sh2 As Worksheet
Dim shName, cellValue As String
'On Error Resume Next

Set sh1 = Workbooks(1).Sheets(1)
'Workbooks.Open Filename:="D:\_jack\Finance Report\Report Layout\TA Opex Report 2014.xlsx"
Workbooks.Open Filename:="D:\_jack\Finance Report\Report Layout\Rolling PL Template v1 (2).xlsx"

For i = 1 To Workbooks(2).Worksheets.Count
    Workbooks(2).Activate
    Set sh2 = ActiveWorkbook.Worksheets(i)
    sh2.Activate
    n = sh2.Name
    'v = sh2.Cells(5, 7).Value  'G5单元格
    v = sh2.Cells(4, 3).Value  'C4单元格
    
    Workbooks(1).Activate
    sh1.Activate
    sh1.Cells(i, 1) = n
    sh1.Cells(i, 2) = v
    
Next
Workbooks(2).Close
End Sub
  


版权:以上代码由本人开发,版权归 http://www.cnblogs.com/mq0036 个人所有。若用于非商业目的,您可以自由转载,转载请注明出处。

posted on 2014-12-31 15:29  jack_Meng  阅读(32181)  评论(0编辑  收藏  举报

导航