プロが教えるわが家の防犯対策術!

例えば、A1セルに45、A2セルに15の数値が打ち込んであるとします。
A3セルに、VBAで作成した掛け算のプログラムを使用して答えを出したいのですが、
繰上げの部分がよくわかりません。
アドバイス頂けないでしょうか。

A 回答 (1件)

質問の意図がいまひとつ掴めないので、補足をお願いします。



「*」演算子を使わずに掛け算の演算を行うユーザー関数をVBAで
作ったけど、繰り上がりの部分が思った通りに動作しない と、
いう意味でよろしいのですか?

もしそうだとしたら、そのVBAで作られたユーザー関数のロジックなり
コードをご呈示いただかないとアドバイスのしようがありません。

もし、的をはずしていたら、ごめんなさい。

この回答への補足

仰る通りです。
下のコードを授業中に作成して、先生からこれを引用して作ってください、と言われたんですが、
何をどうしたらいいのかまったくわからないんです。
頭ではわかってはいるんですが、それをどう打ち込めばいいのかいいのか…。

Sub execPlustest()

For i = 9 To 1000
jmax = i

If i > 100 Then jmax = 100

For j = 1 To jmax

If i + j <> CInt(execPlus(CStr(i), CStr(j))) Then
Cells(1, 1) = i
Cells(1, 2) = j
Cells(1, 3) = "=execPlus(A1,B1)"
Exit Sub
End If
Next j
Next i

MsgBox "OK"

End Sub

Function plus(strA As String, strB As String) As String

If Len(strA) >= Len(strB) Then
plus = execPlus(strA, strB)
Else
plus = execPlus(strB, strA)
End If

End Function

Private Function execPlus(strA As String, strB As String) As String

Dim L1 As Long
Dim L2 As Long
Dim ans As String
Dim kuriage As Integer
Dim a As Integer
Dim b As Integer
Dim wa As Integer

L1 = Len(strA)
L2 = Len(strB)

ans = ""
kuriage = 0

For i = 0 To L2 - 1
a = CInt(Mid(strA, L1 - i, 1))
b = CInt(Mid(strB, L2 - i, 1))

wa = kuriage + a + b

If wa <= 9 Then
ans = CStr(wa) & ans
kuriage = 0
Else
ans = Mid(wa, 2, 1) & ans
kuriage = 1
End If
Next i

If kuriage = 0 Then
If L1 = L2 Then
execPlus = ans
Else
execPlus = Mid(strA, 1, L1 - L2) & ans
End If

Exit Function
End If

For i = L2 To L1 - 1
a = CInt(Mid(strA, L1 - i, 1))
wa = a + kuriage

If wa <= 9 Then
ans = CStr(wa) & ans
kuriage = 0
Exit For
Else
ans = Mid(wa, 2, 1) & ans
kuriage = 1
End If

Next i

If i <= L1 - 2 Then
ans = Mid(strA, 1, L1 - i - 1) & ans
ElseIf i = L1 Then
ans = "1" & ans
End If

execPlus = ans

End Function

補足日時:2006/10/27 02:58
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!