• 2

VBA 成績篩選




Sub tkyo()

   

   

    Application.ScreenUpdating = False

   

    Worksheets("工作表1").Select    '國文

   

   

        If Range("I4") = 2 Then

        If Cells(4, 3) <= Cells(5, 3) Then

        End If

    

        Else:

        If Range("I4") = 3 Then

        If Cells(4, 3) <= Cells(5, 3) _

        And Cells(5, 3) <= Cells(6, 3) _

        Then

   

        Else:

        If Range("I4") = 4 Then

        If Cells(4, 3) <= Cells(5, 3) _

        And Cells(5, 3) <= Cells(6, 3) _

        And Cells(6, 3) <= Cells(7, 3) _

        Then

   

        Else:

        If Range("I4") = 5 Then

        If Cells(4, 3) <= Cells(5, 3) _

        And Cells(5, 3) <= Cells(6, 3) _

        And Cells(6, 3) <= Cells(7, 3) _

        And Cells(7, 3) <= Cells(8, 3) _

        Then

   

        Else:

        If Range("I4") = 6 Then

        If Cells(4, 3) <= Cells(5, 3) _

        And Cells(5, 3) <= Cells(6, 3) _

        And Cells(6, 3) <= Cells(7, 3) _

        And Cells(7, 3) <= Cells(8, 3) _

        And Cells(8, 3) <= Cells(9, 3) _

        Then



   

End If

End If

End If

End If

End If

End If

End If

End If

End If

    Worksheets("工作表1").Select    '英文

        If Range("J4") = 2 Then

        If Cells(4, 4) <= Cells(5, 4) Then

        End If

    

        Else:

        If Range("I4") = 3 Then

        If Cells(4, 4) <= Cells(5, 4) _

        And Cells(5, 4) <= Cells(6, 4) _

        Then

   

        Else:

        If Range("I4") = 4 Then

        If Cells(4, 4) <= Cells(5, 4) _

        And Cells(5, 4) <= Cells(6, 4) _

        And Cells(6, 4) <= Cells(7, 4) _

        Then

   

        Else:

        If Range("I4") = 5 Then

        If Cells(4, 4) <= Cells(5, 4) _

        And Cells(5, 4) <= Cells(6, 4) _

        And Cells(6, 4) <= Cells(7, 4) _

        And Cells(7, 4) <= Cells(8, 4) _

        Then

   

        Else:

        If Range("I4") = 6 Then

        If Cells(4, 4) <= Cells(5, 4) _

        And Cells(5, 4) <= Cells(6, 4) _

        And Cells(6, 4) <= Cells(7, 4) _

        And Cells(7, 4) <= Cells(8, 4) _

        And Cells(8, 4) <= Cells(9, 4) _

        Then


End If

End If

End If

End If

End If

End If

End If

End If

End If



    Worksheets("工作表1").Select    '數學

        If Range("K4") = 2 Then

        If Cells(4, 5) <= Cells(5, 5) Then

        End If

    

        Else:

        If Range("I5") = 3 Then

        If Cells(4, 5) <= Cells(5, 5) _

        And Cells(5, 5) <= Cells(6, 5) _

        Then

   

        Else:

        If Range("I5") = 4 Then

        If Cells(4, 5) <= Cells(5, 5) _

        And Cells(5, 5) <= Cells(6, 5) _

        And Cells(6, 5) <= Cells(7, 5) _

        Then

   

        Else:

        If Range("I5") = 5 Then

        If Cells(4, 5) <= Cells(5, 5) _

        And Cells(5, 5) <= Cells(6, 5) _

        And Cells(6, 5) <= Cells(7, 5) _

        And Cells(7, 5) <= Cells(8, 5) _

        Then

   

        Else:

        If Range("I5") = 6 Then

        If Cells(4, 5) <= Cells(5, 5) _

        And Cells(5, 5) <= Cells(6, 5) _

        And Cells(6, 5) <= Cells(7, 5) _

        And Cells(7, 5) <= Cells(8, 5) _

        And Cells(8, 5) <= Cells(9, 5) _

        Then


End If

End If

End If

End If

End If

End If

End If

End If

End If



    'Range("I6") = ("國英數皆有進步")

    Application.ScreenUpdating = True



End Sub



成績是亂數產生

國英數 週期自行填入(可以比較出成績是否進步也比較彈性)

但問題是~

我該如何讓國英數週期選擇(填入)的3個條件都成立才顯示

Range("I6") = ("國英數皆有進步")


很抱歉,我mobile01密碼忘了。所以沒法用電腦直接上傳檔案
2016-10-24 0:58 發佈
文章關鍵字 VBA 成績篩選
天空藍yoyo wrote:
我該如何讓國英數週期選擇(填入)的3個條件都成立才顯示

Range("I6") = ("國英數皆有進步") ...(恕刪)


您這個問題,其實用函數就可以了,像是 sumproduct(),不需要vba
函數的話,站上很多範例了,自己找一下

您那100多行的程式碼,有太多不需要的if判斷句了

根據您有問題的程式碼來看,似乎是要跟上一期的成績做比較
vba 簡單一點的寫法,請參考

Sub test()
'國
If Cells(Range("i4") + 3, 3) >= Cells(Range("i4") + 2, 3) Or Range("i4") = 1 Then c = 1
'英
If Cells(Range("j4") + 3, 4) >= Cells(Range("j4") + 2, 4) Or Range("j4") = 1 Then e = 1
'數
If Cells(Range("k4") + 3, 5) >= Cells(Range("k4") + 2, 5) Or Range("k4") = 1 Then m = 1

If c + e + m = 3 Then Range("i6") = "ok" Else Range("i6") = "no"

End Sub

snare wrote:
您這個問題,其實用函數就可以了,
像是 sumproduct(),不需要vba


我也贊成如果可以用函數解決的問題,就不需要動用程式,
因為懂得 VBA 程式的高手畢竟只是少數!

話說 SUMPRODUCT() 真的是很好用的函數!
My Interior Knowledge is Extraordinaire
snare wrote:
您這個問題,其實用函...(恕刪)



特別感謝snare 提點

但我的周期選擇,指的是可以任意填選1~2 or 1~3 or 1~4 or 1~5 or 1~6 or 1~7(期數比較)第1期是固定,後面就看要比較幾個期數這樣

就是說想比較幾期都行

然而期數比較完成,在由國英數3方確認皆為進步

至於sumproduct(). 正努力了解中...


很抱歉現在才回文,也因為晚下班 現在才坐在電腦桌前@@

天空藍yoyo wrote:
但我的周期選擇,
指的是可以任意填選1~2 or 1~3 or 1~4 or 1~5 or 1~6 or 1~7(期數比較)
第1期是固定,後面就看要比較幾個期數這樣


不確定您的成績比較是怎麼比法,
附圖是以輸入的期數和前次作比較,
例如英文是和第四週期比,
若為其它方式,只需稍微修改函數中的參數即可!



My Interior Knowledge is Extraordinaire
天空藍yoyo wrote:
指的是可以任意填選1~2 or 1~3 or 1~4 or 1~5 or 1~6 or 1~7(期數比較)第1期是固定,後面就看要比較幾個期數這樣...(恕刪)


看不懂 +1

第一期固定??
2期,是第1期跟第2期比大小嗎?
3期,是第1期跟第3期比大小嗎?

請列出一個範例來看看…
天空藍yoyo wrote:
Sub tkyo ...(恕刪)





Sub tkyo()
Dim kp As Boolean
For i = 0 To 2
For j = 1 To Range("I4").Offset(0, i).Value - 1
If Range("C4").Offset(j - 1, i) >= Range("C4").Offset(j, i) Then kp = True
Next j
Next i
If kp = True Then
Range("I6") = "需再加油!"
Else
Range("I6") = "國英數皆有進步!"
End If
End Sub


跟前一期成績相等算進步嗎?
上面用成績大於前期才算進步來寫
如果要相等也算的話把 >= 改成 >
snare wrote:
看不懂 +1 第一期...(恕刪)



哎呀!我真是笨的可以@@

((~以第7期為最新成績~))
(以下數字皆代表(期))

'國文
填入期數2。代表2期做比較
7>6 then

填入期數3。代表3期做比較
7>6
and
6>5 then

填入期數4。代表4期做比較
7>6
and
6>5
and
5>4 then

填入期數5。代表5期做比較
7>6
and
6>5
and
5>4
and
4>3 then

填入期數6。代表6期做比較
7>6
and
6>5
and
5>4
and
4>3
and
3>2 then

填入期數7。代表7期做比較
7>6
and
6>5
and
5>4
and
4>3
and
3>2
and
2>1 then

然而,國英數3科,選擇周期填入的數字為多期比較(彈性)

國英數3科皆進步,Range("I6")=ok Else Range("I6")=no

因電腦不在身邊,用手機簡短寫一下
YS2000 wrote:
Sub tkyo()...(恕刪)



這迴圈應該可以解開我的問題

等回家後,在試做看看

感謝您YS2000

天空藍yoyo wrote:
哎呀!我真是笨的可...(恕刪)



從7開始往前比

Sub tkyo()
Dim kp As Boolean
For i = 0 To 2
For j = 1 To Range("I4").Offset(0, i).Value - 1
If Range("C10").Offset(1 - j, i) <= Range("C10").Offset(-j, i) Then kp = True
Next j
Next i
If kp = True Then
Range("I6") = "需再加油!"
Else
Range("I6") = "國英數皆有進步!"
End If
End Sub
  • 2
內文搜尋
X
評分
評分
複製連結
Mobile01提醒您
您目前瀏覽的是行動版網頁
是否切換到電腦版網頁呢?