简体   繁体   中英

Loop through all the files in the current directory

My macro requires me to physically open a comma delimited file, run my macro, save the file then repeat.

I require a macro to go through all the files in the current directory make the changes then save itself.

The file directory is
X:\\New Gas Team 2016\\Incorrect TTZ Database\\Read Flows\\UMR

After conversion the file needs to be saved into the following
X:\\New Gas Team 2016\\Incorrect TTZ Database\\Read Flows\\UMR\\converted

as an Excel macro enabled workbook with the file name the same as before conversion.

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

You'd use the Dir function. Here's an example of the loop you'd nest your code in:

'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

Also, you should remove Select from your macro, pretty much everywhere it appears. Selects aren't required and slow your code down. So for example:

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

should just be

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

Also, as my example kind of alludes to, unless you're actually adding an Excel formula to a cell, the .Value property is what you should be setting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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