返回列表 上一主題 發帖

[發問] 日期區間查詢(跨年月)

回復 1# sammay
UserForm3
  1. Dim 日期()
  2. Private Sub UserForm_Initialize()
  3.     CommandButton1.Enabled = False                            '確定鈕控制項: 不可以使用
  4.     日期 = Array(ComboBox1, ComboBox2, ComboBox3, ComboBox4)  '將年月的輸入 置入在陣列
  5.     ComboBox1.RowSource = "下拉選單!c2:c11"
  6.     ComboBox2.RowSource = "下拉選單!d2:d13"
  7.     ComboBox3.RowSource = "下拉選單!c2:c11"
  8.     ComboBox4.RowSource = "下拉選單!d2:d13"
  9. End Sub
  10. Private Sub CommandButton1_Click()
  11.     Dim Data As Range, Rng As Range, Day1 As Date, Day2 As Date, Msg As String, E As Range
  12.     Set Data = Sheets("總表").Range("A3").CurrentRegion
  13.     'Range("A3").CurrentRegion :   總表的資料 A2:D2 ,E欄 請不要有資料輸入
  14.     If Data.Rows.Count = 1 Then         '只有欄位
  15.         MsgBox "總表:  沒有資料 !!!"
  16.         Unload Me
  17.         Exit Sub
  18.     End If
  19.     Day1 = DateSerial(日期(0), 日期(1), 1)                   '轉入日期
  20.     Day2 = DateSerial(日期(2), 日期(3), 1)
  21.     For Each E In Data.Columns(1).Offset(1).Cells             '[A4]->
  22.         If DateSerial(E, E.Cells(1, 2), 1) >= Day1 And DateSerial(E, E.Cells(1, 2), 1) <= Day2 Then
  23.             If Rng Is Nothing Then                     '初次
  24.                 Set Rng = E.Resize(1, 4)
  25.             Else                                        '第二次以後
  26.                 Set Rng = Union(Rng, E.Resize(1, 4))
  27.             End If
  28.         End If
  29.     Next
  30.     Msg = 日期(0) & "/" & 日期(1) & " - " & 日期(2) & "/" & 日期(3)
  31.     If Rng Is Nothing Then
  32.         MsgBox Msg & "找不到  資料"
  33.     Else
  34.         Rng.Copy Sheets("查詢明細").Cells(Rows.Count, 1).End(xlUp).Offset(1)
  35.         'Rng 複製到 "查詢明細"A欄 最後一筆有資料的下一格  Offset(1)
  36.         MsgBox Msg & " 找到  " & Rng.Count / Data.Columns.Count & " 筆資料"
  37.     End If
  38.         Unload Me
  39. End Sub
  40. Private Sub ComboBox1_Change()
  41.     Check_日期
  42. End Sub
  43. Private Sub ComboBox2_Change()
  44.      Check_日期
  45. End Sub
  46. Private Sub ComboBox3_Change()
  47.      Check_日期
  48. End Sub
  49. Private Sub ComboBox4_Change()
  50.     Check_日期
  51. End Sub
  52. Private Sub Check_日期()      '判別 年月輸入
  53.     Dim Msg As Boolean, E As Variant
  54.     For Each E In 日期                          '依序處裡: 年月的輸入
  55.         If Not IsNumeric(E) Then                '不是數字
  56.             CommandButton1.Enabled = False      '確定鈕控制項: 不可以使用
  57.             Msg = True                          'Msg設定為 True
  58.             Exit For
  59.         End If
  60.     Next
  61.     If Msg = False Then                         '日期皆為數字
  62.         If DateSerial(日期(0), 日期(1), 1) <= DateSerial(日期(2), 日期(3), 1) Then
  63.           'DateSerial(年,月, 1)
  64.             CommandButton1.Enabled = True           '確定鈕控制項: 可以使用
  65.         Else
  66.             CommandButton1.Enabled = False          '確定鈕控制項: 不可以使用
  67.         End If
  68.     End If
  69. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2012-1-17 09:18 編輯

回復 4# sammay
CurrentRegion 屬性   該物件代表目前的區域。目前區域是指以任意空白列及空白欄的組合為邊界的範圍。
  1. Sub Ex()
  2.     With ActiveCell.CurrentRegion.Borders
  3.         .LineStyle = 1
  4.         .ColorIndex = 7
  5.     End With
  6. End Sub
複製代碼
如圖 的範圍中選取單一有資料的杵存格 執行Ex巨集



其中"/1"為何意呢?還有+1911用意呢?
/1 :            使其日期為每月的1日
+1911 :  因你的年度是民國, +1911成為西元年度

TOP

        靜思自在 : 心中常存善解、包容、感思、知足、惜福。
返回列表 上一主題