1
Sub GetFileList()

    Dim xFSO As Object
    Dim xFolder As Object
    Dim xFile As Object
    Dim objOL As Object
    Dim Msg As Object
    Dim xPath As String
    Dim thisFile As String
    Dim i As Integer
    Dim lastrow As Long
    
    xPath = Sheets("UI").Range("D7")
    
    Set xFSO = CreateObject("Scripting.FileSystemObject")
    
    Set xFolder = xFSO.GetFolder(xPath)

    i = 1

    
    For Each xFile In xFolder.Files
     i = i + 1
        
        Worksheets("Info").Cells(i, 1) = xPath
        Worksheets("Info").Cells(i, 2) = Left(xFile.Name, InStrRev(xFile.Name, ".") - 1)
        Worksheets("Info").Cells(i, 3) = Mid(xFile.Name, InStrRev(xFile.Name, ".") + 1)
        Worksheets("Info").Cells(i, 6) = Left(FileDateTime(xFile), InStrRev(FileDateTime(xFile), " ") - 1)

        
    Next
    Set Msg = Nothing
    
    Worksheets("Info").Visible = True
    Worksheets("Info").Activate
    



End Sub

The code to extract file information from a folder. The issue is when I change the folder path, it overwrites on the previously fetched data.

Sheet -UI is where the sub executed on press of button, Sheet Info is the place where the data needs to be pasted.

How to write the code to add a new row of data after the data which is already available. If the sheet is blank then add data from the 1st ROW otherwise add data from the LAST ROW.

Win_umra
  • 23
  • 4
  • https://stackoverflow.com/questions/71180/how-can-i-find-last-row-that-contains-data-in-a-specific-column – Tim Williams Jul 02 '20 at 06:16
  • @TimWilliams -- This looks to be a different functionality implemented. Can you pls help with the above code? – Win_umra Jul 02 '20 at 06:24
  • Your question is how to find the last row/first empty row. If there's no data that will be row1 – Tim Williams Jul 02 '20 at 06:27
  • @TimWilliams suggestion is your answer. This can very easily be implemented in your code. Please ask nicely and I'm sure Tim will help you :) – Zac Jul 02 '20 at 08:58

2 Answers2

0
Sheets("UI").Range("A1").End(xlDown).Select
i = Selection.Row + 1
0

Try replacing

i = 1

with

i = Worksheets("Info").UsedRange.Rows.Count + 1

This will set i to 1 the first time around, and to the first free row ever after. New data will be added below the existing data, if there is any.

  • It is pasting out the values in random cell, even if I have an empty sheet. Only headers are there i.e. sheet is empty starting from cell A2. – Win_umra Jul 02 '20 at 07:27