![](/img/trans.png)
[英]EXCEL VBA: Copy Sheet from a workbook to another workbook in different location
[英]Excel VBA - How to copy row by row from a sheet of workbook to another Workbook's sheet using different subroutine
我正在尝试将数据从一张工作簿复制到另一个工作簿,但是代码显示错误1004。这是我在Subroutine findDuplicates()的For循环中遇到错误的代码
Option Explicit
Sub GenerateErrorSheet()
Dim MyBook As Workbook
Dim newBook As Workbook
Dim FileNm As String
Dim rowCount As Integer
Set MyBook = ThisWorkbook
rowCount = 1
FileNm = ThisWorkbook.Path & "\" & "ErrorSheet-" & Date & ".xls"
Set newBook = Workbooks.Add
With newBook
Call findDuplicates(Worksheets("pid"), "PID Generator", rowCount,newBook.Worksheets("Sheet1"))
rowCount = rowCount + 4
'Call findDuplicates(Worksheets("behavioural"), "Behavioural Measurement")
'rowCount = rowCount + 4
'Call findDuplicates(Worksheets("physical"), "Physical Measurement")
'rowCount = rowCount + 4
'Call findDuplicates(Worksheets("biochemical"), "Biochemical Measurement")
'Save new wb with XLS extension
.SaveAs Filename:=FileNm, FileFormat:=xlNormal, CreateBackup:=False
.Close Savechanges:=False
End With
MsgBox "Error Sheet Generated Successfully." & vbNewLine & "Name Of Sheet - ErrorSheet-" & Date & ".xls" & vbNewLine & "Saved Location - " & FileNm
End Sub
我的findDuplicates子例程在for循环中给出了错误
Sub findDuplicates(ByVal sheet As Worksheet, name As String, ByRef row As Integer, ByVal Sheet2 As Worksheet)
Dim i As Integer
Dim numRow As Integer
numRow = sheet.Range("J2", sheet.Range("J2").End(xlDown)).Rows.Count
With Sheet2
Range(Cells(row, "A"), Cells(row, "L")).MergeCells = True
With Cells(row, "A")
.Font.name = "Bell MT"
.Font.FontStyle = "Bold Italic"
.Font.Size = 20
.Font.Color = RGB(255, 99, 71)
.Value = "Multiple Forms Found in " & name & " for single household"
End With
row = row + 1
End With
For i = 1 To numRow
sheet.Rows("i").Copy Sheet2.Rows("row")
row = row + 1
Next i
End Sub
错误显示为- 错误图像
错误之前的本地值- 当地图片
我猜是Sheet2.Rows(“ row”)。 这不应该只是Sheet2.Rows(row)吗?
编辑:另外Sheet.Rows(“ i”)可能是sheet.rows(i)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.