以下是我的VBA代码,由于某些原因代码将运行但实际上并没有粘贴到我需要粘贴的范围内。任何人都有任何想法,为什么它不会粘贴我的价值观?
程序会转到我正在寻找的所选单元格,但现在activecell成为我的范围,我试图将其粘贴到那里。任何信息都会有所帮助,它只是不想粘贴我选择的范围内的值。
Sub Macro1()
Dim Form1033 As Worksheet
Dim CleaningSchedule As Worksheet
Set Form1033 = Worksheets("Form1033andForm1034")
Set CleaningSchedule = Worksheets("CleaningSchedule")
Dim Day As Range
Set Day = Form1033.Range("$J$3")
With Form1033
Range("$G$5:$G$18").Select
Selection.Copy
End With
With CleaningSchedule
Dim i As Integer
For i = 6 To 37
If Cells(4, i).Value = Day.Value Then
Cells(5, i).Select
Range(ActiveCell, Cells(ActiveCell.Rows + 13, ActiveCell.Column)).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
Next i
End With
Form1033.Select
Application.CutCopyMode = False
Range("$G$5:$G$18").ClearContents
MsgBox "Scoresheet Updated"
End Sub
答案 0 :(得分:3)
我在这里修改了代码,但请阅读我在评论中提供的链接,以后您不会遇到这些错误。
我还评论了我对代码所做的重构。另请注意,我已使用.
将单元格和范围分配给父工作表。 (请参阅@ BruceWayne在他对原始问题的评论中的链接)
Sub Macro1()
Dim Form1033 As Worksheet
Dim CleaningSchedule As Worksheet
Set Form1033 = Worksheets("Form1033andForm1034")
Set CleaningSchedule = Worksheets("CleaningSchedule")
Dim Day As Range
Set Day = Form1033.Range("$J$3")
'copy the range directly
Form1033.Range("$G$5:$G$18").Copy
With CleaningSchedule
Dim i As Integer
For i = 6 To 37
If .Cells(4, i).Value = Day.Value Then
'paste directly to range and i also combined 13 rows plus row 5, since you are always using the same row
Range(.Cells(5,i), Cells(18,i)).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End If
Next i
End With
'clear contenst directly
Form1033.Range("$G$5:$G$18").ClearContents
MsgBox "Scoresheet Updated"
End Sub
答案 1 :(得分:0)
因为您正在使用"使用"声明,你需要添加一个"。"在"细胞"前面"范围"和你做的任何其他参考。例如:
With myWorksheet
.range("A1").copy
End with
因此,在这种情况下的问题是您仍然保持在同一工作表上并清除您粘贴的单元格的内容。