![]() |
|
#7
|
|||
|
|||
|
Quote:
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 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 Code:
Do
Do
Code:
Range("aa" & CheckRow).Select
Range("aa" & CheckRow).Value = CheckValue
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 Code:
Loop While Range("cf" & CheckRow).Value = "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
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 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 = ""
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) Code:
End Sub 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. |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| VBA: How to place dropdown list next to text | YigalB | Word VBA | 0 | 08-11-2013 01:48 PM |
| block selection in dropdown list | Intruder | Excel | 2 | 01-10-2013 10:20 AM |
dropdown list for documents
|
r_p_t_0 | Word | 2 | 12-18-2012 05:55 AM |
Can I have a dropdown list to choose from in mail merge?
|
lawlaw | Mail Merge | 3 | 11-29-2012 04:47 PM |
| Dropdown list of email addresses | J Partridge | Outlook | 1 | 01-13-2011 06:37 AM |