Skipping an iteration in for loop (VBA excel) - vba

I have a piece of code in which i would like the for loop to continue to the next value of i if the value in a cell is "nil" and not execute the commands in the loop. I am unable to figure it out.
For i = 2 To n
If .Cells(i, "G").Value = "nil" Then
next i
Else
mon = month(.Cells(i, "G").Value)
acctyp = .Cells(i, "P").Value
end if
next i
Thanks in advance.

The comparison operator <> means "not equal to":
For i = 2 To n
If .Cells(i, "G") <> "nil" Then
mon = Month(.Cells(i, "G"))
acctyp = .Cells(i, "P")
End If
Next i

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.

Why is my Excel VBA macro crashing?

So I made a Excel macro and when i try to run it, it keeps crashing. Here it is:
result = "fail"
j = 4
Do While result = "fail" Or Cells(2, j) <> " "
If Cells(2, j).Value >= 15 Then
result = "pass"
Else
j = j + 1
End If
Loop
Your Do While loop Do While result = "fail" Or Cells(2, j) <> " " will run if either result = "fail" or Cells(2, j) <> " ".
I think you meant to exit the loop once you reach an empty cell, or you get result = "pass". So, you need to change your Or to And:
Do While result = "fail" And Cells(2, j) <> " "
If you want to exit in case there are only empty space in the cell, add also Trim.
Do While result = "fail" And Trim(Cells(2, j)) <> ""
changed or to and.
the loop needs to stop somewhere, currently it is going into an endless loop.
pls try below code and let me know if this is what you wanted or I have understood your requirement incorrectly.
Sub tester()
result = "fail"
j = 4
Do While result = "fail" And Cells(2, j) <> ""
If Cells(2, j).Value >= 15 Then
result = "pass"
Else
j = j + 1
End If
Loop
End Sub

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