Solved

Converting If Then statemnts to VBA

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

740 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