我尝试使用VBA代码在Excel中截取工作表,然后将其保存在指定路径中,但我无法正确保存...
Sub My_Macro(Test, Path)
Dim sSheetName As String
Dim oRangeToCopy As Range
Dim FirstCell As Range, LastCell As Range
Worksheets(Test).Activate
Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
Set FirstCell = Cells(Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues).Row, _
Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, LookIn:=xlValues).Column)
sSheetName = Test ' worksheet to work on
With Worksheets(sSheetName)
.Range(FirstCell, LastCell).CopyPicture xlScreen, xlPicture
.Export Filename:=Path + Test + ".jpg", Filtername:="JPG"
End With
End Sub
Excel不想在截取屏幕后直接执行.Export ...方法。 所以我试图将图片粘贴到新图表中。 Excel将图表图片保存在正确的位置,并在我的图片上添加图表...我还尝试将其粘贴到临时工作表中,但Excel不想将其导出...
任何想法
答案 0 :(得分:5)
忙于LubošSuk的想法。
只需更改图表的大小即可。见下面的脚本。
Sub My_Macro(Test, Path)
Test = "UNIT 31"
Dim sSheetName As String
Dim oRangeToCopy As Range
Dim FirstCell As Range, LastCell As Range
Worksheets(Test).Activate
Set LastCell = Cells(Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Row, _
Cells.Find(What:="*", SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, LookIn:=xlValues).Column)
Set FirstCell = Cells(Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlRows, _
SearchDirection:=xlNext, LookIn:=xlValues).Row, _
Cells.Find(What:="*", After:=LastCell, SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, LookIn:=xlValues).Column)
sSheetName = Test ' worksheet to work on
With Worksheets(sSheetName).Range(FirstCell, LastCell)
.CopyPicture xlScreen, xlPicture
'Getting the Range height
PicHeight = .Height
'Getting the Range Width
PicWidth = .Width
''.Export Filename:=Path + Test + ".jpg", Filtername:="JPG" 'REMOVE THIS LINE
End With
With Worksheets(sSheetName)
'Creating the Chart
.ChartObjects.Add(30, 44, PicWidth, PicHeight).Name = "TempChart"
With .ChartObjects("TempChart")
'Pasting the Image
.Chart.Paste
'Exporting the Chart
.Chart.Export Filename:=Path + Test + ".jpg", Filtername:="JPG"
End With
.ChartObjects("TempChart").Delete
End With
End Sub
答案 1 :(得分:2)
.chart.export
的解决方案,这对我来说似乎最方便。请查看我的代码,我认为您可以根据需要轻松更新它。简单的想法是创建图表,粘贴你想要的截图,将图表导出到图片然后删除id。简单而优雅。随意询问是否存在问题
Sub takeScreen()
Dim mainSheet As Worksheet
Set mainSheet = Sheets("Input-Output")
Dim path As String
path = Application.ActiveWorkbook.path
Application.ScreenUpdating = False
If Dir(path & "\figures\", vbDirectory) = "" Then
MsgBox "Directory figures not found. Cannot save image."
Exit Sub
End If
With mainSheet
.ChartObjects.Add(30, 44, 765, 868).Name = "exportChart"
With .ChartObjects("exportChart")
.Chart.ChartArea.Border.LineStyle = xlNone
.Chart.ChartArea.Fill.Visible = False
mainSheet.Range(mainSheet.Cells(4, "B"), mainSheet.Cells(60, "L")).CopyPicture
.Chart.Paste
.Chart.Export fileName:=path & "\figures\" & "fatigue_summary.png ", FilterName:="png"
End With
.ChartObjects("exportChart").Delete
End With
Application.ScreenUpdating = True
End Sub
根据您的评论,我认为您可以根据行/列大小及其计数来计算图表大小。或者,您可以使用单元格位置和大小属性调整图表大小。 (look for .cells().width, .cells().height,.cells().top, .cells().left
)