我絕不會建議使用UsedRange
原因:
你的數據可能是從單元格A1到B4和圖像是說D1。該UsedRange
在這種情況下,將永遠是A1:B4
而不是A1:F6
![enter image description here](https://i.stack.imgur.com/iAJs6.png)
如果要複製您的圖片,然後要麼找到了工作表的「實際範圍」或指定範圍
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()
嗨亞洲時報Siddharth這太棒了.....這真的幫助了我很多。感謝你的努力和一系列的感謝。 – Rupesh
給我提示。我的excel文件由該範圍內的圖像組成。除了圖像以外,所有內容都被複制。任何想法? – Rupesh
爲什麼不復制整個工作表而不是指定活動範圍? –