为什么我的范围不粘贴?

以下是我的VBA代码,由于某些原因代码将运行,但实际上不粘贴在我需要粘贴的范围内。 任何人有任何想法,为什么它不会粘贴我的价值观?

程序进入我正在寻找的我select的单元格中,但是现在活动单元格变成了我的范围,我试图将它粘贴在那里。 任何信息都将有所帮助,它只是不想粘贴在我select的范围内的值。

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 

我在这里修改了代码,但请阅读我在我的评论中提供的链接 ,将来不会有这些错误。

我也评论了我对代码做的重构。 另外,请注意,我将单元格和范围分配给了父级工作表.(请参阅@ 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 

由于您使用“With”语句,您需要添加一个“。” 在“单元格”和“范围”以及您所做的任何其他参考之前。 例如:

 With myWorksheet .range("A1").copy End with 

所以,在这种情况下,问题是您仍然保持在同一张工作表上,并清除已粘贴的单元格的内容。