Solved

if then statement in vba doesn't respond with excel code

Posted on 2012-04-05
4
339 Views
Last Modified: 2012-04-07
The formula in the excel sheets   cell F5=IF('Bun Mixers'!$D$9>0,IF('Bun Mixers'!$D$10>(VLOOKUP('Bun Mixers'!$D$9,'Bun Dough Recipe Information'!$A$2:$D$62,4)+(SUM(PRODUCT(VLOOKUP('Bun Mixers'!$D$9,'Bun Dough Recipe Information'!$A$2:$D$62,4),5%)))),"Actual mix time is 5% OVER setpoint",""))
 This formula works well in the excel spread sheet.  
I want to pick up this cell if it is true in a report using vba code.

the code is as follows:
Sub Bun_Mixers_Update_Data_Report()
'updating Bun Mixers Data
If Not (IsNull(Worksheets("Bun Mixers").Range("F5"))) Or Not (IsNull(Worksheets("Bun Mixers").Range("F6"))) Then

Worksheets("Bun Report").Range("E1:F8").Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Worksheets("Bun Mixers").Range("E2:F9").Copy
    Worksheets("Bun Report").Range("E1").PasteSpecial xlPasteValues
    Worksheets("Bun Report").Range("E9:F16").Copy
    Worksheets("Bun Report").Range("E1:F8").PasteSpecial xlPasteFormats
    
With Worksheets("Bun Report").Range("F1")
    .NumberFormat = "m/d/yyy h:mm"
    .Interior.ColorIndex = 6
    .Interior.Pattern = xlSolid
End With

With Worksheets("Bun Report").Range("E1")
    .Font.Bold = True
    .Interior.ColorIndex = 6
    .Interior.Pattern = xlSolid
End With

Worksheets("Bun Report").Range("F9").HorizontalAlignment = xlCenterAcrossSelection
Worksheets("Bun Report").Range("F9").NumberFormat = "0.00"

End If

Open in new window


What will work in this code to get an either or situation?

Thanks,
Angie
0
Comment
Question by:Angiehncock
[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
  • 2
4 Comments
 
LVL 3

Expert Comment

by:DaFranker
ID: 37811579
The condition in your initial IF statement checks if the cell range is null. It will never be, as it is a range reference, and the cell also has a formula, which means it isn't 'empty' either.

Assuming the cells are using standard formatting, you could check for:
Worksheets("Bun Mixers").Range("F5").Text = "" Or Worksheets("Bun Mixers").Range("F6").Text = ""

Open in new window

0
 

Author Comment

by:Angiehncock
ID: 37813855
Unforntuately this gives me a type mismatch error '13'.  I also tried If Not(Is Empty,  this gave me everything or nothing depending on whether I used Not.
0
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 37814685
Since you can already do error checking in the worksheet formula such that you get a text string on the TRUE and null string on the FALSE/error, then you can test for NOT vbNullString.  I didn't rewrite your formula to do error checking.  Let's just see if this modification in the VBA works for you.

Why not just change line 4 to:

If Worksheets("Bun Mixers").Range("F5").Value <> vbNullString Or Worksheets("Bun Mixers").Range("F6").Value <> vbNullString Then

Dave
0
 

Author Closing Comment

by:Angiehncock
ID: 37818899
This works well provided that the formula is F5 is constructed properly.   Thanks for your help!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

759 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