Excel VBA - 如何使用不同的子例程逐行从工作簿的 sheet 复制到另一个工作簿的 sheet
Excel VBA - How to copy row by row from a sheet of workbook to another Workbook's sheet using different subroutine
我正在尝试将数据从工作簿的 sheet 复制到另一个工作簿,但代码显示错误 1004。这是我在子程序 findDuplicates()[=18 的 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
错误显示是-Error Image
错误前的本地值 - Locals Image
我猜是 Sheet2.Rows("row")。这不应该只是 Sheet2.Rows(row) 而不是吗?
编辑:Sheet.Rows("i") 也可能是 sheet.rows(i)。
我正在尝试将数据从工作簿的 sheet 复制到另一个工作簿,但代码显示错误 1004。这是我在子程序 findDuplicates()[=18 的 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
错误显示是-Error Image
错误前的本地值 - Locals Image
我猜是 Sheet2.Rows("row")。这不应该只是 Sheet2.Rows(row) 而不是吗?
编辑:Sheet.Rows("i") 也可能是 sheet.rows(i)。