For Loop with If-Or Statement VBA - excel

I have hard-coded for ever two cells in an excel column to count if either the first or second have a value of 1. It worked this way (but also written out for every cell applicable so is inefficent):
If Range("D10").Value = 1 Or Range("D11").Value = 1 Then
x = x + 1
Else
x = x + 0
End If
I am trying to refactor it. My thought was to do a for loop, with an if and an or statement (similar to in my current code), but receive a syntax error. My attempt:
Sub refactor()
Dim x As Integer
x = 0
For Each c In Range("D2-D13").Cells
If c.Value =1 OR (c+1).Value = 1 Then
x = x + 1
Else
x = x + 0
End If
Range("A19") = x
End Sub
All I can find is for loops with if statements in them, but never the added "or" condition considered. VBA is new to me. Is there a way I can code this?

for loop in enough to run entire range itn't required "or c.offset(1,0).value = 1" because "or" operator used to any one condition is true it will allow to run the code.
"if c.Value = 1 and c.Offset(1, 0).Value = 1 Then" = this case is used for if both the statement is true then only if condition allow to run the code. are u looking for this?
Sub refactor()
Dim x As Integer
Dim c As Range
x = 0
For Each c In Range("D2:D13")
'if c.Value = 1 Or c.Offset(1, 0).Value = 1 Then'this line is same as below line
if c.value = 1 then
x = x + 1
End If
Next c
Range("A19") = x
End Sub

Related

i = empty how can i solve this?

Dim a As Integer
a = 0
For i = 4 To i + 1
Set xRange = Sheets("sayfa4").Rows(i)
For j = 1 To 10
If Sheets("sayfa4").Cells(i, j) > 0 And Sheets("sayfa4").Cells(i, j) = Application.WorksheetFunction.Min(xRange) Then
a = a + 1
Sheets("sayfa4").Cells(i, j).ClearContents
Sheets("sayfa4").Cells(15 + a, 1) = i
Sheets("sayfa4").Cells(15 + a, 2) = j
i = j - 1
End If
Next j
Next i
i don't get any errors or something i just can't execute it nothing happens on excel sheet can you help me guys what is the problem
when i try with F8 it shows "i = empty"
my main problem is declaring the next i actually
when i choose a cell(i,j) and printing the value of it
my next i should be j and after i should start to search min value of j row and it turns out next i
I tryed to understand your logic and rewrote the code:
Option Explicit
Sub test()
Dim a As Integer
Dim i As Long, j As Long
Dim xRange As Range
a = 0
i = 4
For i = 4 To i + 1
Set xRange = Sheets("sayfa4").Rows(i)
For j = 1 To 10
If Sheets("sayfa4").Cells(i, j) > 0 And Sheets("sayfa4").Cells(i, j) = Application.WorksheetFunction.Min(xRange) Then
a = a + 1
'Sheets("sayfa4").Cells(i, j).ClearContents
Sheets("sayfa4").Cells(15 + a, 1) = i
Sheets("sayfa4").Cells(15 + a, 2) = j
Exit For
'i = j - 1
End If
Next j
Next i
End Sub
If you are looking for the minimum value of a selection, this code should solve your issue
This statement is meaningless
For i = 4 To i + 1
this is because i is uninitialized or zero and so the loop is really For i=4 to 1 which will be ignored.
If you do not know how many times to iterate use a Do Loop instead. Only use For for fixed iterations and do not alter the iteration variable inside the loop.
If you want to stop the loop use Exit For, or Exit Do and if you want to end the function or subroutine processing use Exit Function or Exit Sub.
To skip to the next iteration in VBA you would use an If structure, since it does not have a continuation keyword like other languages have. (thanks #BigBen)

For Loops to perform a Step of 3 only after inserting a value into 6 rows of cells

The following code I have Will paste a value code of "01" to a cell and then skip 4 rows continuously, until reaching the end of count within the for loop. I want to run a similar loop for "02", but rather than "Step" (skip) 4 rows, I would like it to insert the value in 6 consecutive rows within the same column and then skip 3 rows continuously until reaching the end of count. I am 2 weeks new to vba, so I hope I am explaining this correctly.
Dim i As Long
If Sheet3.Range("C22").Value = "01" Then
For i = 3 To 202 Step 4
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = _
ActiveWorkbook.Sheets("MonData").Cells(22, 5).Value
Next i
ElseIf Sheet3.Range("C22").Value = "02" Then
For i = 3 To 152
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = _
ActiveWorkbook.Sheets("MonData").Cells(22, 5).Value
Next i
End If
Maybe like this:
Dim i As Long, v
v = ActiveWorkbook.Sheets("MonData").Cells(22, 5).Value
If Sheet3.Range("C22").Value = "01" Then
For i = 3 To 202 Step 4
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = v
Next i
ElseIf Sheet3.Range("C22").Value = "02" Then
For i = 3 To 152 Step 9 '6 filled + 3 empty = 9
ActiveWorkbook.Sheets("CrewEntries").Cells(i, 6).Value = v
Next i
End If
For such a kind of question, I would advise a while-loop, as in this piece of pseudo-code:
dim condition as boolean
dim loop_step, interesting_value as integer
condition = true
loop_step = 1 'just in order to be sure that it never is 0, this might create an infinite loop
interesting_value = 0 ' or some other initialisation value
while condition do
if <some_first_condition>
then
do_first_thing(interesting_value, ...)
loop_step = 3
else
do_second_thing(interesting_value, ...)
loop_step = 6
end if
interesting_value = interesting_value + loop_step
if <some_other_condition> then condition = false
Wend
Sub EarningCode()
Dim CpID As String
Dim i As Long
Dim p As Long
CpID = ActiveWorkbook.Sheets("MonData").Cells(22, 3).Value
For i = 3 To 452
If p = 9 Then
p = 1
Else
p = p + 1
End If
If p < 7 Then
ThisWorkbook.Worksheets("CrewEntries").Cells(i, 4).Value = "02"
End If
Next i
End Sub

Sum every odd row in a single Column VBA

I have a code that searches an excel fill for the word distance, takes the value of the cell next to it, paste it into a new cell, then sums all the cells up. Which works great, but i now need to find a way to only sum ever even row number. Does that make sense?
Sub Distance_Check()
Dim DistanceCheck As String
Dim DistanceNumber As String
Dim DistanceSum As String
Dim DistanceTotal As String
DistanceCheck = MsgBox("Would you like to check the distance?", vbYesNo)
If DistanceCheck = vbYes Then
If IsArray(fname) Then Workbooks.OpenText fname(1)
i = 1
findStr = "Distance"
Set foundCel = Range("A:A").Find(what:=findStr)
If Not foundCel Is Nothing Then
firstAddress = foundCel.Address
Do
Range("J" & i).Value = foundCel.Offset(0, 1).Value
Set foundCel = Range("A:A").FindNext(foundCel)
i = i + 1
Loop While Not foundCel Is Nothing And foundCel.Address <> firstAddress
End If
Set wkbSourceBook = ActiveWorkbook
DistanceNumber = i - 2
DistanceSum = WorksheetFunction.Sum(Range(Cells(2, 15), (Cells(DistanceNumber + 1, 15))))
DistanceTotal = DistanceSum / DistanceNumber
If DistanceNumber = Cells(2, 12) Then
MsgBox ("No error found wihin distance")
Else
MsgBox ("Error found with distance")
End If
Else
End If
Call Save_Data
End Sub
Would the way youd go about this be using a for loop on the
cells(DistanceNumber(j,+1)
Where j = 0,
j = j +2 ,
Until j > DistanceNumber,
Would that work? If so how would you go about it?
Thanks
A quick way to step through a loop in the desired increments is to use the Mod operator which divides two numbers and returns any remainder (e.g. 7 mod 2 = 1, as two sixes fit into seven, leaving one).
You can use the row property of the range you identify with the Find method, and since you want to jump by two the modulo should be zero:
If foundcel.Row Mod 2 = 0 Then Range("J" & i).value = foundcel.Offset(0, 1).Value
That said, there is a 'built in' way to step through a loop if using a For loop like this
For x = 2 to 10 Step 2
' Do stuff
Next x
You can also use this method to step backwards, e.g.
For x = 100 to 0 Step -10
' Do stuff backwards!
Next x

VBA error 1004 in do until loop

I'm pretty new to VBA, I've been succesfull before in programming loops, however, this time I can't seem to find the solution to make this code work.
The goal is to assemble data from one sheet (divided over 400+ seperate tables) to another sheet.
The program works without the outer loop, but when coding the loop around the other two loops I constantly get the error "run-time error '1004': Application-defined or object-defined error.
Dim x As String
Dim HStop As String
Dim y As String
Dim VStop As String
Dim z As Integer
Dim Row As Integer
Dim Column As Integer
Dim Counter As Integer
Sub Reform()
'1 maal te doen:
Row = 10
Column = 1
Counter = 6
'begin sub
Do
HStop = Worksheets("Find").Cells(2, 3).Value
VStop = Worksheets("Find").Cells(3, 3).Value
x = 1
y = 1
Do
Column = Column + 1
x = Worksheets("retour").Cells(Row, Column).Value
Loop Until x = HStop
'Checkpoint
Worksheets("Find").Cells(2, 12).Value = x
Do
Row = Row + 1
y = Worksheets("retour").Cells(Row + 7, 2).Value
Worksheets("Find").Cells(Counter, 12).Value = Worksheets("retour").Cells(Row, Column).Value
Counter = Counter + 1
Loop Until y = VStop
'Checkpoint
Worksheets("Find").Cells(3, 14).Value = y
Worksheets("Find").Cells(2, 14).Value = Row
Worksheets("Find").Cells(3, 14).Value = Column
z = z + 1
Loop Until z = 10
End Sub
Can someone help me?
Well, not sure how this code will help you to get desired result, however the error occurs because in the following peice of code
Do
HStop = Worksheets("Find").Cells(2, 3).Value
VStop = Worksheets("Find").Cells(3, 3).Value
x = 1
y = 1
Do
Column = Column + 1
x = Worksheets("retour").Cells(Row, Column).Value
Loop Until x = HStop
'Checkpoint
HStop is assigned a value, then in the loop you are assigning another value to x and looping till x = HStop. In Do loop you are also incrementing the value of Column. In case, x is not equal to HStop and Column value reaches 16385 i.e. one more than the last column of the sheet, it throws runtime error while assigning
x = Worksheets("retour").Cells(Row, 16385).Value

For loop and if statement to copy in another sheet

I am currently trying to check cells from 2 columns (one for loop for each) and see whether they have the string true. If yes I would like to copy some cells corresponding, to another sheet(log).
I know that I have some cells, which contain the word true but when I run the program there is nothing that is copied in my other sheet.
I do not get any compiling errors and would like to know where I am wrong in this code.
Sub isLimit()
Dim a As Long, b As Long, Lr As Long
x = 2
y = 2
Lr = Worksheets("Targets").Cells(Rows.Count, "A").End(xlUp).Row
For i = 8 To Lr
If (StrComp(Cells(i, 15).Text, "TRUE")) = 0 Then
Worksheets("Log").Range("B" & x) = "no"
x = x + 1
End If
Next i
For j = 8 To Lr
If (StrComp(Cells(j, 16).Text, "TRUE")) = 0 Then
Worksheets("Log").Range("B2") = Worksheets("Targets").Range("B1").Value
Worksheets("Log").Range("C" & y) = "yes"
y = y + 1
End If
Next j
End Sub
So far, I can see two problems that might causing you the trouble.
1.Since you are not setting which sheets to be checked from line below,
It will check for the activesheet cells i,15 then return -1 or 0 (true or false).
Which will only work when your screen displays sheet where data is stored.
If (StrComp(Cells(i, 15).Text, "TRUE")) = 0 Then
2.If your DATA contains something else then TRUE (for example space before or after the value).
It might see it as something other than "TRUE"
That's all I can tell without looking at your actual data.

Resources