2017-09-01 87 views
0

列A包含文件名不带扩展名,我想B栏显示的最后修改时间和日期。填充时间戳

Sub Workbook_Open() 
Dim Path As String 
Path = ThisWorkbook.Sheets("E").Range("B14") 
Range("B4").Value = FileDateTime(Path & ThisWorkbook.Sheets("Projekterfassung_Monitoring").Range("A4") & ".xlsx") 
Range("B5").Value = FileDateTime(Path & ThisWorkbook.Sheets("Projekterfassung_Monitoring").Range("A5") & ".xlsx") 
Range("B6").Value = FileDateTime(Path & ThisWorkbook.Sheets("Projekterfassung_Monitoring").Range("A6") & ".xlsx") 
Range("B7").Value = FileDateTime(Path & ThisWorkbook.Sheets("Projekterfassung_Monitoring").Range("A7") & ".xlsx") 
(...) 
End Sub 

如何自动执行此操作,以便我不必在每行的代码中编写一行代码?

回答

1

你只需要实现一个基本的循环

Sub Workbook_Open() 
    Dim Path As String 
    Dim i As Long 
    Path = ThisWorkbook.Sheets("E").Range("B14") 

    With ThisWorkbook.Sheets("Projekterfassung_Monitoring") 
     For i = 4 To .Cells(.Rows.Count, 1).End(xlUp).Row 
      .Range("B" & i).Value = FileDateTime(Path & .Range("A" & i) & ".xlsx") 
     Next i 
    End With 
End Sub