Jump to content



Featured Articles

Check out the latest featured articles.

File Library

Check out the latest downloads available in the File Library.

New Article

Product Viscosity vs. Shear

Featured File

Vertical Tank Selection

New Blog Entry

Low Flow in Pipes- posted in Ankur's blog

Problem In Conditional/ Logical Operation In Excel Vba


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic
Share this topic:
| More

#1 Vegeta

Vegeta

    Gold Member

  • Members
  • 131 posts

Posted 03 October 2015 - 09:03 AM

Dears,

I have tried the attached code and the result is also attached. The purpose of this code is to check the temperatures (cell A13, A14, ...) if it falls between the values in cells B8 &B9. If it does, the font color should be blue with the cell filled with yellow color with the word "True" in it. The problem is that: cell B23 must be "True" and it is "False". What is wrong with the code?

Note: In the code,

N_Cold_Streams= 5

Tot_counter = 60

Those variables are just to continue the checking further.

Regards,

Attached Files

  • Attached File  1.JPG   31.85KB   3 downloads
  • Attached File  2.JPG   65.78KB   3 downloads


#2 Bobby Strain

Bobby Strain

    Gold Member

  • Members
  • 3,529 posts

Posted 03 October 2015 - 09:46 AM

Your code is strange to me. Maybe if you posted your workbook someone might look at it. And, you should simplify the code so that it is readily understood by humans. I suggest that you get a VBA book by John Walkenbach and follow good practice. And don't skip the first few chapters. And, since you are looking for something to do, learn to program with C#. You will find that this skill will greatly enhance your capability as a chemical engineer. While learning C#, you will also learn how to think logically. And, if you have an instutition of higher learning near, you should take a computer science course. This will help you much more than anything else you might do.

ps

Another thought came to me during my nap. Obviously, Excel has an opinion different from yours about the numbers presented to it. Lucky for you, resolution is easy. All you need to do is see what Excel is seeing, perhaps. Then you can adapt your code.This is all part of debugging, an essential ingredient in programming. I think you gave up too soon. But that's the youthful quest for instant gratification.

pps

And, tomorrow I will post a workbook that resolves your dilema as you have engaged my curiosity. "If at first you don't succeed, try, try again."  And I have attempted to make the code simple and easy to understand and maybe useful, at least helpful, for other endevors  with some editing. Between the spreadsheet and the code, you will see some relatively good effort to achieve simplicity and comprehension.  Hopefully, it will also raise a curiosity that you might pursue learning more.

 

Bobby


Edited by Bobby Strain, 03 October 2015 - 10:50 PM.


#3 Vegeta

Vegeta

    Gold Member

  • Members
  • 131 posts

Posted 04 October 2015 - 12:11 PM

Bobby,
Thank you for your advice and I will consider that.

Any more suggestions on the code? Sorry I can't post the workbook as it contains the work of other engineers and this is my part.

#4 Bobby Strain

Bobby Strain

    Gold Member

  • Members
  • 3,529 posts

Posted 04 October 2015 - 02:38 PM

Here is a workbook that does what you wish. I also added several context menus so you can mark/unmark selection. And one to implement your exercise. Note that I formatted the temperatures, not "true" cells. I think that is really your objective. You could delete other portions from your workbook and include only your code. You will get more responses when you do. Excel undoubtedly looks at some values with a different view. So you just have to explore alternatives when this happens. But it's good practice to explicitly convert input to get the type you want. In this case, that did not solve the problem. Rounding the values, however, makes Excel properly evaluate the values.

 

The solution to your problem is even simpler than the one I implemented in the workbook. You only need to convert the values with the Val(text) function. So if you simply make this modification to your code for all of the values read from the sheet, it will work properly.

               example              e = Val(Cells(8, Reference))

But, it is best to develop good programming style early.

 

Bobby

Attached File  Exercise.xlsm   26.2KB   21 downloads


Edited by Bobby Strain, 04 October 2015 - 10:37 PM.





Similar Topics