alex_bn_lee

导航

【037】Excel 中遍历修改文件(VBA)

    Sub OpenCloseArray()
        Dim MyFile As String
        Dim Arr(100) As String
        Dim count As Integer
        MyFile = Dir("F:\Desktop\98、99年\" & "*.xls")  '修改文件夹路径
        count = count + 1
        Arr(count) = MyFile
          
        Do While MyFile <> ""
            MyFile = Dir
            If MyFile = "" Then
                Exit Do
            End If
            count = count + 1
            Arr(count) = MyFile         '将文件的名字存在数组中
        Loop
          
        For i = 1 To count
            Workbooks.Open Filename:="F:\Desktop\98、99年\" & Arr(i)  '循环打开Excel文件
        
        '编辑范围
        
                Dim allRow As Integer
                allRow = Cells(Rows.count, 1).End(xlUp).Row
                
                For j = 2 To allRow
                    Cells(j, 1) = Cells(j, 1) / 1000
                Next
                
         '编辑范围
            
            ActiveWorkbook.Close savechanges:=True       '关闭打开的文件,并保存
        Next
    End Sub

发现在 2010 上面测试有问题,没办法保存,但是在 2007 上是可以的!

对于需要批量改变的内容直接写在编辑范围内就可以了!

posted on 2012-04-27 17:28  McDelfino  阅读(666)  评论(0)    收藏  举报