我正在尝试将单元格(字符串)从一个工作表(第4行的“摘要文档”)复制到第二个工作表中的表格(“工作人员1”,从B10开始)。我相信我已正确定义了所有元素。当我第一次运行时,它可以工作。但是,它在第二次运行时给出了1004错误。我很确定这是我的if / then语句,它会查找下一个空行,但不确定问题是什么。
Dim FileName As String, FileNumber As String, RecordingPeriod As String, RecordingType As String, Auditor As String, ReviewDate As String`
Dim A1 As String, A2 As String, A3 As String, A4 As String, A5 As String, A6 As String
Worksheets("Summary Document").Select
FileName = Range("C4")
FileNumber = Range("D4")
RecordingPeriod = Range("E4")
RecordingType = Range("F4")
Auditor = Range("G4")
ReviewDate = Range("H4")
A1 = Range("J4")
A2 = Range("K4")
A3 = Range("L4")
A4 = Range("M4")
A5 = Range("N4")
A6 = Range("O4")
Worksheets("Worker 1").Select
Worksheets("Worker 1").Range("B9").Select
If Worksheets("Worker 1").Range("B9").Offset(1, 0) <> "" Then
Worksheets("Worker 1").Range("B9").End(x1Down).Select
End If
ActiveCell.Offset(1, 0).Select
ActiveCell.Value = FileName
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = FileNumber
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = RecordingPeriod
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = RecordingType
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Auditor
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = ReviewDate
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = A1
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = A2
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = A3
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = A4
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = A5
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = A6
End Sub
答案 0 :(得分:0)
另外,除上述评论外,代码可以大大简化:
Sub hhhhh()
Dim ows as Worksheet
Dim tws As Worksheet
Dim rw as long
Set ows = Worksheets("Summary Document")
Set tws = Worksheets("Worker 1")
If tws.Range("B10") <> "" Then
rw = 10
Else
rw = tws.Range("B9").End(xlDown).Row + 1
End If
tws.Range("B" & rw & ":G" & rw).Value = ows.Range("C4:H4").Value
tws.Range("I" & rw & ":N" & rw).Value = ows.Range("J4:O4").Value
End Sub
应尽可能避免使用.Select
。有关很好的选择,请参阅This Link。