循环遍历当前目录中的所有文件

时间:2016-02-08 10:49:10

标签: excel vba excel-vba

我的宏要求我以物理方式打开逗号分隔文件, 运行我的宏,保存文件然后重复。

我需要一个宏来浏览当前目录中的所有文件然后保存自己。

文件目录为
X:\新的气体团队2016 \不正确的TTZ数据库\读取流量\ UMR

转换后,文件需要保存到以下
中 X:\新的气体团队2016 \不正确的TTZ数据库\读取流量\ UMR \转换

作为启用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

1 个答案:

答案 0 :(得分:0)

您使用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属性应该是您应该设置的