Solved

Converting If Then statemnts to VBA

Posted on 2011-02-10
5
191 Views
Last Modified: 2012-05-11
Hi all, I need to convert the following If then statements used for conditional formatting to VBA format so they can be used to determine if the formatting was applied. I would also need another formula that combines the two, as I need formatting that would cover both of these formats. Many thanks for any assistance you all can provide.

=IF(AND(NOT(ISBLANK(I29)),NOT(ISBLANK(G29)),NOT(ISBLANK(E29)),NOT(SUM(G29+I29)=E29)),TRUE,FALSE)

=IF(AND(OR(((F8-I8)/I8>0.2),((F8-I8)/I8<-0.2)),NOT(ISBLANK(I8)),NOT(ISBLANK(F8))),TRUE,FALSE)
0
Comment
Question by:shoaibs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 300 total points
ID: 34866011
If Evaluate("IF(AND(NOT(ISBLANK(I29)),NOT(ISBLANK(G29)),NOT(ISBLANK(E29)),NOT(SUM(G29+I29)=E29)),TRUE,FALSE)") THen

Open in new window


and:
If Evaluate("IF(AND(OR(((F8-I8)/I8>0.2),((F8-I8)/I8<-0.2)),NOT(ISBLANK(I8)),NOT(ISBLANK(F8))),TRUE,FALSE)") Then

Open in new window


Didn't really follow the last part.
0
 
LVL 55

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 100 total points
ID: 34866056
=IF(AND(NOT(ISBLANK(I29)),NOT(ISBLANK(G29)),NOT(ISBLANK(E29)),NOT(SUM(G29+I29)=E29)),TRUE,FALSE)

dim result as boolean

if (i29<>"" and G29 <>"" and E29<>"") then
if  cint(G29)+cint(I29)=cint(E29) then
  result = true
else
  result = false
end if
0
 
LVL 42

Assisted Solution

by:dlmille
dlmille earned 100 total points
ID: 34866143
That works, but if your ranges are changes, or you're moving down a column trying to do this formula in a loop, you'll need to have those ranges handy and rebuild the suggested EVALUATE concatenating that string together with your changing addresses, or do this:

Dim colI as string, colG as string, colE as string, colF as string
dim colIaddr as range, colGaddr as range, colEaddr as string, colFaddr as string

be sure to set these ranges as you go through your loop (assuming you are in a loop), then:

For row = 1 to whatever '(assuming going down row by row)

  colI = range("I" & row).address
  colG = range("G" & row).address
  colE = range("E" & row).address
  colF = range("F" & row).address

  If Evaluate("IF(AND(NOT(ISBLANK(" & colI & ")),NOT(ISBLANK(" & colG & ")),NOT(ISBLANK(" & colE & ")),NOT(SUM(" & colG & "+" & colI & ")=" & colE & ")),TRUE,FALSE)") Then
          your code.
..
..

'and

If Evaluate("IF(AND(OR((("& colF & "-" & colI & ")/" & colI & ">0.2),((" & colF & "-" & colI & ")/" & colI & "<-0.2)),NOT(ISBLANK(" & colI & )),NOT(ISBLANK(" & colF & "))),TRUE,FALSE)") Then
      your code

...
...
next row

Hope this helps!

Dave

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 34866252
Not really - if those formulas are being used for CF, you can pull them straight out of the FormatConditions for each cell and evaluate them directly. :)
0
 

Author Closing Comment

by:shoaibs
ID: 34866399
@Rorya- thank you so much for that simple solution- I spent all afternoon tearing my hair out trying to convert those statements directly to VBA

@dlmille- thanks for your help as well- very good to know.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question