Board logo

標題: 乘法運算? [打印本頁]

作者: q1a2z5    時間: 2014-2-19 13:30     標題: 乘法運算?

可否用程式將乘法運算過程解算出來?
謝謝!
作者: owen06    時間: 2014-2-19 18:14

回復 1# q1a2z5


這樣可以嗎?
作者: owen06    時間: 2014-2-19 19:02

回復 2# owen06


  抱歉,上一個檔有寫地方沒寫好,這個你試試看
作者: stillfish00    時間: 2014-2-19 20:42

回復 1# q1a2z5
  1. Sub Test()
  2.   Dim sA As String, sB As String, sM As String, sRslt As String
  3.   Dim rngA, rngRslt As Range
  4.   
  5.   Set rngA = Range("J4:M4")   '範圍或位置只要改這裡就好
  6.   
  7.   sA = Join(Application.Transpose(Application.Transpose(rngA.Value)), "")
  8.   sB = Join(Application.Transpose(Application.Transpose(rngA.Offset(1).Value)), "")
  9.   
  10.   '清除之前的結果
  11.   With rngA
  12.   With .Offset(2, -.Count).Resize(.Count + 1, 2 * .Count)
  13.     .ClearContents
  14.     .Borders(xlInsideHorizontal).LineStyle = xlContinuous
  15.     .Borders(xlInsideHorizontal).LineStyle = xlNone
  16.   End With
  17.   End With
  18.   
  19.   '計算過程
  20.   For i = 1 To Len(sB)
  21.     sM = CStr(CLng(Mid(sB, Len(sB) - i + 1, 1)) * CLng(sA))
  22.     With rngA.Resize(, rngA.Count + 1).Offset(1 + i, -i)
  23.       For j = 1 To Len(sM)
  24.         .Cells(.Count - j + 1).Value = Mid(sM, Len(sM) - j + 1, 1)
  25.       Next
  26.       If i = 1 Then .Borders(xlEdgeTop).LineStyle = xlContinuous
  27.     End With
  28.   Next
  29.   
  30.   '計算結果
  31.   sRslt = CStr(CLng(sA) * CLng(sB))
  32.   Set rngRslt = rngA.Offset(2 + Len(sB), -Len(sB)).Resize(, Len(sA) + Len(sB))
  33.   With rngRslt
  34.     .Borders(xlEdgeTop).LineStyle = xlContinuous
  35.     For i = 1 To Len(sRslt)
  36.       .Cells(.Count - i + 1).Value = Mid(sRslt, Len(sRslt) - i + 1, 1)
  37.     Next
  38.   End With
  39. End Sub
複製代碼

作者: Hsieh    時間: 2014-2-19 23:10

回復 1# q1a2z5
  1. Sub ex()
  2. Set 被乘數 = [J4:M4]
  3. Set 倍數 = [J5:M5]
  4. ActiveSheet.UsedRange.Offset(倍數.Row, 0).ClearContents '清除
  5. n = Val(Join(Application.Transpose(Application.Transpose(被乘數)), ""))
  6. m = Val(Join(Application.Transpose(Application.Transpose(倍數)), ""))
  7. k = 被乘數.Column + 被乘數.Count - 1: r = 倍數.Row + 1
  8. Do While IsNumeric(Cells(5, k)) And Cells(5, k) <> ""
  9. s = n * Cells(5, k)
  10.    For i = Len(s) To 1 Step -1
  11.       Cells(r, k - (Len(s) - i)) = Val(Mid(s, i, 1))
  12.    Next
  13.    r = r + 1
  14.    k = k - 1
  15. Loop
  16. For i = Len(n * m) To 1 Step -1
  17.       Cells(r, 13 - (Len(n * m) - i)) = Val(Mid(n * m, i, 1))
  18. Next
  19. End Sub
複製代碼





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)