![]() |
#1
|
|||
|
|||
![]()
Hi
In cell A1 I have a dropdown list using the validation list type. In cell E1 I have a True or False formula. For example if the values 10, 20 and 30 in the dropdown list correspond to True, I want the macro to tell the dropdown list to stop its run at the value 30 and not move to the value 40 which would correspond to False, Also how could I repeat the macro for the following rows? Thanks |
#2
|
|||||
|
|||||
![]() Quote:
Quote:
Quote:
Quote:
Quote:
Let me know. |
#3
|
|||
|
|||
![]()
Hi
Attached the worksheet I am working on basically it is an engineering design problem: 1- In cell AA I have a dropdown list with values going from 10, 20, 30, and up to 100. 2- In cell CE I have a value as long as this value is less than 1 then the design is safe. 3- In cell CF I have a what if formula [=IF(CE10<=1,"Safe","Unsafe")] if the value in cell CE is less than 1 then the design is safe if it is more than 1 than the design is unsafe. What I want the macro to do is go through the values in the dropdown list in AA one by one as long as the formula in CF states it is safe then it is ok, however if it returns with an answer unsafe than that the wrong answer. I want the macro to stop at the last value in the dropdown list that will give the answer safe. For example subjecting a member to a force 50 is safe however if it is subjected to a force 60 it will become unsafe. Basically I want the answer that will give me the max force a member can take before becoming unsafe. Thanks |
#4
|
|||
|
|||
![]() Quote:
|
#5
|
|||
|
|||
![]() Quote:
Code:
Sub BeSafe() 'code to try number from 10 - 100 until an unsafe calculation is performed Dim CheckCell As Integer, CheckValue As Integer CheckRow = 10 CheckValue = 10 Do Do Range("aa" & CheckRow).Select Range("aa" & CheckRow).Value = CheckValue CheckValue = CheckValue + 10 Loop While Range("cf" & CheckRow).Value = "Safe" ' Take the value back to safe Range("aa" & CheckRow).Value = CheckValue - 20 ' Check if value is above 100 If Range("aa" & CheckRow).Value > 100 Then Range("aa" & CheckRow).Value = 100 End If CheckRow = CheckRow + 1 CheckValue = 10 Loop Until Range("aa" & CheckRow).Value = "" End Sub I will not force this knowledge upon you but will offer to explain it in a future post if you wish. By the way this code should work for multiple rows if you add more. We can also have the code write the if statement in CF so you dont have to copy down the formulas but this should work for now. Let me know how it works for you. Oh one more thing. I get the feeling you were doing values 1 - 10 in column AA because it was a dropdown list. If you want to make this macro do the same but check with any value from 1 to 100 it is very easy to modify the code to do this and it will run just as fast. Let us know Thanks |
#6
|
|||
|
|||
![]()
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 |
#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. |
#8
|
|||
|
|||
![]()
My apologies the code will not run just as fast since it will check in increments of 1. The regular code runs in less than a second but this method above takes about 3 seconds. Still pretty quick by I wanted to correct my inaccurate statement.
|
![]() |
|
![]() |
||||
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 |
![]() |
r_p_t_0 | Word | 2 | 12-18-2012 05:55 AM |
![]() |
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 |