+ Reply to Thread
Results 1 to 12 of 12

Select Method of Range Class Failed

Hybrid View

  1. #1
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Exclamation Select Method of Range Class Failed

    Hi,

    I'm trying to write a code such that when the user presses the button GRAPH, it graphs the data in the file for each column
    Capture.PNG

    I run into a debug error on this line:
    Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
    Full code:
    Private Sub cmdGraph_Click()
    
        gFolderpath.Text = chkLastChar(gFolderpath.Text)
        ChDir gFolderpath.Text
        Workbooks.Open Filename:=gFolderpath.Text & gFilename
        
        
        Dim nLeft As Long, nTop As Long
        Dim strChrt As String
        
        LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
            
        nLeft = 20: nTop = 0
            
        For NCol = 1 To LastCol
                
            Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
            ActiveSheet.Shapes.AddChart2(227, xlLine).Select
            ActiveChart.SetSourceData Source:=Range(Cells(1, NCol), Cells(LastRow, NCol))
            
            strChrt = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
                
            ActiveSheet.Shapes(strChrt).Left = nLeft
            ActiveSheet.Shapes(strChrt).Top = nTop
                
            '~~> Increment the next `Top` placement for the chart
            nTop = nTop + ActiveSheet.Shapes(strChrt).Height + 20
                
        Next NCol
        
    End Sub
    It's weird because I wrote this code before and it worked before. Now I'm trying to modify it responds through a button on a form so that it is more user friendly.
    This is my old code that worked. I can't seem to see the difference.

    Sub MergeWorkbooks_V2()
    
        Dim NCol As Long
        Dim NRow As Long
        Dim LastRow As Long
        Dim LastCol As Long
        Dim NSheets As Long
        Dim LastSheet As Integer
        
        ' Modify this folder path to point to the files you want to use.
        FolderPath = "C:\Users\VCHU\Desktop\Sample_LTE_0021_3-1261\"
        FileName = Dir(FolderPath & "Sheet1.xlsx")
    
        ' Call Dir the first time, pointing it to all Excel files in the folder path.
        FileName = Dir(FolderPath & "Sheet1.xlsx")
        
        ' Open a workbook in the folder
        Set WorkBk = Workbooks.Open(FolderPath & FileName)
        
        ' Number of sheets
        LastSheet = 2
        
        Dim nLeft As Long, nTop As Long
        Dim strChrt As String
        
        For NSheets = 1 To LastSheet
            Sheets(NSheets).Select
            LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
            LastCol = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
            
            nLeft = 20: nTop = 0
            
            For NCol = 1 To LastCol
                
                Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
                ActiveSheet.Shapes.AddChart2(227, xlLine).Select
                ActiveChart.SetSourceData Source:=Range(Cells(1, NCol), Cells(LastRow, NCol))
            
                strChrt = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
                
                ActiveSheet.Shapes(strChrt).Left = nLeft
                ActiveSheet.Shapes(strChrt).Top = nTop
                
                '~~> Increment the next `Top` placement for the chart
                nTop = nTop + ActiveSheet.Shapes(strChrt).Height + 20
                
            Next NCol
        
        Next NSheets
        
    End Sub
    DataProcessing_Tool.xlsm
    Last edited by lalaluye; 09-15-2015 at 04:31 PM.

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

    Re: Select Method of Range Class Failed

    Do you need to select the range?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Re: Select Method of Range Class Failed

    I need to select a range to graph it right? When I edit the range out, it gives me an empty graph.

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

    Re: Select Method of Range Class Failed

    You specify the same range as the source for the chart a couple of lines down, here.
     ActiveChart.SetSourceData Source:=Range(Cells(1, NCol), Cells(LastRow, NCol))
    I would have thought that was all you would need.

  5. #5
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Re: Select Method of Range Class Failed

    I tried removing that range select line, but it gave me an empty chart.

  6. #6
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Re: Select Method of Range Class Failed

    You are right. We don't need to select the range.. I removed it from my old code and it still graphs.
    However, I am still not getting any graphs for my modified code to fit the userform. I get a debug error at strChrt = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, "")) indicating "The item with the specified name not found"

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

    Re: Select Method of Range Class Failed

    Any chance you could upload a sample workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  8. #8
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Re: Select Method of Range Class Failed

    Sure! I attached it to the first post. It is still work in progress. Here is the sample data I need to graph (individually by column)DC Merged.xlsx

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

    Re: Select Method of Range Class Failed

    I think I know what the problem is.

    When you refer to a range in a worksheet module without specifying explicity the sheet and workbook the range is from VBA uses the range on the worksheet who's module the code is in.

    In this case the code is looking at Sheet1 in DataProcessing_Tool.xlsm, not the active sheet of DC Merged.xls.

    Give this a try.
    Private Sub cmdGraph_Click()
    Dim wbData As Workbook
    Dim nLeft As Long, nTop As Long
    Dim strChrt As String
    
        gFolderpath.Text = chkLastChar(gFolderpath.Text)
        ChDir gFolderpath.Text
        Set wbData = Workbooks.Open(Filename:=gFolderpath.Text & gFilename.Text)
    
        With wbData.ActiveSheet
    
            LastRow = .Cells(Rows.Count, 1).End(xlUp).Row
            LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    
            nLeft = 20: nTop = 0
    
            For NCol = 1 To LastCol
                'Sheets(1).Select
                'Sheets(1).Range(Cells(1, NCol), Cells(LastRow, NCol)).Select
                .Shapes.AddChart(227, xlLine).Select
                .ChartObjects(NCol).Chart.SetSourceData Source:=.Range(.Cells(1, NCol), .Cells(LastRow, NCol))
    
                strChrt = Trim(Replace(.ChartObjects(NCol).Chart.Name, .Name, ""))
    
                .Shapes(strChrt).Left = nLeft
                .Shapes(strChrt).Top = nTop
    
                '~~> Increment the next `Top` placement for the chart
                nTop = nTop + .Shapes(strChrt).Height + 20
    
    
            Next NCol
        End With
        
    End Sub

  10. #10
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Re: Select Method of Range Class Failed

    I get a debug error here: .Shapes(strChrt).Left = nLesft
    The item with specified name wasn't found.

    I do see a graph now. Thank you very much. The next part (trying to place it in specified location) is a bit bugged.
    Last edited by lalaluye; 09-15-2015 at 05:03 PM.

  11. #11
    Registered User
    Join Date
    09-15-2015
    Location
    BC, Canada
    MS-Off Ver
    2013
    Posts
    7

    Re: Select Method of Range Class Failed

    It worked, then it doesn't work again when I pressed the GRAPH button... :S

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

    Re: Select Method of Range Class Failed

    You mean when you run it manually it works but it doesn't work when you click the GRAPH button?

    Strange, it works both ways for me with the sample file.

+ 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] Select method of range class failed
    By cjgraham92 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-19-2015, 07:49 PM
  2. Select method of range class failed
    By JayWeb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-17-2010, 09:41 PM
  3. Select Method of Range Class Failed
    By Screamtruth in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-07-2010, 07:01 AM
  4. Select method class range failed
    By phillb in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 11-05-2007, 06:45 AM
  5. Select Method of range class failed
    By excel_rookie74 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-27-2006, 12:17 PM
  6. Select method of range class failed
    By sa02000 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2005, 09:05 AM
  7. [SOLVED] What did I do? (Select Method of Range Class Failed )
    By HotRod in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-20-2005, 10:06 AM

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