Jeremie Tomkins
Jeremie Tomkins

Reputation: 31

VBA for if -then- else statement

Below is code that I came up which copies values from cell Q13, and pastes them into column E until the list is complete (Q13 is updated with a new value each time). I have a condition which says if Q13 reads "45" copy that value and paste it into the p column. I know this an if -then- else statment, but I get a couple different compile errors (block if without end if, and if without block if) and realize it's most likely my syntax. If someone could review this that would be fantastic.

Sub Test()
Dim c As Range
Dim ws As Worksheet: Set ws = Sheet2
Dim range1 As Range: Set range1 = ws.Range("Q13")
Dim Cell As Range

For Each Cell In range1
If Cell.Value = "45" Then
    NextRow = ws.Cells(ws.Rows.Count, "P").End(xlUp).Offset(1, 0).Row
    Cell.Copy ws.Range("P" & NextRow)
    Else
 For Each c In Range("B2:B" & Range("B65536").End(xlUp).Row)
Dim R As Range
Set R = Worksheets("Sheet2").Cells(Rows.Count, "E").End(xlUp) 'last cell in Column E with data
 If Len(R.Value) > 0 Then Set R = R.Offset(1)
R.Value = Worksheets("Sheet2").Range("Q13").Value
Exit For
End If
Next ws
End If

Upvotes: 0

Views: 158

Answers (1)

Mathieu Guindon
Mathieu Guindon

Reputation: 71207

Don't put a loop variable after the Next keyword, it's useless. The only thing it can do, is make VBA throw a compiler error if you use the wrong loop variable - as is the case here. If there's so much code in the a loop body that you no longer know where you're at by the time you reach the bottom, then the loop body is doing too many things. The solution is to pull code from the loop body into smaller, more specialized procedures.

Use an indenter if you aren't sure how to properly indent your code: it makes this kind of syntax error blatantly obvious:

Dim c As Range
Dim ws As Worksheet: Set ws = Sheet2
Dim range1 As Range: Set range1 = ws.Range("Q13")
Dim Cell As Range

For Each Cell In range1
|   If Cell.Value = "45" Then
|   |   NextRow = ws.Cells(ws.Rows.Count, "P").End(xlUp).Offset(1, 0).Row
|   |   Cell.Copy ws.Range("P" & NextRow)
|   Else
|   |   For Each c In Range("B2:B" & Range("B65536").End(xlUp).Row)
|   |   |   Dim R As Range
|   \   |   Set R = Worksheets("Sheet2").Cells(Rows.Count, "E").End(xlUp) 'last cell in Column E with data
\   |\  /   If Len(R.Value) > 0 Then Set R = R.Offset(1)
 \  | \/    R.Value = Worksheets("Sheet2").Range("Q13").Value
  \ / /\    Exit For
   /\/  End If
  / Next
End If

The If...Else...End If block is terminated with a Next; the inner For Each...Next loop is terminated with an End If, and the outer For Each...Next loop is terminated with End If - you need to de-tangle this.

Dim c As Range
Dim ws As Worksheet: Set ws = Sheet2
Dim range1 As Range: Set range1 = ws.Range("Q13")
Dim Cell As Range

For Each Cell In range1
    If Cell.Value = "45" Then
        NextRow = ws.Cells(ws.Rows.Count, "P").End(xlUp).Offset(1, 0).Row
        Cell.Copy ws.Range("P" & NextRow)
    Else
        For Each c In Range("B2:B" & Range("B65536").End(xlUp).Row)
            Dim R As Range
            Set R = Worksheets("Sheet2").Cells(Rows.Count, "E").End(xlUp) 'last cell in Column E with data
            If Len(R.Value) > 0 Then Set R = R.Offset(1)
            R.Value = Worksheets("Sheet2").Range("Q13").Value
            Exit For
        Next
    End If
Next

Upvotes: 1

Related Questions