我遇到了将数据粘贴到不同工作表中的问题。我执行的程序继续保存在同一张表中的数据,虽然我设置它Sheets1 / Sheets2。请帮忙,谢谢。
第一个按钮:
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
第二个按钮:
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
此致 YY
答案 0 :(得分:0)
您应该为单元格使用完全限定名称(指定单元格所属的表格)。 With
语句可以解决问题:
第一个按钮
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
第二个按钮
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 ,因为我看到你有很多重复的代码。我建议您使用带有工作表名称的附加子作为参数:
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
并将其命名为第一个按钮:
Call test("Sheet2")
和第二个按钮:
Call test("Sheet1")
答案 1 :(得分:0)
您需要限定 Cells().........喜欢:
Sheets("Sheet1").Cells(NextRow, 1).Value = objFile.Name
或
Sheets("Sheet2").Cells(NextRow, 1).Value = objFile.Name
所有这些代码行相同......................否则你需要一个 With