PDA

View Full Version : Error in Validation.Add - Application define or Object defined error" in Excel 2013



rsinha
06-11-2015, 02:40 PM
Hi,
I have following code in Excel macro that puts validation rule on a range of cells –

'// Open the File that was selected
If Right(sInFile, 3) <> "csv" Then
'//Array of all 256 colums
For i = 0 To 255
vFieldInfo(i) = Array(i + 1, 2)
Next i
Workbooks.OpenText Filename:=sInFile, _
Origin:=xlWindows, _
StartRow:=1, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=False, _
FieldInfo:=vFieldInfo
Else
Workbooks.Open Filename:=sInFile
End If

Sheets(1).Select
Sheets(1).Name = sInSheet
Sheets(1).Unprotect
OpenFile = True
'// Specify range that must have validation rule
Dim iLastCol As Integer
Dim rRange As Range

iLastCol = Range("I6").Value
Set rRange = Range(Cells(iRow, 10).Address + ":" + _

Cells(iRow + 2, iLastCol).Address + "," + _
Cells(iRow + 4, 10).Address + ":" + _
Cells(iRow + 5, iLastCol).Address)

'// Validation
For Each area In rRange.Areas
count = area.count()
area.Activate
area.Locked = False
area.Value = 0
With area.Validation
.Delete
.Add Type:=xlValidateWholeNumber, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlGreaterEqual, _
Formula1:="0"
.ErrorTitle = "Integers"
.ErrorMessage = "Please enter a positive integer value"
End With
Next

When I run this program in debug mode, i.e. by pressing F8 line by line, the validation rule is
correctly placed in the output file on the above range. But when I run it by clicking on a button
I get following error on ".Add Type" in area.Validation –
"Application define or Object defined error"

I checked the value of rRange both in debug mode and clicking on the button and the
range values are the same in both. Also this error occurs only with Excel 2013. On the
previous versions it works fine.

Does anyone know why it doesn't work in Excel 2013, but works when we run it in debug?

Thanks