返回列表 上一主題 發帖

[發問] 請問 ARRAY範圍 改成 動態 指定 範圍問題 請前輩們指教

[發問] 請問 ARRAY範圍 改成 動態 指定 範圍問題 請前輩們指教

Private Sub CommandButton2_Click()
'3個月
If TextBox2.Value = 1 Then A = Array(3, 5, 7, 15, 17, 19)
If TextBox2.Value = 2 Then A = Array(5, 7, 9, 17, 19, 21)
If TextBox2.Value = 3 Then A = Array(7, 9, 11, 19, 21, 23)
If TextBox2.Value = 4 Then A = Array(9, 11, 13, 21, 23, 25)
If TextBox2.Value = 5 Then A = Array(11, 13, 15, 23, 25)
If TextBox2.Value = 6 Then A = Array(13, 15, 17, 25)
If TextBox2.Value = 7 Then A = Array(15, 17, 19)
If TextBox2.Value = 8 Then A = Array(17, 19, 21)
If TextBox2.Value = 9 Then A = Array(19, 21, 23)
If TextBox2.Value = 10 Then A = Array(21, 23, 25)
If TextBox2.Value = 11 Then A = Array(23, 25)
If TextBox2.Value = 12 Then A = Array(25)
If TextBox2.Value = "" Then A = Array(3, 5, 7, 15, 17, 19)

For I = LBound(A) To UBound(A)
    For J = 4 To Cells(3, 4).End(xlToRight).Column
        If Cells(A(I), J) = "日班" Then Cells(A(I), J) = "夜班"
        If Cells(A(I), J) <> "" And Cells(A(I), J) = "夜班" Then
           Cells(A(I), J).Font.Color = RGB(Cells(1, 26), Cells(1, 27), Cells(1, 28))
           Cells(A(I), J).Interior.Color = RGB(Cells(1, 32), Cells(1, 33), Cells(1, 34))
        End If
    Next J
Next I
Erase A

UserForm3.Hide
TextBox2.Value = ""
End Sub

請問前輩們 以下這段該如何改寫 才能夠改成動態的規律 如何可以縮減程式碼
還有一個延伸問題 當延伸到最後一個月時不滿3個月 該如何延伸至下一年的第一個月
請前輩們指教 程式特別攏長 看起來可能會有些不方便 不好意思 ^^"

If TextBox2.Value = 1 Then A = Array(3, 5, 7, 15, 17, 19)
If TextBox2.Value = 2 Then A = Array(5, 7, 9, 17, 19, 21)
If TextBox2.Value = 3 Then A = Array(7, 9, 11, 19, 21, 23)
If TextBox2.Value = 4 Then A = Array(9, 11, 13, 21, 23, 25)
If TextBox2.Value = 5 Then A = Array(11, 13, 15, 23, 25)
If TextBox2.Value = 6 Then A = Array(13, 15, 17, 25)
If TextBox2.Value = 7 Then A = Array(15, 17, 19)
If TextBox2.Value = 8 Then A = Array(17, 19, 21)
If TextBox2.Value = 9 Then A = Array(19, 21, 23)
If TextBox2.Value = 10 Then A = Array(21, 23, 25)
If TextBox2.Value = 11 Then A = Array(23, 25)
If TextBox2.Value = 12 Then A = Array(25)
If TextBox2.Value = "" Then A = Array(3, 5, 7, 15, 17, 19)
新增資料夾.rar (109.11 KB)

就是月份的部分  改成可以用輸入表單的方式 2個月輪一次 或著是 3個月輪一次

TOP

回復 2# 軒云熊

自己寫的規則自己看得懂, 這才是最重要的, 簡化不一定就好!!!
只一張工作表就看得眼花, 不太想去了解,
日期做成文字, 對EXCEL來說, 不是好習慣, 對往後的維護及統計運算都有障礙,
讓看到的是"格式", 內容是真實數值, 是最好的~~

TOP

謝謝准提大大 往後我會改掉這個壞習慣的

TOP

本帖最後由 n7822123 於 2020-6-17 02:06 編輯

回復 1# 軒云熊

需求:
請問前輩們 以下這段該如何改寫 才能夠改成動態的規律 如何可以縮減程式碼

If TextBox2.Value = 1 Then A = Array(3, 5, 7, 15, 17, 19)
If TextBox2.Value = 2 Then A = Array(5, 7, 9, 17, 19, 21)
If TextBox2.Value = 3 Then A = Array(7, 9, 11, 19, 21, 23)
If TextBox2.Value = 4 Then A = Array(9, 11, 13, 21, 23, 25)
If TextBox2.Value = 5 Then A = Array(11, 13, 15, 23, 25)
If TextBox2.Value = 6 Then A = Array(13, 15, 17, 25)
If TextBox2.Value = 7 Then A = Array(15, 17, 19)
If TextBox2.Value = 8 Then A = Array(17, 19, 21)
If TextBox2.Value = 9 Then A = Array(19, 21, 23)
If TextBox2.Value = 10 Then A = Array(21, 23, 25)
If TextBox2.Value = 11 Then A = Array(23, 25)
If TextBox2.Value = 12 Then A = Array(25)
If TextBox2.Value = "" Then A = Array(3, 5, 7, 15, 17, 19)

同意準大說的,自己寫的程式自己能看懂,才是最好的!
別人寫的很短很厲害,你如果看不懂,之後要改程式也無從下手,會更痛苦
依你的需求,程式改成如下,但執行速度不會比較快~


k = TextBox2.Value: If k = "" Then k = 1
For i = k To 12: Idx = Idx + 1
  If Idx < 4 Or Idx > 6 Then
    itm = itm + 1: If itm = 7 Then Exit For
    s = s & "," & i * 2 + 1
  End If
Next i
A = Split(Mid(s, 2), ",")
程式是依需求寫的,需求表達不清楚
或者沒有上傳附件,愛莫能助

TOP

本帖最後由 n7822123 於 2020-6-17 02:30 編輯

回復 1# 軒云熊


寫法二:
k = TextBox2.Value: If k = "" Then k = 1
For i = k To 12
  s = s & "," & i * 2 + 1
  itm = itm + 1: If itm = 6 Then Exit For
  If i = k + 2 Then i = i + 5
Next i
A = Split(Mid(s, 2), ",")


看你覺得那個你比較好理解
新手不建議在For迴圈裡面For迴圈的變數值
以免不小心就弄成無窮迴圈,所以不太推薦此寫法
程式是依需求寫的,需求表達不清楚
或者沒有上傳附件,愛莫能助

TOP

本帖最後由 n7822123 於 2020-6-17 02:43 編輯

回復 6# n7822123


漏改到,修正寫法二

k = TextBox2.Value: If k = "" Then k = 1
For i = k To 12
  s = s & "," & i * 2 + 1
  itm = itm + 1: If itm = 6 Then Exit For
  If i = k + 2 Then i = i + 3    '跳過後3個迴圈
Next i
A = Split(Mid(s, 2), ",")
程式是依需求寫的,需求表達不清楚
或者沒有上傳附件,愛莫能助

TOP

謝謝 n7822123 大大  程式少很多行了
當延伸到最後一個月時不滿3個月 該如何延伸至下一年的第一個月呢?

TOP

本帖最後由 准提部林 於 2020-6-17 19:05 編輯

回復 8# 軒云熊


1) 日曆只有一年份, 怎延伸下年度???
2) 為何要弄這麼多form???
3) 填底色而己, 何以要用vga表單去調, 不覺太複雜???
4) 每個月用31欄, 要怎列印成一張?

TOP

回准提大大那個我只是想要練習用的因為我還是新手>"<    R1C4 的 2020 改成2021 可以延伸顯示下一年份
班別 輪班 的部分 是可以延伸 但是每個月天數不同 要做到 2~3個月的延伸就卡住了 ^^"

TOP

        靜思自在 : 要用心,不要操心、煩心。
返回列表 上一主題