VBA give error on excel 2013

aliaslamy2k

Active Member
Joined
Sep 15, 2009
Messages
416
Office Version
  1. 2019
Platform
  1. Windows
Dear Experts,

I have below code which works perfectly fine in excel 2019 but it gives me an error when I run the same in excel 2013 on another laptop.
Error is "Error Occured while exporting. Try again". Please help if the code needs to be modified.



Sub Export_to_excel_ActualVSBudget()
On Error GoTo err



Dim xlApp As New Excel.Application
Dim xlWB As New Workbook

Set xlWB = xlApp.Workbooks.Add
'xlWB.Add
xlApp.Visible = False


ThisWorkbook.Activate
Range("A9").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select

Selection.Copy

xlApp.Visible = True
xlWB.Activate
xlWB.Sheets("Sheet1").Select
xlWB.Sheets("Sheet1").Range("B2").PasteSpecial Paste:=xlPasteValues
xlWB.Sheets("Sheet1").Cells.Select
xlWB.Sheets("Sheet1").Cells.EntireColumn.AutoFit
xlWB.Sheets("Sheet1").Range("A2").Select
Exit Sub
err:
MsgBox ("Error Occured while exporting. Try again")

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,
the error is a custom message not the actual error you are getting

untested but try this update to the code which if problem still persists should report the actual error.

Rich (BB code):
Sub Export_to_excel_ActualVSBudget()
    Dim xlApp       As New Excel.Application
    Dim xlWB        As New Workbook

   
    On Error GoTo myerror
   
    Application.ScreenUpdating = False
    Set xlWB = xlApp.Workbooks.Add
   
    With ThisWorkbook.Worksheets("Sheet1")
        .Cells(9, 1).Resize(.Cells(.Rows.Count, "A").End(xlUp).Row - 8, _
                            .Cells(9, .Columns.Count).End(xlToLeft).Column).Copy
    End With
   
    With xlWB.Sheets("Sheet1")
        .Range("B2").PasteSpecial Paste:=xlPasteValues
        .UsedRange.EntireColumn.AutoFit
    End With
   
myerror:
    With Application
        .CutCopyMode = False: .ScreenUpdating = True
    End With
    If err <> 0 Then MsgBox (Error(err)), 48, "Error"
End Sub


You will need to change the name of the sheet you are copying where shown in BOLD

Dave
 
Upvote 0
Hi Dave,

I am getting error when i run the macro "Subscript out of range"
 

Attachments

  • Excel error.jpg
    Excel error.jpg
    11.6 KB · Views: 4
Upvote 0
That indicates code cannot find the worksheet

change this line

VBA Code:
With xlWB.Sheets("Sheet1")

to this

VBA Code:
With xlWB.Sheets(1)

and did you change the name of the worksheet you are copying where shown in BOLD?
 
Upvote 0
Hi Dave,

I run the Macro, but nothing is happening, Below is the code i used.

Sub Export_to_excel_ActualVSBudget()
Dim xlApp As New Excel.Application
Dim xlWB As New Workbook


On Error GoTo myerror

Application.ScreenUpdating = False
Set xlWB = xlApp.Workbooks.Add

With ThisWorkbook.Worksheets("Agency")
.Cells(9, 1).Resize(.Cells(.Rows.Count, "A").End(xlUp).Row - 8, _
.Cells(9, .Columns.Count).End(xlToLeft).Column).Copy
End With

With xlWB.Sheets(1)
.Range("B2").PasteSpecial Paste:=xlPasteValues
.UsedRange.EntireColumn.AutoFit
End With

myerror:
With Application
.CutCopyMode = False: .ScreenUpdating = True
End With
If err <> 0 Then MsgBox (Error(err)), 48, "Error"
End Sub
 
Upvote 0
sorry, without thinking I deleted a line code

place line of code where shown in BOLD & see if resolves.

Rich (BB code):
With xlWB.Sheets(1)
        .Range("B2").PasteSpecial Paste:=xlPasteValues
        .UsedRange.EntireColumn.AutoFit
    End With
    
    xlApp.Visible = True

Dave
 
Upvote 0

Forum statistics

Threads
1,215,029
Messages
6,122,760
Members
449,095
Latest member
m_smith_solihull

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