[英]Paste data to different Sheet excel vba
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 .
尽管我将其设置为Sheets1 / Sheets2,但我执行的程序仍将数据保存在同一张Sheet中。 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 问候,YY
You should use fully qualified name for cells (specify sheet which cells belongs to). 您应为单元格使用完全限定的名称(指定单元格所属的工作表)。
With
statement should do the trick: With
语句应该可以解决问题:
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")
You need to qualify Cells().........like: 您需要限定 Cells().........类似:
Sheets("Sheet1").Cells(NextRow, 1).Value = objFile.Name
or 要么
Sheets("Sheet2").Cells(NextRow, 1).Value = objFile.Name
Same for all such lines of code......................Otherwise you need a With 所有此类代码行都相同.............否则,您需要一个With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.