+ Reply to Thread
Results 1 to 5 of 5

Compile error: sub or function not defined

Hybrid View

Anita7 Compile error: sub or... 08-16-2012, 09:43 PM
Cutter Re: Compile error: sub or... 08-16-2012, 10:48 PM
arlu1201 Re: Compile error: sub or... 08-17-2012, 03:42 AM
royUK Re: Compile error: sub or... 08-17-2012, 06:10 AM
Anita7 Re: Compile error: sub or... 08-17-2012, 08:50 PM
  1. #1
    Registered User
    Join Date
    07-30-2012
    Location
    Tirane, Albania
    MS-Off Ver
    Excel 2010
    Posts
    13

    Post Compile error: sub or function not defined

    Hi,

    I am trying to solve one classic problem. Say, there are some excel sheets available in one workbook. All the excel sheets are of same format (same column name but differnt data and number of rows). I want to merge these sheets into one. I'm using a macro but when I try to run it shows the Compile error: Sub or Function not defined and the second statement (Sub CopyData....) highlighted in yellow.


    Option Explicit
    
    Sub CopyDataWithoutHeaders()
        Dim sh As Worksheet
        Dim DestSh As Worksheet
        Dim Last As Long
        Dim shLast As Long
        Dim CopyRng As Range
        Dim StartRow As Long
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'Delete the sheet "RDBMergeSheet" if it exist
        Application.DisplayAlerts = False
        On Error Resume Next
        ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    
        'Add a worksheet with the name "RDBMergeSheet"
        Set DestSh = ActiveWorkbook.Worksheets.Add
        DestSh.Name = "RDBMergeSheet"
    
        'Fill in the start row
        StartRow = 2
    
        'loop through all worksheets and copy the data to the DestSh
        For Each sh In ActiveWorkbook.Worksheets
            If sh.Name <> DestSh.Name Then
    
                'Find the last row with data on the DestSh and sh
                Last = LastRow(DestSh)
                shLast = LastRow(sh)
    
                'If sh is not empty and if the last row >= StartRow copy the CopyRng
                If shLast > 0 And shLast >= StartRow Then
    
                    'Set the range that you want to copy
                    Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
    
                    'Test if there enough rows in the DestSh to copy all the data
                    If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                        MsgBox "There are not enough rows in the Destsh"
                        GoTo ExitTheSub
                    End If
    
                    'This example copies values/formats, if you only want to copy the
                    'values or want to copy everything look below example 1 on this page
                    CopyRng.Copy DestSh.Cells(Last + 1, "A")
    
                End If
    
            End If
        Next
    
    ExitTheSub:
    
        Application.Goto DestSh.Cells(1)
    
        'AutoFit the column width in the DestSh sheet
        DestSh.Columns.AutoFit
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    Last edited by arlu1201; 08-17-2012 at 03:39 AM. Reason: Added code tags

  2. #2
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Compile error: sub or function not defined

    @ Anita7

    Welcome to the forum.

    Please notice that code tags have been added to your post instead of the quote tags. The forum rules require them so please keep that in mind and add them yourself whenever showing code in any of your future posts. To see instructions for applying them, click on the Forum Rules button at top of the page and read Rule #3.
    Thanks.

    And your post has been moved from the Introduction area so that it will be seen by those who can offer assistance.

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Compile error: sub or function not defined

    The lastrow function was not defined anywhere, hence the issue. This is your updated code -
    Sub CopyDataWithoutHeaders()
        Dim sh As Worksheet
        Dim DestSh As Worksheet
        Dim Last As Long
        Dim shLast As Long
        Dim CopyRng As Range
        Dim StartRow As Long
    
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With
    
        'Delete the sheet "RDBMergeSheet" if it exist
        Application.DisplayAlerts = False
        On Error Resume Next
        ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
        On Error GoTo 0
        Application.DisplayAlerts = True
    
        'Add a worksheet with the name "RDBMergeSheet"
        Set DestSh = ActiveWorkbook.Worksheets.Add
        DestSh.Name = "RDBMergeSheet"
    
        'Fill in the start row
        StartRow = 2
    
        'loop through all worksheets and copy the data to the DestSh
        For Each sh In ActiveWorkbook.Worksheets
            If sh.Name <> DestSh.Name Then
    
                'Find the last row with data on the DestSh and sh
                Last = Worksheets(DestSh.Name).Range("A" & Rows.Count).End(xlUp).Row
                shLast = sh.Range("A" & Rows.Count).End(xlUp).Row
    
                'If sh is not empty and if the last row >= StartRow copy the CopyRng
                If shLast > 0 And shLast >= StartRow Then
    
                    'Set the range that you want to copy
                    Set CopyRng = sh.Range(sh.Rows(StartRow), sh.Rows(shLast))
    
                    'Test if there enough rows in the DestSh to copy all the data
                    If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
                        MsgBox "There are not enough rows in the Destsh"
                        GoTo ExitTheSub
                    End If
    
                    'This example copies values/formats, if you only want to copy the
                    'values or want to copy everything look below example 1 on this page
                    CopyRng.Copy DestSh.Cells(Last + 1, "A")
    
                End If
    
            End If
        Next
    
    ExitTheSub:
    
        Application.Goto DestSh.Cells(1)
    
        'AutoFit the column width in the DestSh sheet
        DestSh.Columns.AutoFit
    
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Compile error: sub or function not defined

    The Function will be available where you got the code from i.e. Ron de Bruin's site
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    07-30-2012
    Location
    Tirane, Albania
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Compile error: sub or function not defined

    Thank you Arlete )

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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