Excel VBA Next without For error - vba

I am getting a "Next Without for Error on the following code:
sub test()
numRows = 11
For i = 0 To numRows
If Cells(i + 1, 2) >= 0 Then
Range((Cells(A, i + 1)), Cells(B, i + 1)).Select
Selection.Copy
Sheets("PasteLocation").Activate
Range("Ai+1").Select
Selection.Paste
Next
End Sub
I assume that my "If" statement doesnt know that it's done and that the "Next" command thinks there should be another nested "for" in the "if" statement but I do not know where.

As commented, you need End IF, but I also made some slight improvements:
Sub test()
numRows = 11
For i = 0 To numRows
If Cells(i + 1, 2) >= 0 Then
With Sheets("Sheet1")
.Range(.Cells(1, i + 1), .Cells(2, i + 1)).Copy
End With
Sheets("PasteLocation").Range("A" & i + 1).Paste
End If
Next
End Sub
Note: Change Sheets("Sheet1") to the appropriate sheet name.

The answer is in the first comment under my original question by user Johnny Mopp
You should close your If with End If. If then Else Excel VBA - "End If" needed?

Related

What is wrong with this code? VBA

The value of the cell is always "error" no matter if the condition is true or not. I've tried using else but it doesn't work either.
Sub ejer_4()
Cells(3, 1).Value = "hola"
For i = 2 To 21:
If Int(Cells(i, 3).Value) <> Int(Cells(i + 1, 3).Value) - 1 Then
Cells(3, 1).Value = "Error"
End If
Next
End Sub
I think that should solve your problem:
Sub ejer_4()
For i = 2 To 21:
If Int(Cells(i, 3).Value) <> Int(Cells(i + 1, 3).Value) - 1 Then
Cells(i, 1).Value = "Error"
Else
Cells(i, 1).Value = "hola"
End If
Next
End Sub
You problem is that you loop thorugh range of cells, but after checking condition you write into one cell. So, this cell will contain oonly the result of the last check. Previous values will be simply overwritten.

Never ending VBA, neither getting the result. Please tell me the error in this code

Option Explicit
Sub dowhileloop()
Dim x As Long
Dim sumx As Long
Sheets("test data").Select
x = 2
Do While Cells(x, 2) <> " "
DoEvents
sumx = Cells(x, 5).Value
If Cells(x, 2) = Cells(x + 1, 2) And Cells(x, 4) = Cells(x + 1, 4) Then
sumx = sumx + Cells(x + 1, 5).Value
Range("F2:G100").Select
Else
End If
Loop
End Sub
Never ending VBA, neither getting the result. Please tell me the error in this code.
I am guessing that you are looking for something like this:
Option Explicit
Sub dowhileloop()
Dim x As Long
Dim sumX As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("test data")
For x = 2 To ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
sumX = ws.Cells(x, 5).Value2
If ws.Cells(x, 2) = ws.Cells(x + 1, 2) And ws.Cells(x, 4) = ws.Cells(x + 1, 4) Then
sumX = sumX + ws.Cells(x + 1, 5).Value
Else
' ...do something else?
End If
Next x
ws.Activate
ws.Range("F2:G100").Select
End Sub
Changes:
There is no need to select anything just to manipulate the data on that sheet. It is especially not necessary to select something multiple times in a Loop. But if you want to make sure that a certain range is selected at the end of your program then you might as well put it to the end of your code.
I changed the Do while ... Loop for a For ... Next loop. This loop is definite while the Do while ... Loop has a tendency to get infinite if you are not careful. Hence, I always try to avoid them.
The code is now more explicit and fully qualified using Set ws = ... in the beginning and then using ws. in all the Cells and Range refernces afterwards.
The line DoEvents does not strike me as being necessary in this context. Hence, I removed it.
The rest is pretty much the same. Still, if you have questions then don't hesitate to ask.
You're not incrementing x - so it never changes.
Two things:
a) Space you need to remove in - Do While Cells(x, 2) <> ""
B) increment x = x + 1

call function to each cell in a range (involve insert rows, so the range will change) in Excel VBA

I'm learning VBA and have some problems
What I have is a list of date:
picture1
What I want to do is add 3 meals for everyday like this
picture2
I have recorded a macro which can achieve this:
Sub InsertMeal()
ActiveCell.EntireRow.Insert
ActiveCell.EntireRow.Insert
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "Breakfast"
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "Lunch"
ActiveCell.Offset(1, 0).Select
ActiveCell.FormulaR1C1 = "Dinner"
ActiveCell.Offset(-2, -2).Range("A1:A3").Select
Selection.Merge
End Sub
Now I want write a macro, which apply this InsertMeal() function to each cells in selected range.
This is what I wrote
Sub ApplyToAll()
For Each c In ActiveCell.CurrentRegion.Cells
Call InsertMeal
ActiveCell.Offset(1, 0).Select
Next
End Sub
The problem is, since I insert rows every time, the For loop doesn't work well. The loop never end. Now I don't know how to "keep the range" to make the loop work.
Please help if you know how to do this. Thank you, really appreciate.
There really is no need for two functions here. To amend your loop to do what you need, b/c you are adding rows, you will need to nest a small loop inside the other loop that works in you block of 3 ,Breakfast ,Lunch, and Dinner.
The code would look something like this, but you will have to amend the range to suit your purpose. For example
Sub dave()
Dim i As Long
Dim lastrow As Long
Dim j As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row * 3
x = Array("Breakfast", "Lunch", "Dinner")
For i = 1 To lastrow
Cells(i + 1, 1).Resize(2).EntireRow.Insert
For j = 1 To 3
Cells(i + j - 1, 3).Value = x(j - 1)
Next j
Cells(i, 1).Resize(3).Merge: i = i + 2
Next i
End Sub
BTW, the lastrow will need to be multiplied by 3 as you are addeding rows, so the original lastrow will not reflect the actuall last row when finnished.
Sub RelativeFunc()
col = ActiveCell.Column
lastrow = Cells(Rows.Count, col).End(xlUp).Row
firstrow = Cells(1, col).End(xlDown).Row
rownum = lastrow - firstrow + 1
frownum = rownum * 3
x = Array("Breakfast", "Lunch", "Dinner")
For i = 1 To frownum
Cells(i + firstrow, col).Resize(2).EntireRow.Insert
For j = 1 To 3
Cells(firstrow + i - 1 + j - 1, col + 1).Value = x(j - 1)
Next j
Cells(firstrow + i - 1, col).Resize(3).Merge
i = i + 2
Next i
End Sub

Compile error: Next without For || VBA

I have a problem with my code, an error appears, and I don;t understand why. The error is:
"Compile error: Next without For"
I do not understand why it is like that. I am new to coding so any help and comments are more than welcome.
This is the code, the Next which is pointed as the one without For is provided a comment.
Sub CGT_Cost()
startrow = Worksheets("GUTS").Cells(10, 1) 'Here I put 1
endrow = Worksheets("GUTS").Cells(11, 1) 'Here I put 1000
For x = endrow To startrow Step -1
If Cells(x, "Q").Value = "Sale" Then
If Cells(x, "D").Value = "1" Then
For i = 1 To 1000
If Cells(x - i, "R").Value <> "1" Then
Next i
Else
Range("G" & x).FormulaR1C1 = "=R[-" & i & "]C/R[-" & i & "]C[-1]*RC[-1]"
End If
End If
End If
Next x
End Sub
Thank you all in advance,
with best regards,
Artur.
Every For statement with a body must have a matching Next, and every If-Then statement with a body must have a matching End If.
Example:
For i = 1 To 10 '<---- This is the header
Hello(i) = "Blah" '<---- This is the body
Next i '<---- This is the closing statement
You have part of the body of your If statement inside your For i loop, and part of it outside. It has to be either ALL inside or ALL outside. Think through the logic and see what it is you want to do.
you have overlapping loops-perhaps
Sub CGT_Cost()
startrow = Worksheets("GUTS").Cells(10, 1) 'Here I put 1
endrow = Worksheets("GUTS").Cells(11, 1) 'Here I put 1000
For x = endrow To startrow Step -1
If Cells(x, "Q").Value = "Sale" Then
If Cells(x, "D").Value = "1" Then
For i = 1 To 1000
If Cells(x - i, "R").Value <> "1" Then
'
Else
Range("G" & x).FormulaR1C1 = "=R[-" & i & "]C/R[-" & i & "]C[-1]*RC[-1]"
End If
Next i
End If
End If
Next x
End Sub

I cannot make this code work, it is a "type mismatch error"

Is there anything wrong with this code. I need to look through a column and if the value is less than a reference value (it is a timer) than copy adjacent cell and put in "A8".
Thanks.
Sub GetData()
Dim i As Integer
For i = 4 To 31
If Cells(i, 38) < Cells(32, 5) Then
Cells(1, 8) = Cells(i, 39)
End If
Next i
End Sub
Or alternatively to all presented options add this additional If statement before your existing if:
If IsError(Cells(i, 39)) = False And IsError(Cells(32, 5))= False Then
you could try testing for a numeric value in the cell before testing for less than:
Sub GetData()
Dim i As Integer
For i = 4 To 31
if isnumeric(cells(i,38)) then
If Cells(i, 38) < Cells(32, 5) Then
Cells(1, 8) = Cells(i, 39)
' Exit For ' UN-COMMENT to exit loop
End If
else
msgbox "Cell '" & cells(i,38).address & "' may have an error",vbexclamation+vbokonly
end if
Next
End Sub
BTW, the comments above by David and Kazjaw are quite right, every iteration of the loop you would potentionally overwrite cell A8!
You could exit the loop as soon as the test returns true like this:
Exit for
To avoid the mismatch, try comparing to the cell's text:
If Cells(i,38).Text < Cells(32,5)...