【发布时间】:2021-01-18 13:54:48
【问题描述】:
子 create_files_add_data(get_input)
lastrow1 = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row 'This is to get a count of rows in the source file.
' Sheet2.Range("A1:K1").Copy ' 这个标题行的副本有效。但是for循环中的数据行的副本不起作用!!!
Dim target_workbook As Workbook
Dim target_worksheet As Worksheet
Set target_workbook = Workbooks.Add
Set target_worksheet = target_workbook.Worksheets("Sheet1")
For r = 2 To lastrow1 ' This is where we use the row count to loop through all the rows to check the deptid and copy the rows
If Worksheets("Sheet1").Range("B" & r).Value = get_input Then
Worksheets("Sheet1").Rows(r).Copy
' lastrow2 = target_worksheet("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
' target_worksheet("Sheet1").Range("A" & lastrow2 + 1).Select
target_worksheet.PasteSpecial xlPasteValues ' pasting here into the target workbook (but doesn't work!!!)
'ActiveCell.PasteSpecial xlPasteValues
End If
Next r
With target_workbook
.SaveAs filename:="C:\Users\" & get_input & ".xlsx"
target_workbook.Close
End With
结束子
【问题讨论】:
-
从设置的工作簿中删除 .Sheets,您在下一行分配工作表。
-
谢谢,但这没有帮助。仍然给我同样的错误。
-
那么字符串可能不正确