可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using VBA in my excel sheet and it gives me an overlfow error. When I checked I found out that it was related to data error which shows unilmited "#" character in one cell.
All values in each cell is assigned to a string variable and when this cell comes in generates an overflow error. I tried to validate this using an if condition, but whenever we check cell.value this generates an overflow error. Is there any way to fix this?
回答1:
You will not get a runtime error if you just read the cell text:
s = Range("C2").Text Debug.Print s ' returns: "###########"
If you really want to read the cell value, and not the cell text (why you would need this, I have no idea, since you're stuffing it in a String anyway) then this is a workaround, assuming the #### are caused by dates Jan 1, 10,000 AD or later.
Const MaxDateSerial As Double = 2958466 Dim s As String Dim dbl As Double Dim nf As Variant ' Save original number format nf = Range("C2").NumberFormat ' Use read-safe number format to read cell content Range("C2").NumberFormat = "General" dbl = Range("C2").Value ' Restore original number format Range("C2").NumberFormat = nf If dbl < MaxDateSerial Then s = Range("C2").Value Else s = "Date overflow!" End If
回答2:
If you are doing something like
Dim s as string Dim cl as Range ... s = cl ' or s = cl.value
s
will be set to the value as diplayed on the sheet, which includes an error
(in the form "Error <Error Code>
" if that's what the sheet displays
If the underlying cell value is valid, you should be able to access if using cl.value2
If you are doing somthing like
Dim v as Variant Dim s as string Dim r as Range ... Set r = <SomeRange> v = r ...
And one or more cells in range r
are an error, then Runtime Error 6 Overflow
will occur
To advise further, please post details of the code, where the error occurs, cell value and cell formatting
The ############### display for dates occurs if the date serial is before 1/1/1900 (negative) or after 1/1/9999 (> 2958101) EDIT actually 31/12/9999 = 2958465
回答3:
Try using the "ISERROR()" function to filter out the invalid values.
IF(ISERROR(A1),0,A1)