+ Reply to Thread
Results 1 to 3 of 3

"Method ‘Range’ of object ‘_Worksheet’ failed" error message in Excel/PowerPoint VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    "Method ‘Range’ of object ‘_Worksheet’ failed" error message in Excel/PowerPoint VBA

    Greetings,

    I've written a macro that creates PowerPoint charts from data in an Excel workbook.

    It is nearly functional, except for a
    Method ‘Range’ of object ‘_Worksheet’
    failed error message.

    The macro is run in a PowerPoint instance with no slides. It creates the correct PowerPoint chart with the data from the first Excel workbook sheet, but fails at this line
    pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range(Cells(1, 1), Cells(LastRow, LastColumn))
    when it gets to the second sheet in the Excel workbook. Specifically it says method
    'Cells' of object '_Global' failed'
    when I hover over the line in the VBA editor. Also,
    LastRow
    and
    LastColumn
    still refer to the 1st worksheets data range.

    This leads me to believe that my:
    LastRow = xlWS.Range("A1").SpecialCells(xlCellTypeLastCell).Row
                            
    LastColumn =xlWS.Range("A1").SpecialCells(xlCellTypeLastCell).Column
    References are not properly cycling through to the next sheet in the Excel workbook. This is weird because when I use a static range such as in
    pptWorkSheet.Range("A1:B5")
    , the macro goes through the sheets as it should.

    I greatly appreciate any help. My full macro is below:

        Sub CreateChartAllWKsv6()
         ''''' Cleaned (i.e. syntax) version of v5
         '''''Impt
         '''This macro can
         ''' 1.add charts of different types
         ''' 2. resize chart data range to fit worksheet data
         
            Dim myChart As Chart
            Dim pptChartData As ChartData
            Dim pptWorkBook As Excel.Workbook
            Dim pptWorkSheet As Excel.Worksheet
            Dim xlApp As Excel.Application
            Dim xlWB As Workbook
            Dim xlWS As Worksheet
            Dim CurSlide As Slide 'new from update
            Dim LastRow As Long ' 8/22
            Dim LastColumn As Long ' 8/22
            
        ' Create new excel instance and open relevant workbook
            Set xlApp = New Excel.Application
            xlApp.Visible = True 'Make Excel visable
            Set xlWB = xlApp.Workbooks.Open("C:\filepath\ExcelData_PPTChartIteration.xlsm", True, False)  'Open relevant workbook
            
        'Loop through each worksheet in xlWB and transfer data to new pptWorkBook and create new PowerPoint chart
                For Each xlWS In xlWB.Worksheets
        
                        'Add a new slide where we will create the PowerPoint worksheet and chart
                                Set CurSlide = ActivePresentation.Slides.Add(ActivePresentation.Slides.Count + 1, ppLayoutText) 'Slide types:
                                ActiveWindow.View.GotoSlide ActivePresentation.Slides.Count
                        ' Create the chart and set a reference to the chart data.
                                Set myChart = CurSlide.Shapes.AddChart(xlColumn).Chart 'Chart types: https://msdn.microsoft.com/en-us/library/office/ff837417.aspx
                                Set pptChartData = myChart.ChartData
                        ' Set the PowerPoint Workbook and Worksheet references.
                                Set pptWorkBook = pptChartData.Workbook
                                Set pptWorkSheet = pptWorkBook.Worksheets("Sheet1") 'From Update
                        'Find Last Row and Column of xlWS
                                LastRow = xlWS.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Row
                                LastColumn = xlWS.Cells(1, 1).SpecialCells(xlCellTypeLastCell).Column
                        ' Add the data to the PowerPoint workbook.
                                pptWorkSheet.ListObjects("Table1").Resize pptWorkSheet.Range(Cells(1, 1), Cells(LastRow, LastColumn)) '<~ Macro breaks down here on second sheet. Works perfect on 1st sheet
                                pptWorkSheet.Range("a2:z100").Value = xlWS.Range("a2:z100").Value '!!! IMPORTANT: FOR SOME REASON YOU CANNOT PUT A REFERENCE TO A2 OR THE CHART RANGE WILL RESIZE BACK TO THE DEFAULT. I SUSPECT THIS IT BECAUSE OF THE SERIES NAME
                        ' Apply styles to the chart.
                                With myChart
                                        .ChartStyle = 4
                                        .ApplyLayout 4
                                        .ClearToMatchStyle
                                End With
                        'Add Title
                                myChart.HasTitle = True
                        'Format title
                                With myChart.ChartTitle
                                    .Characters.Font.Size = 18
                                    .Text = "Test Chart"
                                End With
                        ' Add the axis title.
                                With myChart.Axes(xlValue)
                                        .HasTitle = True
                                        .AxisTitle.Text = "Units"
                                End With
                        'Apply data labels
                        myChart.ApplyDataLabels
                        'Close PowerPoint worksheet
                        myChart.ChartData.Workbook.Close
                        
                Next xlWS
        
        ' Clean up the references.
                Set pptWorkSheet = Nothing
        ' pptWorkBook.Application.Quit
                Set pptWorkBook = Nothing
                Set pptChartData = Nothing
                Set myChart = Nothing
        'Clean up Excel references.
                Set xlApp = Nothing
        'Option to close excel workbook
                xlWB.Close
        'Option to close the excel application
                'xlApp.Quit
        End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: "Method ‘Range’ of object ‘_Worksheet’ failed" error message in Excel/PowerPoint VBA

    You have no worksheet reference for Cells in the line causing the error.

    Try this.
    With pptWorkSheet
        .ListObjects("Table1").Resize .Range(.Cells(1, 1), .Cells(LastRow, LastColumn))
    End With
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    01-27-2014
    Location
    Fresno, USA
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: "Method ‘Range’ of object ‘_Worksheet’ failed" error message in Excel/PowerPoint VBA

    Thanks! Worked. Can't believe I missed that.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Runtime Error: 1004 Method 'Visible' of Object "_Worksheet' failed
    By Sorjas in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-15-2015, 02:29 AM
  2. [SOLVED] "Method 'Range' of object '_worksheet' failed" when a different sheet is active
    By CO# in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-10-2014, 11:48 AM
  3. XL03 Sort error: "Method 'Range' of object '_Worksheet' failed"
    By AdLoki in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2011, 08:23 AM
  4. "Method 'Range' of object '_Worksheet' failed" error in Windows Script Host
    By jikenj in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-16-2010, 02:18 PM
  5. [SOLVED] Error in Macro: "Method 'Paste' of object '_Worksheet' failed"
    By blork in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-05-2006, 01:50 PM
  6. What is Error "Method "Paste" of object "_Worksheet" failed?
    By vat in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-17-2006, 04:10 PM
  7. [SOLVED] "GoalSeek method of Range object failed" error message
    By Fixit_Steve in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2005, 04:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1