返回列表 上一主題 發帖

[發問] 請教如何用VBA在另一工作表計算出結果?

回復 1# ji12345678
  1. Private Sub cbCal_Click()
  2.   Dim iSCol%, iTCol%, iNum%
  3.   Dim lSRow&, lTRow&
  4.   Dim sStr$
  5.   Dim shSou As Sheet1, shTar As Sheet3

  6.   Set shSou = Sheets("總表")
  7.   Set shTar = Sheets("變動")
  8.   
  9.   With shTar.Cells
  10.     .ClearContents
  11.     .Interior.ColorIndex = -4142
  12.   End With
  13.   
  14.   With shSou
  15.     iSCol = 2 ' 日期與增減量
  16.     Do While .Cells(1, iSCol) <> ""
  17.       shTar.Cells(1, iSCol) = .Cells(1, iSCol)
  18.       shTar.Cells(2, iSCol) = "增減量"
  19.       iSCol = iSCol + 1
  20.     Loop
  21.     sStr = Cells(1, iSCol - 1).Address
  22.     sStr = Mid(sStr, 2, InStr(2, sStr, "$") - 2)
  23.     shTar.Columns("B:" & sStr).ColumnWidth = 8.38
  24.    
  25.     lSRow = 3 ' 工班名
  26.     Do While .Cells(lSRow, 1) <> ""
  27.       shTar.Cells(lSRow, 1) = .Cells(lSRow, 1)
  28.       lSRow = lSRow + 1
  29.     Loop
  30.    
  31.     iSCol = 3
  32.     Do While .Cells(1, iSCol) <> ""
  33.       lSRow = 3
  34.       Do While .Cells(lSRow, 1) <> ""
  35.         sStr = .Cells(lSRow, 1)
  36.         
  37.         If Left(sStr, 1) <> "總" Then iNum = CInt(Mid(sStr, 2, Len(sStr) - 2)) Else iNum = 1
  38.         
  39.         With .Cells(lSRow, iSCol)
  40.          shTar.Cells(lSRow, iSCol) = .Value - .Offset(, -1)
  41.         End With
  42.         
  43.         With shTar.Cells(lSRow, iSCol)
  44.           Select Case .Value
  45.             Case Is > 0
  46.               If iNum > 9 Then .Interior.ColorIndex = 41 Else .Interior.ColorIndex = 38
  47.             Case 0
  48.               .Interior.ColorIndex = -4142
  49.             Case Is < 0
  50.               If iNum > 9 Then .Interior.ColorIndex = 46 Else .Interior.ColorIndex = 35
  51.           End Select ' 藍 41 橘 46 綠 35 粉 38
  52.         End With
  53.         lSRow = lSRow + 1
  54.       Loop
  55.       iSCol = iSCol + 1
  56.     Loop
  57.   End With
  58. End Sub
複製代碼
問問題-102.10.2-a.zip (14.43 KB)

TOP

        靜思自在 : 有願放在心裡,沒有身體力行,正如耕田不播種,皆是空過因緣。
返回列表 上一主題