2017-09-01 82 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