Home » excel » excel – VBA Compile Error: Procedure too long

excel – VBA Compile Error: Procedure too long

Posted by: admin May 14, 2020 Leave a comment

Questions:

How could I shorten this code? Forgive me for bad coding practices I’m self taught and pretty much a noob.

The code is comprised of 199 “Elseif” statements, I don’t know any other way to write what i’m trying to do.

Sub CopytoRoutine()


If Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B9") And Sheets("iPhone     view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C9:D9").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=FalseL

ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B10") And     Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C10:D10").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False

ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B11") And     Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C11:D11").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False

ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B12") And Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C12:D12").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False

ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B13") And    Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C13:D13").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False

ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B14") And Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C14:D14").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False

ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B15") And    Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7") Then
Range("A5:B5").Select
Selection.Copy
Sheets("Routine").Select
Range("C15:D15").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False

Sheets(“Routine”).Range(“Bx”) Where x= 9-29

Sheets(“Routine”).Range(“C7”), this range changes to G7,K7,O7,S7,…AM7. when the above gets to B29

The column letter for Range(“C9:D9”).Select changes respectively with the above, so do the row numbers

So I could essentially do something like this?

Sub CopytoRoutine()

Dim wb As Workbook
Dim cpuview As Worksheet
Dim routine As Worksheet

Set wb = ThisWorkbook
Set cpuview = wb.Sheets("iPhone view")
Set routine = wb.Sheets("Routine")

Dim x As Integer

For x = 9 To 29




If cpuview.Range("A2") = routine.Range("Bx") And cpuview.Range("A3") = routine.Range("C7")     Then
Range("A5:B5").Select
Selection.Copy
routine.Select
Range("Cx:Dx").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    True, Transpose:=False
How to&Answers:

There are several things you can do. First of all, it looks like you’re checking the following in every one of your if statements:

And Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7")

You only need to run that test once until “C7” changes to “D7”. You can do that like this, although we’ll optimize it even more in a little bit:

If Sheets("iPhone view").Range("A3") = Sheets("Routine").Range("C7")
    If Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B9") Then
      ...etc
End If

You can also replace your sheets with worksheet variables, which will reduce the amount of typing you have to do (also reducing the length of your code).

For example:

    Dim wb As Workbook
    Dim iphone As Worksheet
    Dim routine As Worksheet

    Set wb = ThisWorkbook
    Set iphone = wb.Sheets("iPhone view")
    Set routine = wb.Sheets("Routine")

    ' now we don't have to specify the sheet each time, so instead of this:
    ElseIf Sheets("iPhone view").Range("A2") = Sheets("Routine").Range("B10") Then

    ' we can use this shorter version:
    If iphone.Range("A2") = routine.Range("B10") Then

But by far the biggest bang for your buck will be replacing your if statements with a loop using a variable.

As you point out in your comment, “Where x=9-29” can be written like this:

Dim myRow As Integer

For myRow = 9 To 29
    ' do stuff, replacing "x" with myRow
Next myRow

Putting it all together, you can nest two loops, one for the column that increases, and the other for the range of rows that you are processing for each column.

Try this:

Sub ReplaceElseIfWithLoops()
    Dim wb As Workbook
    Dim iphone As Worksheet
    Dim routine As Worksheet
    Dim myRow As Integer
    Dim myCol As Integer

    Set wb = ThisWorkbook
    Set iphone = wb.Sheets("iPhone view")
    Set routine = wb.Sheets("Routine")

    For myCol = 3 To 39 ' column C = 3 and AM = 39
        If iphone.Range("A3") = routine.Cells(7, myCol) Then
            For myRow = 9 To 29
                If iphone.Range("A2") = routine.Range("B" & myRow) Then
                    routine.Range(Cells(myRow, myCol), Cells(myRow, myCol + 1)).Value = iphone.Range("A5:B5").Value 
                End If
            Next myRow

        End If
    Next myCol

End Sub

Note that you don’t have to select the target sheet before pasting. In fact, selecting and activating things in your VBA code is almost always a really bad practice.