Need edit on this VBA

Shinod

New Member
Joined
Jun 29, 2022
Messages
38
Office Version
  1. 2019
Platform
  1. Windows
I'm using this VBA to export a specific range to an Image file. But is asking where to save the file. Can someone help me to correct this VBA by setting a default file location?

And also don't know why this part is using "Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)".

Thanks in advance.

VBA Code:
Sub SaveAsJPG()
    Dim ChO As ChartObject, ExportName As String
    Dim CopyRange As Range
    Dim Pic As Picture
    Dim i As Long
   
        Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("G12:G33")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
       
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True


    With ActiveSheet
        Set CopyRange = Range("A1:H43")
        If Not CopyRange Is Nothing Then
            Application.ScreenUpdating = False
            ExportName = Application.GetSaveAsFilename(InitialFileName:=.Range("G3") & " " & .Range("C3"), fileFilter:="JPEG Files (*.jpg), *.jpg")
            If Not ExportName = "False" Then
                CopyRange.Copy
                .Pictures.Paste
                Set Pic = .Pictures(.Pictures.Count)
                Set ChO = .ChartObjects.Add(Left:=10, Top:=10, Width:=Pic.Width, Height:=Pic.Height)
                Application.CutCopyMode = False
                Do
                    DoEvents
                    Pic.Copy
                    DoEvents
                    ChO.Chart.Paste
                    DoEvents
                    i = i + 1
                Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)

                ChO.Chart.Export Filename:=ExportName, Filtername:="JPG"
                ChO.Delete
                Pic.Delete
            End If
            Application.ScreenUpdating = True
        End If
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try This... Will save to desktop automatically...

VBA Code:
Sub SaveAsJPG()
    Dim ChO As ChartObject, ExportName As String
    Dim CopyRange As Range
    Dim Pic As Picture
    Dim i As Long
     Dim Fold As String
    Dim Name As String
    Dim Path As String
    
   
        Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("G12:G33")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
       
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True




    
    Fold = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    Name = ActiveSheet.Range("G3").Value & " " & ActiveSheet.Range("C3").Value
    Path = Fold & Application.PathSeparator & Name & ".jpg"




    With ActiveSheet
        Set CopyRange = Range("A1:H43")
        If Not CopyRange Is Nothing Then
            Application.ScreenUpdating = False
            ExportName = Path
            If Not ExportName = "False" Then
                CopyRange.Copy
                .Pictures.Paste
                Set Pic = .Pictures(.Pictures.Count)
                Set ChO = .ChartObjects.Add(Left:=10, Top:=10, Width:=Pic.Width, Height:=Pic.Height)
                Application.CutCopyMode = False
                Do
                    DoEvents
                    Pic.Copy
                    DoEvents
                    ChO.Chart.Paste
                    DoEvents
                    i = i + 1
                Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)

                ChO.Chart.Export Filename:=ExportName, Filtername:="JPG"
                ChO.Delete
                Pic.Delete
            End If
            Application.ScreenUpdating = True
        End If
    End With
End Sub
 
Upvote 0
Solution
Thank you so much @Jimmypop. It is working. But it was a part of VBA. Could you suggest your edit here in the full VBA.

The error message is "Duplicate declaration in current scope"

VBA Code:
Sub PrintPaySlips()
    Dim rCl As Range
    Dim rRng As Range
    With Sheets("Master Sheet")
        Set rRng = .Range(.Cells(2, 1), .Cells(.Rows.Count, 1).End(xlUp))
        For Each rCl In rRng
            With Sheets("Payslips")
                .Cells(3, 7).Value = rCl.Value
        Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("G12:G33")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
        
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True

    
    Dim ChO As ChartObject, ExportName As String
    Dim CopyRange As Range
    Dim Pic As Picture
    Dim i As Long
     Dim Fold As String
    Dim Name As String
    Dim Path As String
    
   
        Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("G12:G33")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
       
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg
    Application.ScreenUpdating = True




    
    Fold = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    Name = ActiveSheet.Range("G3").Value & " " & ActiveSheet.Range("C3").Value
    Path = Fold & Application.PathSeparator & Name & ".jpg"




    With ActiveSheet
        Set CopyRange = Range("A1:H43")
        If Not CopyRange Is Nothing Then
            Application.ScreenUpdating = False
            ExportName = Path
            If Not ExportName = "False" Then
                CopyRange.Copy
                .Pictures.Paste
                Set Pic = .Pictures(.Pictures.Count)
                Set ChO = .ChartObjects.Add(Left:=10, Top:=10, Width:=Pic.Width, Height:=Pic.Height)
                Application.CutCopyMode = False
                Do
                    DoEvents
                    Pic.Copy
                    DoEvents
                    ChO.Chart.Paste
                    DoEvents
                    i = i + 1
                Loop Until (ChO.Chart.Shapes.Count > 0 Or i > 50)

                ChO.Chart.Export Filename:=ExportName, Filtername:="JPG"
                ChO.Delete
                Pic.Delete
            End If
            Application.ScreenUpdating = True
        End If
    End With
End Sub
 
Upvote 0
Will have a look when back at my pc tomorrow...
 
Upvote 0
In the meanwhile I think I see your problem...

On line

VBA Code:
Dim xRg As Range
    Application.ScreenUpdating = False
        For Each xRg In Range("G12:G33")
            If xRg.Value = "0" Then
                xRg.EntireRow.Hidden = True
       
            Else
                xRg.EntireRow.Hidden = False
            End If
        Next xRg

Remove

VBA Code:
Dim xRg As Range

Seeing as this has already been declared previously in code...
 
Upvote 0
In reality, this macro creates payslips in picture format. It is now functional. nonetheless, after producing a few payslips, certain inaccuracies appeared.

Will, you be kind enough to fix the issues.?

The macro is aasigned here in the (Create all Payslips)

1656568335391.png


 
Upvote 0
Hi Shinod

I am quite busy and would not be bale to get to everything. Where and on what line are you getting the Expected End With error message?
 
Upvote 0
It is not working to create all the payslips. Only a few are created by this.
 
Upvote 0
Will check as soon as available again. In the mean time if someone else wants to take a stab at this they more than welcome...
 
Upvote 0

Forum statistics

Threads
1,214,849
Messages
6,121,922
Members
449,056
Latest member
denissimo

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