Results 1 to 5 of 5

Compile error: sub or function not defined

Threaded View

  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

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