Solved

# Excel If tests

Posted on 2016-10-20
54 Views
So, I would like to do some real Excel kung Fu.  I have to evaluate A LOT of excel documents.  The formatting has to be exactly right.  I want to make a tab that I can copy into the doc, that will run a bunch of tests to check the data and formatting.

Like (For Exact match's) (this works fine)
=IF(A4=Sheet1!A2,"The Value is Exact","The value is NOT exact")

But the more complicated ones, I am not sure how to check;
Is this a Date in specific format mm/yyyy or mm/dd/yyyy
Is this a number
Is this a WHOLE number (no decimals) (FIXED)
=IF(INT(SHEET1!L3)=SHEET1!L3,"Is a whole #","Is NOT a Whole #")
Is this a Number with 2 decimals
Does this have a '.' (period) in it anyplace)
Does this SUM to 1000
Does A1= any value in a list (column or something)
Is the cell formatted as number/text/general

One last thing, when I copy to a new sheet, it keeps trying to reference the old file like;
=IF(A5=[20161020_Tester_Tab.xlsx]SHEET1!A1,"The Value is Exact","The value is NOT exact")

More awesome, How would I have it check MORE then just one value, but a whole set of values A3-(Last value)

Please save me days and weeks of work, and help me out :(
0
Question by:loftyworm
• 5
• 4

LVL 49

Expert Comment

ID: 41853215
for a series of validations, you need to determine:
* a list of validation rules
* the sequence of validation rules

if the validation rules is too long, you would probably not able to enter all rules in the cell's formula, and hence you probably may do the validation in Macro function instead. and then call it when you want to do the validation.

One last thing, when I copy to a new sheet, it keeps trying to reference the old file like;
that's the default behaviour how Excel works. You will need to change the formula manually if you want it to refer to the same workbook.
0

LVL 43

Expert Comment

ID: 41853282
I would suggest doing it in vba macro too. A bit harder if not familiar but would be portable to other sheets, just have it work down a range of cells or sheets.

Done if the above such as checking date formats and numbers with two decimal places etc are only related to the formatting if the cell as the data would be a date or number but could be formatted to show 21-oct. 21/10/16 or whatever and value of a number could be 1 but down as 1.00 or conversely 12.542575 but down as 13

How many checks are we talking here?

Steve
0

LVL 11

Author Comment

ID: 41853833
TY!!
Can I get some more direction on the specific macros.  I have zero experience with them (other then disabling them for security)

The tests will be 1 per cell, not all at once.
0

LVL 43

Expert Comment

ID: 41854027
Suggest give us a example sheet with some data in it and can give some examples.

Steve
0

LVL 11

Author Comment

ID: 41854051
Sure, one sec
0

LVL 11

Author Comment

ID: 41854113
Sorry about the delay, got called by boss.
Here is an example of what I need to test

Ideally (Open to suggestion) I would think I would take the tab, and copy it to the FILETOBETESTED.  I have had problems with this though, as the tab remembers the file name, so I may have to copy the data to the tester file.

I know how to do the color formatting so that all the TRUE values would be green, and all the bad values red.  I just need the tests.
C--Temp-Book1.xlsx
0

LVL 43

Accepted Solution

Steve Knight earned 500 total points
ID: 41854225
A quick example bit of VBA macro is below and attached in spreadsheet.  This runs down the cells you have selected and based on the column it checks for various formatted data.  In this case then colours them in red or green whether they pass or not but could do whatever else was needed.

You could have a master sheet which you paste the values into, you could point it at a data sheet and have it copy the data out as it is displayed and copy it... anything possible but depends more on what you want to do.

At the moment it checks the value displayed in the cell (cell.text) rather than checking the actual value (cell.value) or how it is formatted.

Steve

Sub CheckData()

Dim myarea As Range
Dim ans As Integer
Dim CheckCell As Boolean
Set myarea = Selection.Cells

ans = MsgBox("You have highlighted " & myarea.Cells.Count & " cells.  Is this right to continue?", vbYesNo + vbQuestion + vbDefaultButton2, "CHECK WHICH CELLS")
If ans = 7 Then Exit Sub

For Each col In myarea.Columns
For Each cell In col.Cells
CheckCell = True
Select Case cell.Column
Case 2 ' Column B - check if specific strings
Select Case cell.Text
Case "bob", "steve", "mary": CheckCell = True
Case Else: CheckCell = False
End Select

Case 3 ' Column C - check if mm/dd/yyyy format
If Not cell.Text Like "##/##/####" Then CheckCell = False

Case 4 ' Column D - check if specific string value
Select Case cell.Text
Case "2Car1House", "1Car2House": CheckCell = True
Case Else: CheckCell = False
End Select

Case 6 ' Column F - check if Integer of value is same as value
If Not cell.Text Like "*#" Then CheckCell = False

Case 10 ' Column J - check if it looks like number pattern shown
If Not cell.Text Like "*#.##" Then CheckCell = False
End Select
Select Case CheckCell
Case False: cell.Interior.Color = 255
Case True: cell.Interior.Color = 5296274
End Select
Next
Next

End Sub
C--Temp-Book1.xlsm
0

LVL 43

Expert Comment

ID: 41859114
Thanks for choosing my answer but if you want any extra help or clarification just say.  There are lots of ways of doing things like this of course.

Steve
0

LVL 11

Author Comment

ID: 41859439
0

LVL 11

Author Comment

ID: 41859441
Oops, I think maybe you have!!  This is what I get for doing to many things at the same time
0

## Featured Post

### Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…