What I want this part of the code to do is if the value of cell H83 is greater than 22 then I want the code to increase the value of cell R14 which is 0.7 by an increment of 0.01 until either R14 reaches 0.75 or until H83 is less than 22. I have tried:
For j = 0.69 To 0.74
w = j + 0.01
If Range("h83") > 22 Then
Range("r14").Value = w
If 21 < Range("h83") < 23 Then Exit For
End If
Next j
This doesn't work and right now I have it so it increases it by 0.01 only once (part in asterisks), full code:
Sub C_CreateTestResultTableV2()
Application.ScreenUpdating = True 'helps the code run faster
Dim vInputs, vResults()
Dim c As Integer, i As Integer
'create INPUTS
c = Range("b5").End(xlToRight).Column
vInputs = Range("b5", Cells(9, c))
'determine last value in the column
c = UBound(vInputs, 2)
'create RESULTS
ReDim vResults(1 To 4, 1 To c)
For i = 1 To c
'checks to see if t_air_in > 22
If vInputs(1, i) > 22 And vInputs(3, i) < 70 Then
'set values
Range("j18") = vInputs(1, i)
Range("n14") = vInputs(3, i)
Range("r16") = vInputs(5, i)
'checks to see if t_air_out = 22 and changes t_wat_in and m_wat_in accordingly
If Range("h83") > 22 Then
Range("r16").Value = Range("r16").Value - 3
End If
*If Range("h83") > 22 Then
Range("r14").Value = Range("r14").Value + 0.01
End If*
'copy output values into RESULTS
vResults(1, i) = Range("h83")
vResults(2, i) = Range("k83")
vResults(3, i) = Range("z14")
vResults(4, i) = Range("r15")
End If
'resets values
Range("r16").Value = 13
Range("r14").Value = 0.7
Next i
Range("b96").Resize(4, c) = vResults
Application.ScreenUpdating = True
End Sub
If you want something to happen until a certain condition is met, but you don't know exactly when that happens, you need a Do-Loop:
Do While Range("H83").Value < 23
Range("R14").Value = Range("R14").value + 0.01
Range("H83").value = Range("H83").value + x 'If you don't do something with Range("H83"), the loop will go on forever
Loop
Edit: From someone who made this mistake more than willing to admit: If you create a do-loop, please make sure that you don't create an infinite loop, because the chance is high that VBA won't respond anymore.
Related
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)
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
Hi I am trying to compare two sets of data by having indicators if they increased, decreased, or stayed the same. I was able to get it working on one column. My problem is I can't loop it on multiple columns.
Basically:
If A1 = C1 then D1.Value = 0
If A1 > C1 then D1.Value = 1
If A1 < C1 then D1.Value = 2
I've tried to do the "do while" to add increments on the columns but it did not work.
Sub ChangeIndicator2()
Dim i As Long
Dim a As Long
Dim b As Long
Dim x As Long
Dim y As Long
Dim ProgramCount As Long
i = 2
a = 8
b = 2
x = 0
y = 8
ProgramCount = 12
Do While y <= ProgramCount
For Each c In Worksheets("Sheet1").Range("A2:A20").Offset(x, y)
If Worksheets("Sheet1").Cells(i, a).Value = Worksheets("Sheet1").Cells(i, b).Value Then
c.Value = 0
ElseIf Worksheets("Sheet1").Cells(i, a).Value < Worksheets("Sheet1").Cells(i, b).Value Then
c.Value = 1
ElseIf Worksheets("Sheet1").Cells(i, a).Value > Worksheets("Sheet1").Cells(i, b).Value Then
c.Value = 2
End If
i = i + 1
Next c
a = a + 2
b = b + 2
y = y + 2
Loop
End Sub
Only the first column works, the second column only shows 0 values.
So basically, what you want to do is compare 2 columns which are 2 columns apart and repeat that on another pair of columns which is 8 columns from the first column. If my assumption is correct then have a go at this:
For i = 0 To (ProgramCount * 8) Step 8
With Worksheets("Sheet1").Range("A2:A20").Offset(, i + 3)
.FormulaR1C1 = "=IF(RC[-3]=RC[-1],0,IF(RC[-3]>RC[-1],1,2))"
.Value2 = .Value2
End With
Next
Adjust the offset to suit your needs (I may have misunderstood the actual columns you target to update). Hope this helps.
For something like the code below..
For i = 0 to 40
R = R & "," & Application.WorksheetFunction.Min((Cell.Value + i * 5/8), 0.8)
Next i
How can I stop it when R starts producing the same result as last iteration (for example, i = 11 and i =12 producing the same result..)
I tried something like
If i = i - 1 Then
Exit for
End If
but VBA cannot read it
You need to store your previous result in some variable, and then compare it to the current result, then Exit For if equal.
Dim previousResult as Double
Dim thisResult as Double
For i = 0 to 40
previousResult = thisResult
thisResult = Application.WorksheetFunction.Min((Cell.Value + i * 5/8), 0.8)
If previousResult = thisResult Then Exit For
R = R & "," & thisResult
Next
One way to do it is to add another variable that stores the previous iteration's result before doing work to the current iteration's variable. Then you just compare them and exit the loop if they match.
For i = 0 to 40
Rprev = R
R = R & "," & Application.WorksheetFunction.Min((Cell.Value + j * 5/8), 0.8)
If R = Rprev Then
Exit For
End If
Next i
I want the code to read the value of 'vResults(1, i)' and if it is greater than 21, I want it to increase the value of 'vInputs(5, i)' by 1 and keep doing that until 'vResults(1, i)' is greater than 21.
Doesn't give out any errors, but Excel just crashes.
Sub CreateTestResultTableV2()
Application.ScreenUpdating = False 'helps the code run faster
Dim vInputs, vResults()
Dim c As Integer, i As Integer
'create INPUTS
c = Range("b5").End(xlToRight).Column
vInputs = Range("b5", Cells(9, c))
'determine last value in the column
c = UBound(vInputs, 2)
'create RESULTS
ReDim vResults(1 To 3, 1 To c)
For i = 1 To c
If vInputs(1, i) > 22 Then
'set values
Range("j16") = vInputs(1, i)
Range("n12") = vInputs(3, i)
Range("r14") = vInputs(5, i)
'copy output values into RESULTS
vResults(1, i) = Range("h41")
vResults(2, i) = Range("k41")
vResults(3, i) = Range("z14")
Do Until vResults(1, i) > 21
vInputs(5, i).Value = vInputs(5, i).Value + 1
Loop
End If
Next i
Range("c47").Resize(3, c) = vResults
Application.ScreenUpdating = True
End Sub
The do until loop repeats its instructions until the tested condition becomes true. In your case, yours has a condition that will never change:
Do Until vResults(1, i) > 21
vInputs(5, i).Value = vInputs(5, i).Value + 1
Loop
You will enter the loop until vResults(1, i) is greater than 21, but since the value never changes, the loop is never going to end (AKA : infinite loop). Windows will eventually say that excel is not responding and will ask you if you want to end the process to get out of the loop, which may look like the app has crashed.
Either change the value of vResults(1, i) or change the condition to check something else that will change in the loop (probably vInputs(5, i)).