Results 1 to 15 of 15

Thread: Procedure Too Large - Compile Error

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    You should learn how to use loops in your code
    Notice that your worksheet change code has the exactly the same code for B9 ,C9 and then B10 ,C10
    A reference to B9 can be made numerically Cells(“B9”) is equivalent to Cells(9,2), where the 9 is the row number and 2 is the column number. ( for some silly reason excel vba reversed the order of columns and rows in the two reference systems. Or you can reference by use of code such as
    Range("B" & i) where I is an index value.

    So the whole of the worksheet change code could be changed to:

    Private Sub Worksheet_Change(ByVal Target As Range)
        For i = 9 To 43
        If Not Intersect(Target, Range("B" & i)) Is Nothing Or Not Intersect(Target, Range("C" & i)) Is Nothing Then
                param1 = "B" & i
                param2 = "C" & i
                Call StudentAssign(Target, param1, param2) 'Student i
        End If
        Next i
        End Sub
    looking further into your code all of the code in your studentassign function could be simplified because there is a clear pattern in the case statement, so you can get rid of the case statement entirely by passing the index value across to the function instead of the B9 ,C9 references so:
    Case "B9"             intws1 = 6 
                intws2 = 7 
                intBcell = 1 
                strCcell = "S1" 
                strAcell = "A70"
    etc , etc down to end case is replaced with

    intws1=i-3
    intws2=i-2
    intBcell=i-8
    strCcell=”S” & i-8
    StrAcell= “A” & i+61
    where "i" is the index from the worksheet change event

    note:I have written code which is 10 times larger than yours without running into any size problems so that is not the reason it wouldn't compile in my opinion.
    Last edited by offthelip; 08-22-2017 at 02:03 AM.

Posting Permissions

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