我得到了“#Value!”对于具有配方的单元格或如果列是串联填充的单元格。我试图在某些条件下将内容从一张纸复制到另一张。
Sub TransferDataMacro()
Sheets("Summary").UsedRange.Cells.Clear
Sheets("OSS").Range("B2:K2").Copy Destination:=Sheets("Summary").Range("B2")
Dim xRg As Range
Dim i, LastRow As Long, xdate As Date, change As String
j = 3
LastRow = Sheets("OSS").Range("G" & Rows.Count).End(xlUp).Row
For i = 3 To LastRow
change = "G" + CStr(i)
Sheets("OSS").Activate
xdate = Worksheets("OSS").Range(change)
If Month(Date) = Month(xdate) And Year(Date) = Year(xdate) Then
Sheets("OSS").Range(Cells(i, "B"), Cells(i, "K")).Copy
Sheets("Summary").Activate
Sheets("Summary").Range(Cells(j, "B"), Cells(j, "K")).Select
ActiveSheet.Paste
j = j + 1
End If
Next i
End Sub
答案 0 :(得分:0)
如果您希望复制值和单元格格式,则需要使用PasteSpecial
:
Sub TransferDataMacro()
Dim i As Long, LastRow As Long, xdate As Date
Dim j As Long
With Worksheets("OSS")
Worksheets("Summary").UsedRange.Cells.Clear
.Range("B2:K2").Copy Destination:=Worksheets("Summary").Range("B2")
j = 3
LastRow = .Cells(.Rows.Count, "G").End(xlUp).Row
For i = 3 To LastRow
xdate = .Cells(i, "G").Value
If Month(Date) = Month(xdate) And Year(Date) = Year(xdate) Then
.Cells(i, "B").Resize(1, 10).Copy
Worksheets("Summary").Cells(j, "B").PasteSpecial Paste:=xlPasteValues
Worksheets("Summary").Cells(j, "B").PasteSpecial Paste:=xlPasteFormats
j = j + 1
End If
Next i
End With
End Sub