Вроде бы получилось:
| Код |
|---|
Sub Range_to_Picture()
Dim sName As String, wsTmpSh As Worksheet, sPath As String, sFileName As String, Rng As Range
Dim startCell As String, endCell As String
Dim width As Double, height As Double
' Get the Excel file path
sPath = ThisWorkbook.Path & Application.PathSeparator
' Get the start and end cell addresses from the "Указатели" sheet
With ThisWorkbook.Sheets("Указатели")
startCell = .Range("A2").Value
endCell = .Range("B2").Value
End With
' Get the Excel file name without extension
sFileName = Left(ThisWorkbook.Name, InStrRev(ThisWorkbook.Name, ".", -1, vbTextCompare) - 1)
' Define the range based on the start and end cells
With ThisWorkbook.Sheets("Данные")
Set Rng = .Range(startCell & ":" & endCell)
End With
' Calculate the width and height based on the range size
width = (Rng.Columns.Count * 92.44) ' Adjust multiplier as needed
height = (Rng.Rows.Count * 33.58) ' Adjust multiplier as needed
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wsTmpSh = ThisWorkbook.Sheets.Add
With wsTmpSh.ChartObjects.Add(0, 0, width:=width, height:=height).Chart
.ChartArea.Border.LineStyle = 0
.Parent.Select
Rng.CopyPicture
.Paste
.Export fileName:=sPath & sFileName & ".jpg", FilterName:="jpg"
.Parent.Delete
End With
wsTmpSh.Delete
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "Картинка сохранена!" & vbNewLine & sPath & sFileName & ".jpg", vbInformation, "Конец"
End Sub |