Results 1 to 12 of 12

Select Method of Range Class Failed

Threaded 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.

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