Board logo

標題: 請問Excel 自動判斷正負數數量 [打印本頁]

作者: joefox0725    時間: 2022-12-28 16:45     標題: 請問Excel 自動判斷正負數數量

請問各位Excel 前輩, E儲存格有資料每天不斷的新增要判斷正數與負數的數量 ,跟最後10筆與5筆的正負數數量 ,請問要如何使用  謝謝大家
作者: Andy2483    時間: 2022-12-29 08:49

回復 1# joefox0725


    謝謝前輩發表此主題
後學藉此帖練習需多程式碼,請試試看是否符合前輩需求情境

E欄新增亂數正負值:
[attach]35690[/attach]

在F欄顯示_判斷E欄正0負結果:
[attach]35691[/attach]

凍結窗格_第一列_視窗顯示在最後10筆資料:
[attach]35692[/attach]

在VBA裡放入以下程式碼:

Option Explicit
Sub E欄新增亂數正負值() '這是產生模擬範例用的
Dim Brr As Range
Set Brr = [E65536].End(3).Item(2).Resize(10)
With Brr
   .Formula = "=IF(RAND()>.5,INT(RAND()*100),INT(RAND()*-100))"
   .Value = .Value
End With
[E1] = IIf([E1] = "", "數值", [E1])
End Sub

Sub 在F欄顯示_判斷E欄正0負結果()
Dim Brr As Range
Set Brr = Range([E2], [E65536].End(3)).Offset(, 1)
With Brr
  .Formula = "=IF(E2=0,0,IF(E2>0,""正值"",""負值""))"
End With
[F1] = IIf([F1] = "", "判定", [F1])
End Sub

Sub 凍結窗格_第一列_視窗顯示在最後10筆資料()
With ActiveWindow
   .FreezePanes = False
   .ScrollRow = 1
   .SplitRow = 1
   .FreezePanes = True
   .ScrollRow = [E65536].End(3).Row - 9
End With '
End Sub
作者: hcm19522    時間: 2022-12-29 11:23

https://blog.xuite.net/hcm19522/twblog/590672242
作者: Andy2483    時間: 2022-12-30 14:18

回復 3# hcm19522


    謝謝前輩
後學藉此帖學習到很多知識與經驗,以下是模擬每天統計與圖表
請各位前輩指導
執行前:
[attach]35703[/attach]

全部:
[attach]35704[/attach]

最後5筆:
[attach]35705[/attach]

最後10筆:
[attach]35706[/attach]

Option Explicit
Dim x&
Sub 重複執行_製作範例()
For x = -10 To 0
   Call E欄新增亂數正負值_日期
Next
End Sub

Sub E欄新增亂數正負值_日期() '這是產生模擬範例用的
Dim Arr As Range, Brr As Range
Set Brr = [E65536].End(3).Item(2).Resize(Rnd() * 100 \ 5 + 10)
Set Arr = Brr.Offset(, -4)
With Brr
   .Formula = "=IF(RAND()>.5,INT(RAND()*100),INT(RAND()*-100))"
   .Value = .Value
End With
With Arr
   .Formula = "=TODAY()+" & x
   .Value = .Value
End With
[A1] = IIf([A1] = "", "日期", [A1])
[E1] = IIf([E1] = "", "數值", [E1])
End Sub

Sub 在F欄顯示_判斷E欄正0負結果()
Dim Brr As Range
Set Brr = Range([E2], [E65536].End(3)).Offset(, 1)
With Brr
  .Formula = "=IF(E2=0,0,IF(E2>0,""正數"",""負數""))"
End With
[F1] = IIf([F1] = "", "判定", [F1])
Call 凍結窗格_第一列_視窗顯示在最後10筆資料

End Sub

Sub 凍結窗格_第一列_視窗顯示在最後10筆資料()
With ActiveWindow
   .FreezePanes = False
   .ScrollRow = 1
   .SplitRow = 1
   .FreezePanes = True
   .ScrollRow = [E65536].End(3).Row - 9
End With
[A1].Activate
End Sub

Sub 客戶出貨金額_統計圖表()
Application.ScreenUpdating = False
Call 在F欄顯示_判斷E欄正0負結果

Dim Yrr, i&, Y, Z, V, d, n, R, d5, x, k0, k1, M
Dim Dats As Date, Datn As Date, Arr, Brr, Crr
Set Y = CreateObject("Scripting.Dictionary")
Set Z = CreateObject("Scripting.Dictionary")
Set V = CreateObject("Scripting.Dictionary")
Set Yrr = Range([A1], [E65536].End(3))
R = [A1].End(xlDown).Row
For i = R To 2 Step -1
   Dats = Yrr(2, 1)
   Datn = Yrr(R, 1)
   If Yrr(i, 1) <> "" Then
      d = Yrr(i, 1)
      d5 = Yrr(i, 5)
      If d5 > 0 Then
         Y(d & "|正數") = Y(d & "|正數") + 1
         If Z(d) < 5 Then
            Z(d & "|正數") = Z(d & "|正數") + 1
            Z(d) = Z(d) + 1
         End If
         If V(d) < 10 Then
            V(d & "|正數") = V(d & "|正數") + 1
            V(d) = V(d) + 1
         End If
         ElseIf d5 < 0 Then
            Y(d & "|負數") = Y(d & "|負數") + 1
            If Z(d) < 5 Then
               Z(d & "|負數") = Z(d & "|負數") + 1
               Z(d) = Z(d) + 1
            End If
            If V(d) < 10 Then
               V(d & "|負數") = V(d & "|負數") + 1
               V(d) = V(d) + 1
            End If
         Else
            Y(d & "|零") = Y(d & "|零") + 1
            If Z(d) < 5 Then
               Z(d & "|零") = Z(d & "|零") + 1
               Z(d) = Z(d) + 1
            End If
            If V(d) < 10 Then
               V(d & "|零") = V(d & "|零") + 1
               V(d) = V(d) + 1
            End If
      End If
   End If
Next
ReDim Arr(1 To Y.Count, 1 To 4)
For Each x In Y.KEYS
   If InStr(x, "|") Then
      k0 = Split(x, "|")(0)
      k1 = Split(x, "|")(1)
      If M <> k0 Then
         n = n + 1
         M = k0
         Arr(n, 1) = k0
      End If
      If k1 = "正數" Then Arr(n, 2) = Y(x)
      If k1 = "負數" Then Arr(n, 3) = Y(x)
      If k1 = "零" Then Arr(n, 4) = Y(x)
   End If
Next
Workbooks.Add
[A1].Resize(, 4) = [{"日期","正數","負數","零"}]
[A2].Resize(UBound(Arr), 4) = Arr
Cells.Columns.AutoFit
[E1] = "每日全部正負數數量統計! 區間(" & Dats & "~" & Datn & ")"
[E1].Font.Size = 18
[E1].Font.Bold = True
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range([A1], Cells([A65536].End(xlUp).Row, "D"))
With ActiveSheet.Shapes("圖表 1")
   .ScaleWidth 2, msoFalse, msoScaleFromBottomRight
   .ScaleWidth 1, msoFalse, msoScaleFromTopLeft
   .ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
   .IncrementLeft -500
   .IncrementTop -500
   .IncrementTop 20
   ActiveChart.SeriesCollection(1).ApplyDataLabels
   ActiveChart.SeriesCollection(2).ApplyDataLabels
   ActiveChart.SeriesCollection(3).ApplyDataLabels
End With
[E1].Activate
ActiveSheet.Name = "每日全部正負數"
'------------------------------------------------------
Worksheets.Add.Name = "每日最後5筆"
ReDim Arr(1 To Z.Count, 1 To 4)
n = 0
For Each x In Z.KEYS
   If InStr(x, "|") Then
      k0 = Split(x, "|")(0)
      k1 = Split(x, "|")(1)
      If M <> k0 Then
         n = n + 1
         M = k0
         Arr(n, 1) = k0
      End If
      If k1 = "正數" Then Arr(n, 2) = Z(x)
      If k1 = "負數" Then Arr(n, 3) = Z(x)
      If k1 = "零" Then Arr(n, 4) = Z(x)
   End If
Next
[A1].Resize(, 4) = [{"日期","正數","負數","零"}]
[A2].Resize(UBound(Arr), 4) = Arr
Cells.Columns.AutoFit
[E1] = "每日最後5筆正負數數量統計! 區間(" & Dats & "~" & Datn & ")"
[E1].Font.Size = 18
[E1].Font.Bold = True
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range([A1], Cells([A65536].End(xlUp).Row, "D"))
With ActiveSheet.Shapes("圖表 1")
   .ScaleWidth 2, msoFalse, msoScaleFromBottomRight
   .ScaleWidth 1, msoFalse, msoScaleFromTopLeft
   .ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
   .IncrementLeft -500
   .IncrementTop -500
   .IncrementTop 20
   ActiveChart.SeriesCollection(1).ApplyDataLabels
   ActiveChart.SeriesCollection(2).ApplyDataLabels
   ActiveChart.SeriesCollection(3).ApplyDataLabels
End With
[E1].Activate
'------------------------------------------------------
Worksheets.Add.Name = "每日最後10筆"
ReDim Arr(1 To V.Count, 1 To 4)
n = 0
For Each x In V.KEYS
   If InStr(x, "|") Then
      k0 = Split(x, "|")(0)
      k1 = Split(x, "|")(1)
      If M <> k0 Then
         n = n + 1
         M = k0
         Arr(n, 1) = k0
      End If
      If k1 = "正數" Then Arr(n, 2) = V(x)
      If k1 = "負數" Then Arr(n, 3) = V(x)
      If k1 = "零" Then Arr(n, 4) = V(x)
   End If
Next
[A1].Resize(, 4) = [{"日期","正數","負數","零"}]
[A2].Resize(UBound(Arr), 4) = Arr
Cells.Columns.AutoFit
[E1] = "每日最後10筆正負數數量統計! 區間(" & Dats & "~" & Datn & ")"
[E1].Font.Size = 18
[E1].Font.Bold = True
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlColumnClustered
ActiveChart.SetSourceData Source:=Range([A1], Cells([A65536].End(xlUp).Row, "D"))
With ActiveSheet.Shapes("圖表 1")
   .ScaleWidth 2, msoFalse, msoScaleFromBottomRight
   .ScaleWidth 1, msoFalse, msoScaleFromTopLeft
   .ScaleHeight 1.5, msoFalse, msoScaleFromTopLeft
   .IncrementLeft -500
   .IncrementTop -500
   .IncrementTop 20
   ActiveChart.SeriesCollection(1).ApplyDataLabels
   ActiveChart.SeriesCollection(2).ApplyDataLabels
   ActiveChart.SeriesCollection(3).ApplyDataLabels
End With
[E1].Activate
Set Y = Nothing
Set Z = Nothing
Set V = Nothing
Set Yrr = Nothing
End Sub




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