我有这个代码,但它只粘贴标题和不完整的数据列。

我希望代码粘贴整个列,如果标题匹配。 到目前为止,它只是粘贴第(1)行的值。 非常感谢。 如果还有其他问题,我会很快回复。 我拿出所有的小东西和什么东西。

Sub sample() Set sh1 = Sheets("Dec Demand") Set sh2 = Sheets("List") Set sh3 = Sheets("Results") With sh2 Set rngLookupValues = .Range("J2", .Range("J" & .Rows.Count).End(xlUp)) End With Debug.Print rngLookupValues.Address With sh1 Set rngHeaders = .Range("A1", .Range("A1").End(xlToRight)) End With Debug.Print rngHeaders.Address For Each cValue In rngLookupValues lngColumnToCopy = WorksheetFunction.Match(cValue, rngHeaders, 0) Debug.Print lngColumnToCopy With sh1 Set rngCellsToCopy = .Range(.Cells(1, lngColumnToCopy), .Cells(Rows.Count, lngColumnToCopy).End(xlUp)) ' HERE i want to have a copy entire column End With Debug.Print rngCellsToCopy.Address With sh3 lngCurFirstEmptyColumn = .Cells(1, Columns.Count).End(xlToLeft).Column + 1 End With Debug.Print lngCurFirstEmptyColumn sh3.Cells(1, lngCurFirstEmptyColumn).Resize(rngCellsToCopy.Rows.Count) = rngCellsToCopy Next cValue With sh3.Range("A1") If Len(.Value) < 1 Then .EntireColumn.Delete End If End With End Sub 

你需要改变

sh3.Cells(1, lngCurFirstEmptyColumn).Resize(rngCellsToCopy.Rows.Count) = rngCellsToCopy

sh3.Cells(1, lngCurFirstEmptyColumn).Resize(rngCellsToCopy.Rows.Count).Value = rngCellsToCopy.Value

要么

rngCellsToCopy.Copy sh3.Cells(1, lngCurFirstEmptyColumn)

(不知道为什么,但是你的声明不能复制任何东西,但是加上.Value修复了这个问题)


'在这里我想有一个复制整个列

如果你真的需要复制EntireColumn (这会让你的代码非常慢),请按照下面的步骤操作

1)改变

 Set rngCellsToCopy = .Range(.Cells(1, lngColumnToCopy), .Cells(Rows.Count, lngColumnToCopy).End(xlUp)) 

 Set rngCellsToCopy = .Cells(1, lngColumnToCopy).EntireColumn 

2)然后改变

  sh3.Cells(1, lngCurFirstEmptyColumn).Resize(rngCellsToCopy.Rows.Count) = rngCellsToCopy 

  sh3.Cells(1, lngCurFirstEmptyColumn).EntireColumn.Value = rngCellsToCopy.Value 

或者你也可以使用rngCellsToCopy.Copy sh3.Cells(1, lngCurFirstEmptyColumn)


还有一点点注意:用忘了加上句号. 在行Set rngCellsToCopy = .Range(.Cells(1, lngColumnToCopy), .Cells(Rows.Count, lngColumnToCopy).End(xlUp))之前Set rngCellsToCopy = .Range(.Cells(1, lngColumnToCopy), .Cells(Rows.Count, lngColumnToCopy).End(xlUp))