Paste data to different sheets excel vba

advertisements

i got problem for paste data into different sheets. The program i execute keep on save the data in the same Sheet although i set it Sheets1 / Sheets2 . Please help,Thanks.

First Button:

NextRow = ThisWorkbook.Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
'Loop through each file in the folder
For Each objFile In objFolder.files
'List the name, size, and date/time of the current file
Cells(NextRow, 1).Value = objFile.Name
Cells(NextRow, 2).Value = objFile.Path
Cells(NextRow, 3).Value = Format(objFile.DateLastModified, "dd/mm/yyyy hh:mm:ss")
'Find the next row
NextRow = NextRow + 1
Next objFile

Second Button:

NextRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row + 1
'Loop through each file in the folder
For Each objFile In objFolder.files
'List the name, size, and date/time of the current file
Cells(NextRow, 1).Value = objFile.Name
Cells(NextRow, 2).Value = objFile.Path
Cells(NextRow, 3).Value = Format(objFile.DateLastModified, "dd/mm/yyyy hh:mm:ss")
'Find the next row
NextRow = NextRow + 1
Next objFile

Regards, YY


You should use fully qualified name for cells (specify sheet which cells belongs to). With statement should do the trick:

First button

With ThisWorkbook.Sheets("Sheet2")
    NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    'Loop through each file in the folder
    For Each objFile In objFolder.Files
        'List the name, size, and date/time of the current file
        .Cells(NextRow, 1).Value = objFile.Name
        .Cells(NextRow, 2).Value = objFile.Path
        .Cells(NextRow, 3).Value = Format(objFile.DateLastModified, "dd/mm/yyyy hh:mm:ss")
        'Find the next row
        NextRow = NextRow + 1
    Next objFile
End With

Second button

With ThisWorkbook.Sheets("Sheet1")
    NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
    'Loop through each file in the folder
    For Each objFile In objFolder.Files
        'List the name, size, and date/time of the current file
        .Cells(NextRow, 1).Value = objFile.Name
        .Cells(NextRow, 2).Value = objFile.Path
        .Cells(NextRow, 3).Value = Format(objFile.DateLastModified, "dd/mm/yyyy hh:mm:ss")
        'Find the next row
        NextRow = NextRow + 1
    Next objFile
End with


BTW, as I see you have a lot of repeated code. I recommend you to use additional sub with sheet name as parametr:

Sub test(sheetName As String)
    With ThisWorkbook.Sheets(sheetName)
        NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        'Loop through each file in the folder
        For Each objFile In objFolder.Files
            'List the name, size, and date/time of the current file
            .Cells(NextRow, 1).Value = objFile.Name
            .Cells(NextRow, 2).Value = objFile.Path
            .Cells(NextRow, 3).Value = Format(objFile.DateLastModified, "dd/mm/yyyy hh:mm:ss")
            'Find the next row
            NextRow = NextRow + 1
        Next objFile
    End With
End Sub

and call it for first button:

Call test("Sheet2")

and for second button:

Call test("Sheet1")