Solved

Converting If Then statemnts to VBA

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

785 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