[英]Excel error on function MOD
我想在我的excel文件(最好是VBA)中輸入此公式。 但是由於某種原因,當我在公式欄中放置此函數時,我得到一個錯誤:
#NUM! - Number Error
我的職能是:
=98-mod(2000000000123456*100;97))
但是,當我只輸入10位數字時,它就可以工作。 我需要使用16位數字。 我基本上是在嘗試根據ISO 7064 Mod 97,10公式驗證數字,謝謝。
感謝pepople,感謝您的努力,但是我嘗試了@Robyn編寫的代碼,但出現溢出錯誤。 這是我的代碼(我將只寫Function部分):'注意:一個輸入的參數是:3259300700853850和97
Function DblMod(Dividend, Divisor)
' Declare two double precision variables
Dim D1 As Double
Dim D2 As Double
' Copy function arguments to local variables
D1 = Dividend
D2 = Divisor
DblMod = D1 Mod D2
End Function
如前所述,Excel的精度限制為15位數字。 因此,當您在上方輸入公式時,您的16位數字將被更改:
2000000000123456
變成2000000000123460
解決此限制的一種方法涉及使用VBA並更改您的公式。
而不是輸入16 digit number * 100
,而是將整個值輸入為字符串:
“ 200000000012345600”
例如:
=98-xMOD("200000000012345600";97)
然后使用此UDF,它利用了VBA的Decimal數據類型提供的提高的精度:
Option Explicit
Function xMOD(Num As Variant, Div As Long)
Dim x As Variant, y As Variant
x = CDec(Num)
y = CDec(Div)
xMOD = x - Int(x / y) * y
End Function
如果要在某個單元格中輸入16位數字( 作為字符串 ),並且始終乘以100,則可以使用以下公式:
=98-xMOD(C1&"00";97)
或者,您可以只輸入18位數字作為字符串到某個單元格中,並在公式中引用它,而忽略&"00"
部分。
如果可以找到另一種選擇,那就是不受支持的免費加載項xNumbers
。 盡管不存在與功能區的集成,但最新版本6.0
仍適用於Excel 2016,因此無法使用某些配置選項。
編輯自從您寫下您無法使該UDF在您的系統中工作以來,我將發布一個屏幕截圖,顯示該UDF在我的系統中確實有效,並且與您在賞金描述中發布的結果一致。 (xNumbers中的xIntMod也返回相同的結果)
對於abs()小於或等於2,147,483,647的數字
根據Microsoft支持文章XL,這是一個已知問題:MOD()函數返回#NUM! 錯誤值 。
請注意,本文已過時-因此,這可能不是您實例中的真正根本原因。
本文建議以下解決方法:
代替使用此公式
=MOD(J13,K13)
使用以下公式:=J13-(INT(J13/K13)*K13)
或者, 本文建議使用等效的VBA:
Function DblMod(Dividend, Divisor)
' Declare two double precision variables
Dim D1 As Double
Dim D2 As Double
' Copy function arguments to local variables
D1 = Dividend
D2 = Divisor
DblMod = D1 Mod D2
End Function
對於abs()大於2,147,483,647的數字
在這種情況下,VBA MOD函數會因Run Time Error '6': Overflow
。 Microsoft支持文章PRB:“溢出”(使用整數除法和MOD Operator)中解決了此問題 。 本文提出了VBA中的以下解決方案,我已經使用OP的給定編號測試了該解決方案的預期效果:
Sub Main()
Call DblMod2(3259300700853850, 97)
' returns 46, which matches the Big Integer Calculator at http://www.javascripter.net/math/calculators/100digitbigintcalculator.htm suggested by Ron
End Sub
Function DblMod2(Dividend, Divisor)
Dim dblX As Double
Dim dblY As Double
dblX = Dividend ' numerator
dblY = Divisor ' denominator
' round off the numerator and denominator (ensure number is .0)
dblX = Int(dblX + 0.5)
dblY = Int(dblY + 0.5)
' Emulate integer division with: Fix(dblX / dblY)
' Emulate modulo arithmetic with: dblX - (dblY * Fix(dblX / dblY))
DblMod2 = dblX - (dblY * Fix(dblX / dblY))
Debug.Print DblMod2
End Function
沒有為MOD功能的備用位置
a - (b * (a / b))
=98-((2000000000123450*100)-(97*((2000000000123450*100)/97)))
編輯1
嘗試以下操作,它將返回46
Function DblMod(Dividend, Divisor)
' Declare two double precision variables
Dim D1 As Double
Dim D2 As Double
Dim D3 As Double
Dim str1
Dim Str2
' Copy function arguments to local variables
D1 = CDbl(Dividend)
D2 = Divisor
DblMod = CDec(D1) - (Round(CDec(D1) / D2) * D2)
End Function
Sub test()
test = DblMod("3259300700853850", 97)
End Sub
與其處理非常大的數字,不如將它們拆分成這樣:
Public Function ExtMod(Dividend As String, Divisor As Long) As Double
Dim i As Long, j As Double
For i = 1 To Len(Dividend)
If Not IsNumeric(Mid(Dividend, i, 1)) Then
j = j & Mid(Dividend, i)
Exit For
End If
j = j * 10 + Mid(Dividend, i, 1)
While j >= Divisor
j = j - Divisor
Wend
Next
ExtMod = j
End Function
也適用於小數,語言環境無關緊要。 唯一的限制是您可能永遠不會碰到的最大字符串長度。
使用“ 123456789012345678901234567890.3241543”和643(即435.3241543)進行了測試。
如果你有問題,就問吧 ;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.