繁体   English   中英

循环浏览当前目录中的所有文件

[英]Loop through all the files in the current directory

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

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

文件目录是
X:\\ New Gas Team 2016 \\错误的TTZ数据库\\读取流\\ UMR

转换后,文件需要保存到以下文件中
X:\\ New Gas Team 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

您将使用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属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM