vba little change in code (entire workbook replace with one sheet in code)

hematinsite

New Member
Joined
Nov 3, 2020
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
hi dears
i have this 2 macro for excel to jpg convert and work perfectly , but my workbook have 10 sheets and i must repeat this code for every sheets . i need change these codes
macro a :
1-for do it for entire workbook automativally and with no question
2-every export jpg file name is same as sheet name in my excel file path
3- this macro need select range before run it . i need automatically printarea by default selected for each sheets , for to convert in jpg .

macro a:

VBA Code:
Sub ExportRangeToJPG()
 '--exports selected range to jpg file
 '  default filename is address of selected range
 '  based on code example posted at:
 '     http://www.emoticode.net/visual-basic/vba-export-excel-range-as-image-and-save-as-file.html

 Dim vFilePath As Variant
 Dim sDefaultName As String
 
 If TypeName(Selection) <> "Range" Then
   MsgBox "Selection is not a range of cells."
   Exit Sub
 End If

 With Selection
   sDefaultName = Replace(.Address(0, 0, xlA1, 1), ":", "to") & ".jpg"
   vFilePath = Application.GetSaveAsFilename( _
    InitialFileName:=sDefaultName, _
    FileFilter:="JPEG File Interchange Format (*.jpg), *.jpg", _
    Title:="Save As")
    
 '--exit if cancelled by user,
 If vFilePath = False Then Exit Sub
   '--Make picture of selection and copy to clipboard
   .CopyPicture Appearance:=xlScreen, Format:=xlBitmap
   
   '--Create an empty chart with exact size of range copied
   With ActiveSheet.ChartObjects.Add( _
        Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
     .Name = "TempChart"
     .Activate
   End With
 End With

 '--Paste into chart area, export to file, delete chart.
 ActiveChart.Paste
 With ActiveSheet.ChartObjects("TempChart")
  .Chart.Export CStr(vFilePath)
  .Delete
 End With
End Sub

and macro b :
and the next macro is for adjust zoom total sheets automatically . i need this macro , merge in with above macro a . and in first , this macro b run and then above macro a.

macro b

VBA Code:
[CODE]Sub SetZoom()

Dim ws As Worksheet
Application.ScreenUpdating = False    'Optional
For Each ws In ActiveWorkbook.Worksheets
    ws.Activate
    ActiveWindow.Zoom = 200
Next
Application.ScreenUpdating = True

End Sub
[/CODE]
and in the end , i attached example xlsm file for try on it on google drive .
main.xlsm
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
hi hematinsite,
your file was not found therefore can not try the code below.
maybe try it, on a copy of your excel file.

VBA Code:
Sub ExportRangeToJPG()
 '--exports selected range to jpg file
 '  default filename is address of selected range
 '  based on code example posted at:
 '     http://www.emoticode.net/visual-basic/vba-export-excel-range-as-image-and-save-as-file.html
dim was as worksheet
for each ws in worksheets
 Dim vFilePath As Variant
 Dim sDefaultName As String
 
 If TypeName(Selection) <> "Range" Then
   MsgBox "Selection is not a range of cells."
   Exit Sub
 End If

 With Selection
   sDefaultName = Replace(.Address(0, 0, xlA1, 1), ":", "to") & ".jpg"
   vFilePath = Application.GetSaveAsFilename( _
    InitialFileName:=sDefaultName, _
    FileFilter:="JPEG File Interchange Format (*.jpg), *.jpg", _
    Title:="Save As")
    
 '--exit if cancelled by user,
 If vFilePath = False Then Exit Sub
   '--Make picture of selection and copy to clipboard
   .CopyPicture Appearance:=xlScreen, Format:=xlBitmap
   
   '--Create an empty chart with exact size of range copied
   With ws.ChartObjects.Add( _
        Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
     .Name = "TempChart"
     .Activate
   End With
 End With

 '--Paste into chart area, export to file, delete chart.
 ws.Paste
 With ws.ChartObjects("TempChart")
  .Chart.Export CStr(vFilePath)
  .Delete
 End With
    ws.Activate
    ActiveWindow.Zoom = 200
Next ws

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top