【问题标题】:Loop through all the files in the current directory循环遍历当前目录下的所有文件
【发布时间】:2016-02-08 10:49:09
【问题描述】:

我的宏要求我实际打开一个逗号分隔的文件, 运行我的宏,保存文件然后重复。

我需要一个宏来遍历当前目录中的所有文件进行更改然后保存自己。

文件目录是
X:\New Gas Team 2016\Incorrect TTZ Database\Read Flows\UMR

转换后的文件需要保存到以下
X:\New Gas Team 2016\Incorrect TTZ Database\Read Flows\UMR\converted

作为启用 Excel 宏的工作簿,文件名与转换前相同。

Sub UMR()
'
' UMR Macro
'
Range("A1").Select
ActiveCell.FormulaR1C1 = "Transaction_Type"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Meter_Point_Ref"
Range("C1").Select
ActiveCell.FormulaR1C1 = "Actual_Read_Date"
Range("D1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Source"
Range("E1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading_Reason"
Range("F1").Select
ActiveCell.FormulaR1C1 = "Meter_Serial_Number"
Range("G1").Select
ActiveCell.FormulaR1C1 = "Meter_Reading"
Range("H1").Select
ActiveCell.FormulaR1C1 = "Meter_ROC_Count"
Range("I1").Select
ActiveCell.FormulaR1C1 = "Meter_Read_Verified"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serialNumber"
Range("J1").Select
ActiveCell.FormulaR1C1 = "Corrector_serial_Number"
Range("K1").Select
ActiveCell.FormulaR1C1 = "Corrector_Uncorrected_Reading"
Range("L1").Select
ActiveCell.FormulaR1C1 = "Corrector_Corrected_Reading"
Range("M1").Select
ActiveCell.FormulaR1C1 = "Corrector_ROC_Count"
Range("N1").Select
ActiveCell.FormulaR1C1 = "Corrector_Usable_IND"
Range("O1").Select
ActiveCell.FormulaR1C1 = "Corrector_Read_Verified"

Columns("C:C").ColumnWidth = 8.29
Columns("C:C").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("E:E").Select
Columns("D:D").EntireColumn.AutoFit
Columns("E:E").EntireColumn.AutoFit

Columns("F:F").EntireColumn.AutoFit
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
Columns("I:I").EntireColumn.AutoFit
Columns("J:J").EntireColumn.AutoFit
Range("Q1").Select
Columns("K:K").EntireColumn.AutoFit
Columns("L:L").EntireColumn.AutoFit
Range("R1").Select
Columns("M:M").EntireColumn.AutoFit
Columns("N:N").EntireColumn.AutoFit
Columns("O:O").EntireColumn.AutoFit
Call M_Z99
End Sub

Sub M_Z99()

'Application.ScreenUpdating = False

'  Dim aCell As Range

ActiveSheet.Range("A2").Select

Trans_count = Cells(Rows.Count, 1).End(xlUp).Row

i = 0

Do Until i = Trans_count

    i = i + 1

    If ActiveCell.Value = "Z99" Then
        Call Delete_row
    ElseIf ActiveCell.Value = "" Then
        MsgBox "done"
    Else: Call T_Skip
    End If

Loop
End Sub

Sub Delete_row()
ActiveCell.EntireRow.Select
Selection.Delete Shift:=xlUp
End Sub

Sub T_Skip()
ActiveCell.Offset(1, 0).Select                                
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您将使用Dir 函数。这是您将代码嵌套在其中的循环示例:

    'Get a count of the files to be merged
    f = Dir(fol & "\" & y & "*Office Hrs.xls*")
    Do While Len(f) > 0
        fc = fc + 1
        f = Dir
    
        'Fail safe escape option
        If fc > 600 & Len(f) > 0 Then
            MsgBox "An error has occurred causing it to appear that there are more than 600 files in the specified folder.", vbOKOnly, "Overly Large File Count"
                Application.Calculation = xlCalculationAutomatic
                Application.ScreenUpdating = True
                End
        End If
    Loop
    

    另外,你应该从你的宏中删除Select,几乎所有它出现的地方。选择不是必需的,会减慢您的代码速度。比如:

    Range("A1").Select
    ActiveCell.FormulaR1C1 = "Transaction_Type"
    

    应该是

    Range("A1").Value = "Transaction_Type"
    

    此外,正如我的示例所暗示的那样,除非您实际上将 Excel 公式添加到单元格中,否则您应该设置 .Value 属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-20
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多