我永远不会推荐使用UsedRange
原因:
您的数据可能来自单元格 A1 到 B4,图像在 D1 中。在UsedRange
这种情况下,将永远是A1:B4
而不是A1:F6
如果要复制图片,请找出工作表的“实际范围”或指定范围
lobjCurrentWorkSheet.Range("A1:F6").Copy()
lobjTargetExcelWorkSheet.Paste()
编辑:我试图将我的手放在 VB.Net 上,以便我可以给你一个确切的示例代码。我终于做到了:)
看到这个(这将复制实际范围,而不必指定它。)
Dim shp As Excel.Shape
Dim lCol As Integer = 0
'~~> Loop through all shapes and find the last col of the shape
For Each shp In lobjCurrentWorkSheet.Shapes
If shp.BottomRightCell.Column > lCol Then _
lCol = shp.BottomRightCell.Column
Next
With lobjCurrentWorkSheet
'~~> Find actual last Row
Dim LastRow As Integer = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=Excel.XlLookAt.xlPart, _
LookIn:=Excel.XlFindLookIn.xlFormulas, _
SearchOrder:=Excel.XlSearchOrder.xlByRows, _
SearchDirection:=Excel.XlSearchDirection.xlPrevious, _
MatchCase:=False).Row
'~~> Find actual last column
Dim LastColumn As Integer = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=Excel.XlLookAt.xlPart, _
LookIn:=Excel.XlFindLookIn.xlFormulas, _
SearchOrder:=Excel.XlSearchOrder.xlByColumns, _
SearchDirection:=Excel.XlSearchDirection.xlPrevious, _
MatchCase:=False).Column
'~~> Check if we have the correct last columnm
If LastColumn < lCol Then LastColumn = lCol
.Range("A1:" & Split(.Cells(, LastColumn).Address,
"$")(1) & LastRow).Copy()
End With
'~~> Copies to the current active cell in lobjTargetExcelWorkSheet
lobjTargetExcelWorkSheet.Paste()