Quote:
Originally Posted by shield5
Hi
Thank you very much the loop worked perfectly, sorry for not responding earlier I was out of town the past few days.
I would greatly appreciate it if you could explain the loop like you suggested I would like to know how it works, as I would like to use the same method for other projects.
You are correct I limited it to 10 values from 10 to 100 because of the dropdown list, as I felt checking it for values from 1 to 100 will be imposable to do manually.
Thanks
|
Thanks for getting back to me and Bravo for asking how the code works. There are a lot of people that could care less and you will find yourself being able to write things like this with a little practice. I will address a couple of things with the posted code so you can
A- Understand some key terms and methods
B- Understand how the code works
C- Understand how to manipulate the code to do other things such as perform the calculation going from 1 to 100 rather than 10 to 100.
With all that said lets look at the code again and we will break down each part.
Code:
Sub BeSafe()
'code to try number from 10 - 100 until an unsafe calculation is performed
This is the beginning to most macros it stands for sub routine. The sub can be named whatever you want it to be except that it cannot start with number or special symbol and must be all one word.
the part where it says "code to try number from 10 - 100 blah blah" is actually a comment.
You can see this from the ' marking.
Placing the ' before anything in your VBA code turns it to a comment giving you the ability to make any notes or explanations.
Next Part
Code:
Dim CheckCell As Integer, CheckValue As Integer
CheckRow = 10
CheckValue = 10
This part can get a little tricky but all it is doing is declaring or dimming variables. Variables are basically names that you can reference later in your code. In this macro we have 2 variables that will be integers otherwise known as basic numbers. We then took each of those integers and gave the the value of 10. This will be explained in the next notes.
We need to stop here for a second. DO is a loop. Basically it is a group of code that keeps looping until we tell it to stop. The reason DO is typed twice is because it is a loop within a loop. These are very confusing to write at first so it is better to write your inside loop and then write the outside loop around it. This will make more sense when we explain the loop a little more.
Code:
Range("aa" & CheckRow).Select
Range("aa" & CheckRow).Value = CheckValue
At this point it may look kind of overwhelming but just remember that the method "Range" allows you to select, copy, delete or otherwise modify a range of cells in Excel. Here is a simple example of the Range method. Range("a1").value = "Kitty Cat"
What that would do is put the text Kitty Cat in cell A1 of your worksheet
What this code does is a little bit different.
You will notice that the range is "aa" & CheckRow VBA sees this as AA10. why? because we declared CheckRow to be the value 10. Also notice that CheckRow is not in quotes but AA is. This is because if a text string is not in quotes VBA will look throughout the code to try and find out what it does. Meaning if you wrote range(AA & CheckRow).value VBA would say that AA is undefined because it would not be a variable. This is confusing at first but until you get it just follow the syntax of range till you are comfortable.
So with that long explanation the range method here selects AA10 again because the range is "AA" & the value of CheckRow equaling AA10. It gives the range AA10 the value of CheckValue which was declared as 10 as well up above. So range AA10 now equals 10
Code:
CheckValue = CheckValue + 10
Remember how DO is a loop. This loop is going to keep on repeating so we need to make it change each time the loop is done. This little piece of code takes the value of CheckValue and adds 10 to it. The first time it goes through CheckValue is equal to 10 the 2nd time it is equal to 20 next time till 30 and so on until it gets to where the loop stops.
Code:
Loop While Range("cf" & CheckRow).Value = "Safe"
This is where the Inside loop is told to stop. It basically says keep adding 10 to CheckValue while the range CF10 = "Safe"
Again notice that Safe is in quotes so VBA does not think it is a variable. CF contains your IF formula and the loop keeps on going until it says UNSAFE.
Code:
' Take the value back to safe
Range("aa" & CheckRow).Value = CheckValue - 20
But we do not want the value in CF to be UNSAFE we want it to go back to safe so what we need to do is kick the value back twice.
Why twice? reason is because the loop stops when the value in CF is UNSAFE but it still adds another 10 to CheckValue making it over by 20 so this piece of code takes the range of AA10 and gives the value of CheckValue - 20 thereby taking the CF range back to safe.
Code:
' Check if value is above 100
If Range("aa" & CheckRow).Value > 100 Then
Range("aa" & CheckRow).Value = 100
End If
This is what an IF formula looks like in VBA. No Parentheses or commas just if and then
This IF statement only looks if the value is true, if we wanted it to look at a false possibility we would do an else but it was not necessary. The reason for the if statement is because while I was testing this code with your spreadsheet the values in AA would go beyond 100 before the value in CF would result in UNSAFE. Per your instructions you wanted it to go up to 100 and no higher. So all we needed to do was see if the value was over 100. If it was, take the value down to 100 otherwise leave it. Again we used the range method in the IF statement and then when the IF statement was done we typed End If. This is very important since VBA needs to know when the IF test and result are done.
Code:
CheckRow = CheckRow + 1
CheckValue = 10
Loop Until Range("aa" & CheckRow).Value = ""
A lot has been covered so far but remember how DO was typed twice. So far we have only ended the inside loop but the outside loop is still running. This piece of code modifies itself so it can loop for a certain amount of times. here is what it does.
CheckRow was equal to 10 but after this piece runs it will be equal to 11 so the INSIDE loop will start all over again but will be performing the same functions except on AA11 rather than AA10.
The next thing this code does is take the value of CheckValue back down to 10. Why? because CheckValue may be 30 or 50 or even 150 because it kept increasing by 10 until CF was unsafe. We need to get it back to 10 so it can try each increment in AA11 until CF is unsafe.
And the last part the end of the outside loop. At this point it will be checking cell AA11 but will stop once the checked cell = nothing. Remember the row in AA to be checked will increase by 1 each time the loop runs so it will stop once it gets to row 20 (I think it may be row 21 which ever = nothing)
The Final part of all basic subs end sub means the macro is done.
A couple of things to remember when dealing with loops. There is always the possibility of accidentally writing something called an infinite loop. This means the Loop NEVER STOPS. the code above could be an infinite loop with something like
Loop until range("aa" & CheckRow).value = 150
This would never end because as the code works the IF statement in the code takes the value of AA down to 100 so the code would keep going forever and never stop, even it it ran out of rows in Excel. The way to stop an infinite loop is by pressing CTRL + Break on your keyboard.
The other thing is that I mentioned you could modify this code to do the same thing with 1 to 100 rather than 10 to 100. All you need to do is change CheckValue to 1 when it is declared and when the inside loop starts. You also need to modify the lines that say CheckValue = CheckValue + 10 to
CheckValue = CheckValue + 1
Lastly in the if statement since we are dealing with 10's simply change that to - 2.
So the code would now look like this
Code:
Sub BeSafe()
'code to try number from 1 - 100 until an unsafe calculation is performed
Dim CheckCell As Integer, CheckValue As Integer
CheckRow = 10
CheckValue = 1
Do
Do
Range("aa" & CheckRow).Select
Range("aa" & CheckRow).Value = CheckValue
CheckValue = CheckValue + 1
Loop While Range("cf" & CheckRow).Value = "Safe"
' Take the value back to safe
Range("aa" & CheckRow).Value = CheckValue - 2
' Check if value is above 100
If Range("aa" & CheckRow).Value > 100 Then
Range("aa" & CheckRow).Value = 100
End If
CheckRow = CheckRow + 1
CheckValue = 1
Loop Until Range("aa" & CheckRow).Value = ""
End Sub
Keep in mind this level of VBA is not suppose to be easy to comprehend at first but look at each piece and my explanations until it makes sense. If you are still having trouble understanding it let me know in a PM and we will discuss it further. I am completely self taught with all this and I know how difficult it can be to learn. I am here to help and I thank you for the opportunity to teach and learn from this exercise.