Solved

Converting If Then statemnts to VBA

Posted on 2011-02-10
5
185 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
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 51

Assisted Solution

by:HainKurt
HainKurt 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 41

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now