What is wrong with this code? VBA - 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.

Related

Excel VBA Next without For error

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?

VBA referencing next cell in For loop

I need to compare a cell to the one below it in a loop. i know for most languages you can say something like "if cells(i,1).value = cells(i+1,1).value then..."
is there a way to do this in vba for some reason it isn't working for me. thanks
For i = 7 To ltrw
If (Cells(i, 1).Value = 0 And Cells(i + 1, 1).Value = 0 Then
Cells(i, 1).EntireRow.Hidden = True
End If
Next i
like this ?
For i = 7 To ltrw
If Cells(i).Value = Cells(i + 1).Value Then ' you can skip the ", 1" as its optional
Cells(i).EntireRow.Hidden = True
End If
Next i

Loop Crashing Excel VBA

i have been having problems with getting my code to run through its conditional loop and stopping. Here is what I have:
Do While True
Dim i As Integer
i = 2
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
Range(Cells(i, 1), Cells(i, 22)).Copy
Range(Cells(i, 1), Cells(i, 22)).PasteSpecial
i = i + 1
Else
Exit Do
End If
Loop
What I'm trying to do is to get the program to check if one cells isn't empty and if another doesn't have an error in it, if that condition is met, then the program would copy a certain row and re-paste it as just it's values since some of the cells in the row is a formula. For some reason the loop doesn't exit and Excel crashes, am I missing something?
the i = 2 should be outside
Dim i As Integer
i = 2
Do While True
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
Range(Cells(i, 1), Cells(i, 22)).Copy
Range(Cells(i, 1), Cells(i, 22)).PasteSpecial
i = i + 1
Else
Exit Do
End If
Loop
Two points :
The i = 2 must be outside the while loop.
Don't use Copy and PasteSpecial. Using the clipboard will give lots of problems later on. Additionally PasteSpecial likes you to be specific with "what" PasteSpecial action you're using. Rather assign values directly.
Dim i As Integer, Dataset As Variant
i = 2
Do While True
If Cells(i, 1).Value <> "" And Not IsError(Cells(i, 2).Value) Then
'This seems silly, but it overwrites the cell with its value.
Dataset = Range(Cells(i, 1), Cells(i, 22)).Value
Range(Cells(i, 1), Cells(i, 22)).Value = Dataset
i = i + 1
Else
Exit Do
End If
Loop

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)...